Skip to content

Instantly share code, notes, and snippets.

@faustomilletari
Created April 26, 2020 17:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faustomilletari/9be4d12ed4fc54742e0ea9acc0032585 to your computer and use it in GitHub Desktop.
Save faustomilletari/9be4d12ed4fc54742e0ea9acc0032585 to your computer and use it in GitHub Desktop.
Issue and verify JWT tokens (Medium post)
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