From 40810d631049994aee268dface684f38eb8f49ab Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Fri, 3 Nov 2017 14:37:39 +1100 Subject: [PATCH] Refactor the actual JWT calls into separate functions since I'll be needing them for tokens as well as auth codes --- lemonauth/tokens.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lemonauth/tokens.py b/lemonauth/tokens.py index 88b349b..240f5cd 100644 --- a/lemonauth/tokens.py +++ b/lemonauth/tokens.py @@ -4,6 +4,14 @@ from datetime import datetime, timedelta from django.conf import settings +def encode(payload): + return jwt.encode(payload, settings.SECRET_KEY, algorithm='HS256') + + +def decode(token): + return jwt.decode(token, settings.SECRET_KEY, algorithms=('HS256',)) + + def gen_auth_code(post): params = {'me': post['me']} if 'state' in post: @@ -20,9 +28,9 @@ def gen_auth_code(post): if 'scope' in post: code['sco'] = ' '.join(post.getlist('scope')) - params['code'] = jwt.encode(code, settings.SECRET_KEY, algorithm='HS256') + params['code'] = encode(code) return params def verify_auth_code(c): - return jwt.decode(c, settings.SECRET_KEY, algorithms=('HS256',)) + return decode(c)