-
-
Save rcmachado/8ead12e294624483bbe8f575127c53d9 to your computer and use it in GitHub Desktop.
Generating a JWT token
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
from __future__ import absolute_import | |
# | |
# To run: | |
# | |
# $ pip install pyjwt | |
# $ python main.py | |
# | |
import jwt | |
ISSUER = '8eb12629d265493588af989decb91209' | |
IAT = '1516239022' | |
EXP = '1526239022' | |
def _get_private_key(): | |
# File private_key.pem generated using `openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:4096` | |
with open('private_key.pem', 'r') as f: | |
return f.read() | |
def build_jwt_token(subject='sys'): | |
payload = { | |
'sub': subject, | |
'iss': ISSUER, | |
'iat': IAT, | |
'exp': EXP, | |
} | |
private_key = _get_private_key() | |
return jwt.encode(payload, key=private_key, algorithm='RS256').decode('utf-8') | |
if __name__ == '__main__': | |
print('sys token: ' + build_jwt_token()) | |
print('user token: ' + build_jwt_token('usr:123123-123123-45325')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment