我想要一种使用flask-oidc激活和停用 OIDC 检查的方法。
使用@require_login
装饰器是个问题,因为它不能被停用。例如 :
g_use_oidc = True # <= A global boolean to activate or deactivate OIDC at application level
@app.route('/') # <= Flask routing
@require_login # <= Flask-OIDC making redirection to oAuth provider
def home():
return render_template('index.html')
当我被重定向到我的 oAUth 提供商时,这与 OIDC 配合得很好。但现在我想g_use_oidc
设置为 False 并@require_login
停止重定向呼叫。
为了实现这一点,我创建了一个装饰器来检查如下g_use_oidc
状态:
def check_oidc_test(func):
@wraps()
def inner():
if not use_oidc or oidc.user_loggedin == True:
func()
elif use_oidc and oidc.user_loggedin == False:
return oidc.redirect_to_auth_server(None, request.values) # <= Redirection to OIDC provider as shown in the doc
return inner
我应该能够像这样使用装饰器:
@app.route('/')
@check_oidc # <= my new decorator
def home():
return render_template('index.html')
不幸的是,我从 OIDC 得到了这个堆栈跟踪:
Traceback (most recent call last):
File "x.local/lib/python3.8/site-packages/flask/app.py", line 2088, in __call__
return self.wsgi_app(environ, start_response)
File "x.local/lib/python3.8/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.handle_exception(e)
File "x.local/lib/python3.8/site-packages/flask/app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "x.local/lib/python3.8/site-packages/flask/app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "x.local/lib/python3.8/site-packages/flask/app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "x.local/lib/python3.8/site-packages/flask/app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "x.local/lib/python3.8/site-packages/flask_oidc/__init__.py", line 657, in _oidc_callback
plainreturn, data = self._process_callback('destination')
File "x.local/lib/python3.8/site-packages/flask_oidc/__init__.py", line 707, in _process_callback
response = self.extra_data_serializer.loads(state[statefield])
KeyError: 'destination'