def generate_token(user_id, expire_in=None, data={}, issuer=None, iat=None):
    """Generate a new JWT token for this user_id. Default expiration date
    is 1 year from creation time"""
    assert user_id, "No user_id passed to generate_token()"
    assert isinstance(data, dict), "generate_token(data=) should be a dictionary"
    assert get_config().jwt_secret, "No JWT secret configured in pymacaron"

    if not issuer:
        issuer = get_config().jwt_issuer

    assert issuer, "No JWT issuer configured for pymacaron"

    if expire_in is None:
        expire_in = get_config().jwt_token_timeout

    if iat:
        epoch_now = iat
    else:
        epoch_now = to_epoch(timenow())
    epoch_end = epoch_now + expire_in

    data['iss'] = issuer
    data['sub'] = user_id
    data['aud'] = get_config().jwt_audience
    data['exp'] = epoch_end
    data['iat'] = epoch_now

    headers = {
        "typ": "JWT",
        "alg": "HS256",
        "iss": issuer,
    }

    log.debug("Encoding token with data %s and headers %s (secret:%s****)" % (data, headers, get_config().jwt_secret[0:8]))

    t = jwt.encode(
        data,
        get_config().jwt_secret,
        headers=headers,
    )

    if type(t) is bytes:
        t = t.decode("utf-8")

    return t
def generate_token ( user_id , expire_in = None , data = { } , issuer = None , iat = None ) : assert user_id , "No user_id passed to generate_token()" assert isinstance ( data , dict ) , "generate_token(data=) should be a dictionary" assert get_config ( ) . jwt_secret , "No JWT secret configured in pymacaron" if not issuer : issuer = get_config ( ) . jwt_issuer assert issuer , "No JWT issuer configured for pymacaron" if expire_in is None : expire_in = get_config ( ) . jwt_token_timeout if iat : epoch_now = iat else : epoch_now = to_epoch ( timenow ( ) ) epoch_end = epoch_now + expire_in data [ 'iss' ] = issuer data [ 'sub' ] = user_id data [ 'aud' ] = get_config ( ) . jwt_audience data [ 'exp' ] = epoch_end data [ 'iat' ] = epoch_now headers = { "typ" : "JWT" , "alg" : "HS256" , "iss" : issuer , } log . debug ( "Encoding token with data %s and headers %s (secret:%s****)" % ( data , headers , get_config ( ) . jwt_secret [ 0 : 8 ] ) ) t = jwt . encode ( data , get_config ( ) . jwt_secret , headers = headers , ) if type ( t ) is bytes : t = t . decode ( "utf-8" ) return t
Generate a new JWT token for this user_id. Default expiration date
    is 1 year from creation time