Skip to content
Snippets Groups Projects

Middleware REST API Bearer Token Generator

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Marvin S. Addison
    Edited
    gen-bearer-token.py 674 B
    #!/usr/bin/env python
    import jwt
    import os
    import sys
    import time
    
    # Token validity period in hours
    # Set to 0 to produce a token that never expires (not recommended)
    VALIDITY_PERIOD=12
    
    if len(sys.argv) < 3:
      print "USAGE: ed-id-service /path/to/private.key"
      sys.exit(0)
    
    issuer = sys.argv[1]
    key_path = sys.argv[2]
    
    with open(key_path, 'r') as f:
      key = f.read()
    
    # Create authentication token that is valid for VALIDITY_PERIOD hours
    now = int(time.time())
    claims = {}
    claims['iss'] = 'uusid={0},ou=services,dc=vt,dc=edu'.format(issuer)
    claims['iat'] = now
    claims['exp'] = now + VALIDITY_PERIOD * 60 * 60
    token = jwt.encode(claims, key, algorithm='RS256')
    print token
    0% or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment