简体   繁体   English

如何使用 Flask-JWT-Extended 的 create_access_token() 设置 JWT 的“iss”声明

[英]How to set the 'iss' claim of JWT using Flask-JWT-Extended's create_access_token()

Is there a way to set the iss claim of the JWT that is generated by create_access_token of Flask-JWT-Extended?有没有办法设置由 Flask-JWT-Extended 的 create_access_token 生成的 JWT 的 iss 声明?

I tried to put the iss claim under the parameter 'user_claims' of the create_access_token:我试图将 iss 声明放在 create_access_token 的参数“user_claims”下:

access_token = create_access_token(
   identity=data['username'],
   expires_delta = timedelta(seconds=JWT_LIFE_SPAN),
   user_claims={'iss': ISSUER}
)

However, when I decoded the token using PyJWT from the side of resource server, it looked like this:但是,当我从资源服务器端使用 PyJWT 解码令牌时,它看起来像这样:

{'iat': 1597581227, 'nbf': 1597581227, 'jti': '4e6c9677-d698-421c-91c4-0b2f3a6da4e9', 'exp': 
1597583027, 'identity': 'asdf', 'fresh': False, 'type': 'access', 'user_claims': {'iss': 
'sample-auth-server'}}

I tried to look for a configuration options from the docs , but I can't find any option to set the iss.我试图从docs中查找配置选项,但我找不到任何设置 iss 的选项。 There's an iss set, but it is under the user_claims.有一个 iss 集,但它在 user_claims 下。 What I want to accomplish is to set it as one of the registered claims for the JWT.我想要完成的是将其设置为 JWT 的注册声明之一。

I think you can make use of encode_access_token api and create an encoded access code with issuer.我认为您可以使用encode_access_token api 并使用发行人创建编码的访问代码。

Example:例子:

 encode_access_token(
    identity=data['username'],
    issuer=JWT_ISSUER,
    expires_delta=timedelta(seconds=JWT_LIFE_SPAN),
    ...
 ):

Reference: https://github.com/vimalloc/flask-jwt-extended/blob/5bd8b1ed08ea64d23869a629af3c3c868816b8a8/flask_jwt_extended/tokens.py#L34参考: https://github.com/vimalloc/flask-jwt-extended/blob/5bd8b1ed08ea64d23869a629af3c3c868816b8a8/flask_jwt_extended/tokens.py#L34

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM