Created
March 1, 2019 17:15
-
-
Save eduardocardoso/8086cf1e8c8761b9003ddb43f27a4146 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 | |
from time import time | |
import jwt | |
ISSUER = '8eb12629d265493588af989decb91209' | |
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', ttl=60, alg='RS256'): | |
iat = time() | |
payload = { | |
'sub': subject, | |
'iss': ISSUER, | |
'iat': int(iat), | |
'exp': int(iat + ttl), | |
} | |
private_key = _get_private_key() | |
return jwt.encode(payload, key=private_key, algorithm=alg).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