python - 使用 Authlib 0.11 的 JWT token 生成器

标签 python flask jwt authlib

首先:非常感谢 Authlib 创建者/其他开源创建者和支持者。

我希望 Authlib 0.11 以 JWT 形式返回 oauth token 。 我尝试按照 Authlib 网站中提供的文档使用 Authlib 0.11 https://docs.authlib.org/en/latest/flask/2/authorization-server.html#token 创建 JWT token 生成器.

由于我是这个主题的新手用户,我仍然无法找出将 JWT token 生成器方法传递给配置的正确方法:OAUTH2_ACCESS_TOKEN_GENERATOR

感谢任何帮助。

这是我的虚拟 jwt token 生成器:

from authlib.jose import jwt
def gen_access_token(client, grant_type, user, scope):
    log.debug('Not used yet in the JWT:: {} \n{} \n{} \n{}'.format( client, grant_type, user, scope))
    header = {'alg': 'RS256'}
    payload = {
        'iss': 'http://127.0.0.1:5000/oauth/token',
        'sub': 'test client',
        'aud': 'profile'
    }
    try:
        key = open('wf-app-server.key', 'r').read()
        s = jwt.encode(header, payload, key)
        claims = jwt.decode(s, open('wf-app-pub.pem', 'r').read())
    except Exception as e:
        log.debug('JWT exception', e)
    log.debug("jwt encoded:{}\n decoded :{} \n header:{}".format(
        s, claims, claims.header))
    return s

OAUTH2_REFRESH_TOKEN_GENERATOR = True
OAUTH2_TOKEN_EXPIRES_IN = {
    'authorization_code': 874000,
    'implicit': 3600,
    'password': 600000,
    'client_credentials': 600000
    }

OAUTH2_ACCESS_TOKEN_GENERATOR = gen_access_token('bCsNV2Lo8hxD593Km84lWM5d', 'client_credentials', 'admin', 'profile') 

-- 输出显示我的 JWT token 生成器正常工作并且返回的值可以正确解码 --

2019-06-22 13:37:38,024 DEBUG gen_access_token (7) Not used yet in the JWT:: bCsNV2Lo8hxD593Km84lWM5d  client_credentials  admin  profile

2019-06-22 13:37:38,052 DEBUG gen_access_token (21) jwt encoded:b'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwOi8vMTI3LjAuMC4xOjUwMDAvb2F1dGgvdG9rZW4iLCJzdWIiOiJ0ZXN0IGNsaWVudCIsImF1ZCI6InByb2ZpbGUifQ.BU5dSbPAFzoDDo4vathd6jlQVmDHaygEUh4GCwknCdbf4AVig3SgOW8JbITuPCKTf7qnxE8iJCWUOAd_wDCZwWKXdpisG6EGGmNpwZLAsDqL1CLgqTsRuGrc2kUfyMOHXfGXGkqsNROuPFV0-XYgxCQOz4LolNcB3Knvu1ApRcZyej8nAFXKxccDkLYyhldjRJwRehRZ4tMjDlbP4ghmEUFBF1Msx5Yzot26IK3ps4dfLnYVJr2dKUIPK75BzYR5kgUm3nkJRe4F0898j8tIMZwvKa2lKSypORDQXUxC3i8-x7A2vsVk7Jw3qcbZBarqstUEWITCZSVPYoHoF5l8iw'

decoded :{'iss': 'http://127.0.0.1:5000/oauth/token', 'sub': 'test  client', 'aud': 'profile'}   header:{'alg': 'RS256', 'typ': 'JWT'}

首先,为了测试我的 oauth token 请求凭据是否正确,我尝试从 Authlib 请求具有正确 client_credentials 和默认 token_generator 的 oauth token 。这样我就得到了默认的 oauth token 。

其次,我使用 token 生成器更新了配置,然后当我使用相同的客户端凭据请求 oauth token 时,出现以下错误:

2019-06-22 13:40:56,700 DEBUG authenticate_client_secret_basic (65)
Authenticate bCsNV2Lo8hxD593Km84lWM5d via "client_secret_basic"
success

I created this custom debug line below to understand what the default access_token_generator() takes as input parameters. It is exactly take the same types - my input parameter types also match!

2019-06-22 13:40:56,701 DEBUG validate_token_request (67)
Validate token request of <OAuth2Client 2> client: <OAuth2Client 2>
type:<class 'website.models.OAuth2Client'> grant_type:
client_credentials type:<class 'str'> user: None type:<class
'NoneType'> scope: rs1secret type:<class 'str'> 

2019-06-22 13:40:56,708 INFO _log (122) 127.0.0.1 - - [22/Jun/2019 13:40:56] "POST /oauth/token HTTP/1.1" 500 - Traceback (most recent call last):  File  "/home/pksec/.local/share/virtualenvs/oAuthProvider-n_KOMqPA/lib/python3.7/site-packages/flask/app.py",
  line 2328, in __call__
return self.wsgi_app(environ, start_response)   File "/home/pksec/.local/share/virtualenvs/oAuthProvider-n_KOMqPA/lib/python3.7/site-packages/flask/app.py",
  line 2314, in wsgi_app
     response = self.handle_exception(e)   File "/home/pksec/.local/share/virtualenvs/oAuthProvider-n_KOMqPA/lib/python3.7/site-packages/flask/app.py",
 line 1760, in handle_exception
     reraise(exc_type, exc_value, tb)   File "/home/pksec/.local/share/virtualenvs/oAuthProvider-n_KOMqPA/lib/python3.7/site-packages/flask/_compat.py",
 line 36, in reraise
     raise value   File "/home/pksec/.local/share/virtualenvs/oAuthProvider-n_KOMqPA/lib/python3.7/site-packages/flask/app.py",
 line 2311, in wsgi_app
     response = self.full_dispatch_request()   File "/home/pksec/.local/share/virtualenvs/oAuthProvider-n_KOMqPA/lib/python3.7/site-packages/flask/app.py",
 line 1834, in full_dispatch_request
     rv = self.handle_user_exception(e)   File "/home/pksec/.local/share/virtualenvs/oAuthProvider-n_KOMqPA/lib/python3.7/site-packages/flask/app.py",
 line 1737, in handle_user_exception
     reraise(exc_type, exc_value, tb)   File "/home/pksec/.local/share/virtualenvs/oAuthProvider-n_KOMqPA/lib/python3.7/site-packages/flask/_compat.py",
 line 36, in reraise
     raise value   File "/home/pksec/.local/share/virtualenvs/oAuthProvider-n_KOMqPA/lib/python3.7/site-packages/flask/app.py",
 line 1832, in full_dispatch_request
     rv = self.dispatch_request()   File "/home/pksec/.local/share/virtualenvs/oAuthProvider-n_KOMqPA/lib/python3.7/site-packages/flask/app.py",
 line 1818, in dispatch_request
     return self.view_functions[rule.endpoint](**req.view_args)   File "/home/pksec/xx/oAuthProvider/website/routes.py",
 line 193, in issue_token
     return authorization.create_token_response()   File "/virtualenvs/oAuthProvider-n_KOMqPA/lib/python3.7/site-packages/authlib/oauth2/rfc6749/authorization_server.py",
 line 186, in create_token_response
     args = grant.create_token_response()   File "/home/pksec/.local/share/virtualenvs/oAuthProvider-n_KOMqPA/lib/python3.7/site-packages/authlib/oauth2/rfc6749/grants/client_credentials.py",
 line 104, in create_token_response
     include_refresh_token=False,   File "/home/pksec/.local/share/virtualenvs/oAuthProvider-n_KOMqPA/lib/python3.7/site-packages/authlib/oauth2/rfc6749/grants/base.py",
 line 58, in generate_token
     include_refresh_token=include_refresh_token,   File "/home/pksec/.local/share/virtualenvs/oAuthProvider-n_KOMqPA/lib/python3.7/site-packages/authlib/oauth2/rfc6750/wrappers.py",
 line 91, in __call__
     access_token = self.access_token_generator(client, grant_type, user, scope) TypeError: 'NoneType' object is not callable

当我将 gen_acc_token() 方法传递给配置时,我知道我做错了什么 - 但无法准确找出问题所在。

传递示例 gen_JWT_access_token() 的小代码片段会很棒。

最佳答案

我终于找到了将 JWT token 生成器方法传递给配置的正确方法:OAUTH2_ACCESS_TOKEN_GENERATOR

这是我的虚拟 jwt token 生成器:

from authlib.jose import jwt
def gen_access_token(client, grant_type, user, scope):
    log.debug('Not used yet in the JWT:: {} \n{} \n{} \n{}'.format( client, grant_type, user, scope))
    header = {'alg': 'RS256'}
    payload = {
        'iss': 'http://127.0.0.1:5000/oauth/token',
        'sub': 'test client',
        'aud': 'profile'
    }
    try:
        key = open('wf-app-server.key', 'r').read()
        s = jwt.encode(header, payload, key)
        claims = jwt.decode(s, open('wf-app-pub.pem', 'r').read())
    except Exception as e:
        log.debug('JWT exception', e)
    log.debug("jwt encoded:{}\n decoded :{} \n header:{}".format(
        s, claims, claims.header))
    return s

OAUTH2_REFRESH_TOKEN_GENERATOR = True
OAUTH2_TOKEN_EXPIRES_IN = {
    'authorization_code': 874000,
    'implicit': 3600,
    'password': 600000,
    'client_credentials': 600000
    }
OAUTH2_ACCESS_TOKEN_GENERATOR = gen_access_token

不要传递函数参数:Python NoneType object is not callable (beginner)

这是初学者的错误!按照你的错误输出,你会找到解决方案!

这是您不应该传递生成器函数的方式:

OAUTH2_ACCESS_TOKEN_GENERATOR = gen_access_token('bCsNV2Lo8hxD593Km84lWM5d', 'client_credentials', 'admin', 'profile')

关于python - 使用 Authlib 0.11 的 JWT token 生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56715779/

相关文章:

python - 如何处理来自谷歌地图 api 的错误?

jquery - x-Editable 与 Flask 后端,如何发送数据

python - 用于 Python 变量的 Docker ENV

node.js - 登录成功后如何发送JWT到前端服务器存储到localStorage?

python - Pyperclip 复制导致操作系统错误

python - Mac OS 上未显示菜单项

python - Keras 中的自定义损失函数仅运行一次

python - 将 Flask 应用连接到 Websocket 服务器

javascript - AWS Cognito 正确的用户流程

c# - Identity Server 4 - 角色策略不起作用