moved JWT creation logic to shared package for re-usability
parent
d1160891b1
commit
5122dc96d0
|
@ -1,7 +1,11 @@
|
|||
from datetime import datetime
|
||||
from logging import getLogger
|
||||
from time import time
|
||||
|
||||
import jwt
|
||||
|
||||
THIRTY_DAYS = 2592000
|
||||
|
||||
_log = getLogger(__package__)
|
||||
|
||||
|
||||
|
@ -9,13 +13,32 @@ class AuthenticationError(Exception):
|
|||
pass
|
||||
|
||||
|
||||
def encode_auth_token(secret_key, user_uuid):
|
||||
"""
|
||||
Generates the Auth Token
|
||||
:return: string
|
||||
"""
|
||||
token_expiration = time() + THIRTY_DAYS
|
||||
payload = dict(iat=datetime.utcnow(), exp=token_expiration, sub=user_uuid)
|
||||
selene_token = jwt.encode(
|
||||
payload,
|
||||
secret_key,
|
||||
algorithm='HS256'
|
||||
)
|
||||
|
||||
# before returning the token, convert it from bytes to string so that
|
||||
# it can be included in a JSON response object
|
||||
return selene_token.decode()
|
||||
|
||||
|
||||
def decode_auth_token(auth_token: str, secret_key: str) -> tuple:
|
||||
"""
|
||||
Decodes the auth token
|
||||
:param auth_token: the Selene JSON Web Token extracted from the request cookies.
|
||||
:param auth_token: the Selene JSON Web Token extracted from cookies.
|
||||
:param secret_key: the key needed to decode the token
|
||||
:return: two-value tuple containing a boolean value indicating if the token is good and the
|
||||
user UUID extracted from the token. UUID will be None if token is invalid.
|
||||
:return: two-value tuple containing a boolean value indicating if the
|
||||
token is good and the user UUID extracted from the token. UUID will
|
||||
be None if token is invalid.
|
||||
"""
|
||||
try:
|
||||
payload = jwt.decode(auth_token, secret_key)
|
||||
|
|
Loading…
Reference in New Issue