Skip to content

Instantly share code, notes, and snippets.

@eduardocardoso
Created March 1, 2019 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eduardocardoso/8086cf1e8c8761b9003ddb43f27a4146 to your computer and use it in GitHub Desktop.
Save eduardocardoso/8086cf1e8c8761b9003ddb43f27a4146 to your computer and use it in GitHub Desktop.
Generating a JWT token
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