Created
November 4, 2021 08:15
-
-
Save bchaber/e7b5c6c12f7c09038c115e6e4ca83892 to your computer and use it in GitHub Desktop.
Script for token generation
This file contains hidden or 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
# $ export JWT_SECRET=something | |
# $ python3 -m pip install pyjwt | |
import jwt, os | |
import datetime | |
SECRET = os.getenv("JWT_SECRET") | |
NOW = datetime.datetime.now(tz=datetime.timezone.utc) | |
def user_token(uid, role, seconds=60): | |
dt = datetime.timedelta(seconds=seconds) | |
return jwt.encode({"exp": NOW + dt, "uid":uid, "role":role, "aud":"grocery.shop"}, SECRET, algorithm="HS256") | |
def payment_token(payment, seconds=90): | |
dt = datetime.timedelta(seconds=seconds) | |
return jwt.encode({"exp": NOW + dt, "payment":payment, "aud":"grocery.shop"}, SECRET, algorithm="HS256") | |
print("== Admin token ===") | |
print(user_token(1, "admin", seconds=360), end="\n\n") | |
print("== User #1 token =") | |
print(user_token(101, "customer", seconds=360), end="\n\n") | |
print("== User #2 token =") | |
print(user_token(102, "customer", seconds=360), end="\n\n") | |
print("== Payment token =") | |
print(payment_token("DEAFBEE", seconds=360)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment