Skip to content

Instantly share code, notes, and snippets.

@cuongld2
Created March 17, 2020 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cuongld2/c28414a3a173d480323685a23e180277 to your computer and use it in GitHub Desktop.
Save cuongld2/c28414a3a173d480323685a23e180277 to your computer and use it in GitHub Desktop.
generate and decode access token
from datetime import timedelta, datetime
import jwt
secret_key = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
algorithm = "HS256"
def create_access_token(*, data: dict, expires_delta: timedelta = None):
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=15)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, secret_key, algorithm=algorithm)
return encoded_jwt
def decode_access_token(*, data: str):
to_decode = data
return jwt.decode(to_decode, secret_key, algorithm=algorithm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment