2017-11-03 02:42:57 -04:00
|
|
|
from jose import jwt
|
2017-11-02 01:36:16 -04:00
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
|
2017-11-02 23:37:39 -04:00
|
|
|
def encode(payload):
|
|
|
|
return jwt.encode(payload, settings.SECRET_KEY, algorithm='HS256')
|
|
|
|
|
|
|
|
|
|
|
|
def decode(token):
|
|
|
|
return jwt.decode(token, settings.SECRET_KEY, algorithms=('HS256',))
|
|
|
|
|
|
|
|
|
2017-11-03 00:51:27 -04:00
|
|
|
def gen_auth_code(req):
|
2017-11-02 01:36:16 -04:00
|
|
|
code = {
|
2017-11-03 00:51:27 -04:00
|
|
|
'uid': req.user.id,
|
2017-11-03 01:14:30 -04:00
|
|
|
'cid': req.POST['client_id'],
|
|
|
|
'uri': req.POST['redirect_uri'],
|
|
|
|
'typ': req.POST.get('response_type', 'id'),
|
2017-11-02 23:33:27 -04:00
|
|
|
'iat': datetime.utcnow(),
|
|
|
|
'exp': datetime.utcnow() + timedelta(seconds=30),
|
2017-11-02 01:36:16 -04:00
|
|
|
}
|
2017-11-03 01:14:30 -04:00
|
|
|
if 'scope' in req.POST:
|
|
|
|
code['sco'] = ' '.join(req.POST.getlist('scope'))
|
2017-11-02 01:36:16 -04:00
|
|
|
|
2017-11-03 01:14:30 -04:00
|
|
|
return encode(code)
|
2017-11-03 02:18:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
def gen_token(code):
|
|
|
|
tok = {
|
|
|
|
'uid': code['uid'],
|
|
|
|
'cid': code['cid'],
|
|
|
|
'sco': code['sco'],
|
|
|
|
'iat': datetime.utcnow(),
|
|
|
|
}
|
2017-11-03 02:42:57 -04:00
|
|
|
return encode(tok)
|