Update: Session JWT added (not working now)

This commit is contained in:
Yûki VACHOT 2022-01-05 10:09:40 +01:00
parent 1771c63b36
commit 6dee0956dd
5 changed files with 60 additions and 41 deletions

View file

@ -0,0 +1,38 @@
from datetime import datetime, timedelta
from flask import current_app as app
import jwt
def create_auth_token(user, time_second=1800):
try:
time = datetime.now()
payload = {
'exp': time + timedelta(days=0, seconds=time_second),
'iat': time,
'user': user
}
return jwt.encode(
payload,
app.config.get('SECRET_KEY'),
algorithm='HS256'
)
except Exception as e:
return e
def decode_auth_token(auth_token):
try:
payload = jwt.decode(
auth_token,
app.config.get('SECRET_KEY')
)
return {'success': True, 'payload': payload['user']}
except jwt.ExpiredSignatureError:
return {'success': False, 'message': 'Signature expired . Please log in again.'}
except jwt.InvalidTokenError:
return {'success': False, 'message': 'Invalid token. Please log in again.'}
def check_auth_token(request):
token = request.cookies.get('SESSIONID')
return decode_auth_token(token)