Created
April 26, 2020 17:31
-
-
Save faustomilletari/9be4d12ed4fc54742e0ea9acc0032585 to your computer and use it in GitHub Desktop.
Issue and verify JWT tokens (Medium post)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jwt | |
from datetime import datetime, timedelta | |
JWT_SECRET = '****CHANGE_THIS_TO_KICKASS_SECRET_$#($#@(&$(_CHANGE_THIS_TO_KICKASS_SECRET_#*@#*(@_CHANGE_THIS_TO_KICKASS_SECRET****' | |
JWT_ALGORITHM = 'HS256' | |
JWT_EXP_DELTA_SECONDS = 900 | |
def give_jws_token(email): | |
payload = { | |
'email': email, | |
'exp': datetime.utcnow() + timedelta(seconds=JWT_EXP_DELTA_SECONDS) | |
} | |
jwt_token = jwt.encode(payload, JWT_SECRET, JWT_ALGORITHM).decode('ascii') | |
return jwt_token | |
def validate_jwt(jwt_token): | |
try: | |
payload = jwt.decode(jwt_token.encode('utf-8'), JWT_SECRET, algorithms=[JWT_ALGORITHM]) | |
except (jwt.DecodeError, jwt.ExpiredSignatureError): | |
payload = None | |
return payload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment