1

我想要一种使用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'
4

1 回答 1

0

解决了。

我查看了如下代码@require_login

if g.oidc_id_token is None:
    return self.redirect_to_auth_server(request.url)

( require_login_code )

所以我没走远...

我更新了我的装饰器以匹配他们的

def check_oidc(view_func):
    @wraps(view_func)
    def decorated(*args, **kwargs):
        if g_use_oidc is True and oidc.user_loggedin == False:
            return oidc.redirect_to_auth_server(request.url)
        return view_func(*args, **kwargs)
    return decorated

它使用oidc.redirect_to_auth_server的是从 1.0.0 开始弃用的。但它是内部使用的功能,@require_login所以它没有被弃用。

于 2021-12-22T10:19:35.290 回答