-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxelektronika.py
52 lines (35 loc) · 1.65 KB
/
xelektronika.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import models
#from models import *
from functools import wraps
import jwt
from werkzeug.security import generate_password_hash, check_password_hash
import datetime
now = datetime.datetime.now()
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
token = None
if 'x-token-access' in request.headers: #llooping
token = request.headers['x-token-access']
try:
data = jwt.decode(token, app.config['SECRET_KEY'])
current_user = models.userid.query.filter_by(nomor = data['id']).first()
except:
return jsonify({'status': False, 'message' : 'token invalid.'}), 401
else:
return jsonify({'status': False, 'message' : 'token is missing'}), 401
return f(current_user, *args, **kwargs)
return decorated
def verify(username, password):
user = models.userid.query.filter_by(kode=username).first()
if user:
phash = generate_password_hash(user.psw)
divisi = models.divisiuserid.query.filter_by(nomoruserid=user.nomor).first()
if check_password_hash(phash, password):
#payload
token = jwt.encode({'id' : user.nomor, 'uuid' : user.lokasi, 'guid' : divisi.nomordivisi, 'exp' : datetime.datetime.utcnow() + datetime.timedelta(minutes=10), 'iss': 'klikmediasoft'}, app.config['SECRET_KEY'])
return jsonify({'status': True, 'token': token.decode('UTF-8')})
else:
return jsonify({'status': False, 'message': 'invalid credentials'}), 401
else:
return jsonify({'status': False, 'message': 'login is empty.'}), 401