我正在尝试从 https://github.com/lepture/flask-oauthlib/tree/master/tests/oauth2/server.py代码库在 Python 中实现 Oauth2 提供程序服务器,但在尝试执行一些重定向 uri 验证自定义时卡住了对于 Oauth2 授权(类型=授权代码)流程。
根据 Oauth2.py,我应该能够添加一个名为 validate_redirect_uri() 的函数来进行我自己的自定义 redirect_uri 验证。但是,在 Grant 类中实现“def validate_redirect_uri(self,redirect_uri):”后,发现当我使用“response_type=code&client_id=confidential&redirect_uri=https%3A%2F%2fXXX.com”等参数发布到 /oauth/authorize 时,它从未被调用过%2Foauth%2Fcallback%3FXXX%3D111&state=11"
我确信该帖子很好(范围已放在 POST 正文中,因此它没有丢失),因为如果我只将 redirect_uri 更改为未自定义的,它就可以了。
在 Server.py 中:
@oauth.grantgetter
def get_grant(client_id, code):
return Grant.query.filter_by(client_id=client_id, code=code).first()
授予类(db.Model):
def validate_redirect_uri(self, redirect_uri):
print('validate_redirect_uri:', redirect_uri,'\n')
if customizedValidateRedirectURI(redirect_uri)!=-1:
print('valid redirect uri')
return True
return False
def delete(self):
db.session.delete(self)
db.session.commit()
return self
在 oauth2.py 中
def confirm_redirect_uri(self, client_id, code, redirect_uri, client,
*args, **kwargs):
"""Ensure client is authorized to redirect to the redirect_uri.
This method is used in the authorization code grant flow. It will
compare redirect_uri and the one in grant token strictly, you can
add a `validate_redirect_uri` function on grant for a customized
validation.
"""
client = client or self._clientgetter(client_id)
log.debug('Confirm redirect uri for client %r and code %r.',
client.client_id, code)
grant = self._grantgetter(client_id=client.client_id, code=code)
if not grant:
log.debug('Grant not found.')
return False
if hasattr(grant, 'validate_redirect_uri'):
return grant.validate_redirect_uri(redirect_uri)
“print('validate_redirect_uri:', redirect_uri,'\n')” 从未被打印,所以函数“validate_redirect_uri(self, redirect_uri)” 从未被调用?