【问题标题】:Restore Auth0 access token authorization after update (Python Flask) on local在本地更新(Python Flask)后恢复 Auth0 访问令牌授权
【发布时间】:2021-03-26 15:24:40
【问题描述】:

我希望将本地/开发 Auth0 功能恢复到我最近从 Python 2.7 更新到 Python 3 (v 3.8.6) 的 Flask 应用程序。 Auth0 authorize_access_token 现在在我的本地开发服务器上失败,但仍然可以在部署的临时站点上运行。我没有对此代码或 Auth0 我的设置进行任何更改。

错误信息:

  File "/Users/h/.local/share/virtualenvs/stf-hashhere/lib/python3.8/site-packages/authlib/integrations/base_client/base_app.py", line 126, in _retrieve_oauth2_access_token_params
    raise MismatchingStateError()
authlib.integrations.base_client.errors.MismatchingStateError: mismatching_state: CSRF Warning! State not equal in request and response.

代码:

def create_app(test_config=None):
# Factory to create and configure the app
app = Flask(
    __name__,
    static_folder='../www/static',
    static_url_path='/static',
    template_folder='../www/static/dist',
    instance_relative_config=True,
)

oauth = OAuth(app) 
app.secret_key = app.config['SESSION_KEY']
auth0_base = 'https://{}'.format(app.config['AUTH0_API_AUDIENCE'])
auth0 = oauth.register(
    'auth0',
    client_id=app.config['AUTH0_CLIENT_ID'],
    client_secret=app.config['AUTH0_CLIENT_SECRET'],
    api_base_url=auth0_base,
    access_token_url='{}/oauth/token'.format(auth0_base),
    authorize_url='{}/authorize'.format(auth0_base),
    client_kwargs={
        'scope': 'openid profile email',
    },
)

@app.route('/earlybird')
def login():
    return auth0.authorize_redirect(redirect_uri=app.config['AUTH0_CALLBACK_URL'])

@app.route('/auth/callback')
def callback_handling():
    auth0.authorize_access_token()
    return redirect('/profile')


{'framework': <authlib.integrations.flask_client.integration.FlaskIntegration object at 0x110be03a0>, 'name': 'auth0', 'client_id': '<client>', 'client_secret': 'secret', 'request_token_url': None, 'request_token_params': None, 'access_token_url': 'https://smalltradeflora.auth0.com/oauth/token', 'access_token_params': None, 'authorize_url': 'https://smalltradeflora.auth0.com/authorize', 'authorize_params': None, 'api_base_url': 'https://smalltradeflora.auth0.com', 'client_kwargs': {'scope': 'openid profile email'}, 'compliance_fix': None, 'client_auth_methods': None, '_fetch_token': None, '_update_token': None, '_user_agent': 'Authlib/0.15.2 (+https://authlib.org/)', '_server_metadata_url': None, 'server_metadata': {'refresh_token_url': None, 'refresh_token_params': None}, '_fetch_request_token': None, '_save_request_token': None}
  • http://flora.loc:5000/auth/callback是我的Allowed Callback URL,也是我的app.config['AUTH0_CALLBACK_URL']

我试过了:

  • 验证配置变量
  • 添加一个SESSION_NAME 然后app.config.SESSION_COOKIE_NAME 尝试按照这个SO thread
  • 使用url_for('callback_handling', _external=True) 确保与回调对齐
  • 验证 AUTHO 参数不需要以字节形式输入(u'' 转换是这些代码行中 2.7 的唯一顶级可见更改)
  • http://127.0.0.1:5000(相同端口)运行

我注意到@lepture 在这个thread 中也有注释

在 Authlib 0.9 中,状态的会话密钥已更改。

但我还不明白这如何或是否适用于我需要的代码调整。

【问题讨论】:

  • 深入authlib代码时,请求状态存在,但框架状态为None

标签: flask oauth auth0


【解决方案1】:

我最终完全重写了我的应用程序工厂,从下载的 Oauth for Python Web 应用程序示例迭代构建。代码的工作版本和非工作版本之间的两个主要区别是:

  • 使用localhost:5000 作为我的本地主机地址。我映射的flora.loc 基地将无法工作。笔记:
    • 我仍然不确定为什么映射的主机名在这里不起作用(记录引用的 url 显示 flora.loc 符合预期,但肯定有一些关于我还没有捕捉到的分辨率)
    • 与往常一样,确保您的预期地址也列在您的 auth0 应用仪表板中。
  • Audience 参数现在需要预先添加 https:// 才能成功解析

【讨论】:

    猜你喜欢
    • 2018-11-24
    • 2020-05-29
    • 2020-03-28
    • 1970-01-01
    • 2021-02-23
    • 2019-07-22
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多