Created
March 17, 2020 05:51
-
-
Save cuongld2/c28414a3a173d480323685a23e180277 to your computer and use it in GitHub Desktop.
generate and decode access token
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
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