在本地更新(Python Flask)后还原 Auth0 访问令牌授权



我希望将本地/dev Auth0功能恢复到我最近从Python 2.7更新到Python 3(v 3.8.6)的Flask应用程序。Auth0authorize_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 线程
  • 使用url_for('callback_handling', _external=True)确保与回调对齐
  • 验证 AUTHO 参数不需要键入为字节(u''转换是这些代码行中与 2.7 相比唯一顶级可见的变化)
  • 从 http://127.0.0.1:5000 运行(同一端口)

我注意到@lepture也注意到这个线程

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

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

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

  • 使用localhost:5000作为我的本地主机地址。我的映射flora.loc基础将不起作用。笔记:
    • 我仍然不确定为什么映射的主机名在这里不起作用(记录引用 url 显示flora.loc如预期的那样,但一定有一些关于我尚未捕获的解决方案)
    • 与往常一样,请确保您的预期地址也列在 Auth0 应用仪表板中。
  • 受众群体参数现在需要一个预先挂起的https://才能成功解析

最新更新