authlib 0.9: MismatchingStateError: mismatching_state
We just discovered that our login page (using Auth0) was broken because of the latest authlib 0.9. We're using Flask 0.12.2. Reverting authlib to 0.8 solves the problem. Our requirements.txt is set to use the latest version of authlib.
The following error message was reported in our logs:
File "<DELETED_PATH>/lib/authlib/flask/client/oauth.py", line 248, in authorize_access_token
params = _generate_oauth2_access_token_params(self.name)
File "<DELETED_PATH>/lib/authlib/flask/client/oauth.py", line 270, in _generate_oauth2_access_token_params
raise MismatchingStateError()
MismatchingStateError: mismatching_state: CSRF Warning! State not equal in request and response.
Authlib has this covered in test case. In Authlib 0.9 the session key for state has changed. If you are using Authlib correctly, there wouldn’t be an error. I have no context on how you are using it, in this case I can’t provide a solution for you.
I followed a tutorial from Auth0 [1] when implementing our login feature, so maybe this is the wrong forum to address this issue. I'm going to open an issue at Auth0 instead.
[1] https://auth0.com/docs/quickstart/webapp/python/01-login
I read through our code and noticed that we had applied a work-around to get Auth0 to work:
# The authlib library doesn't work out-of-the-box with Auth0 Lock, so we need to force some of its session keys, e.g.
# - "_auth_state_": holds a random state parameter used to mitigate CSRF attacks
# - "_auth_callback": holds callback url for Auth0
So I guess that work-around is the culprit.
@roku6185 But why do you need a work around to make it work? Can you share some inside information with me so that I can make Authlib work out of the box.
We're not using the authorize_redirect() provided by authlib, but instead we're using Auth0's Web Lock feature [1], which is a javascript module to embed a login form. The javascript redirects to our own callback:
from authlib.flask.client import OAuth
app = Flask(__name__)
oauth = OAuth(app)
@app.route('/login/callback', methods=['GET'], strict_slashes=False)
def login_callback():
auth0 = oauth.register(
'auth0',
client_id = '<AUTH0_CLIENT_ID>',
client_secret = '<AUTH0_CLIENT_SECRET>',
api_base_url = '<AUTH0_DOMAIN>',
access_token_url = '<AUTH0_DOMAIN>/oauth/token',
authorize_url = '<AUTH0_DOMAIN>/authorize',
client_kwargs = {
'scope': 'openid',
},
)
response = auth0.authorize_access_token()
token = response['access_token']
expires_at = response['expires_at']
# Store token etc.
...
Since we're not calling authorize_redirect() authlib's session keys are not set (i.e. in 0.8: _auth0_state_ and _auth0_callback_), so we need to set them explicitly before rendering our login page. We basically create our own state variable and pass that to Auth0's javascript module and at the same time we set the session key for authlib:
import uuid
def prepare_login():
session['_auth0_state_'] = uuid.uuid4().hex
session['_auth0_callback_'] = '<AUTH0_CALLBACK>'
[1] https://auth0.com/docs/libraries/lock
@roku6185 Added a save_authorize_state method in https://github.com/lepture/authlib/commit/9e3fc71bad6569c2b7755874cea9b6560fc872c9
Awesome! Thanks for the response. I'm looking forward for the next release of authlib. I'm also thinking about doing a pull request to Auth0's sample repository [1] to provide a working example with Auth0's Web Lock and authlib.
[1] https://github.com/auth0-samples/auth0-python-web-app
Hi guys, I just bumped into this exact issue : -)
@roku6185 did you ever managed to get it working with the save_authorize_state method on v0.9 or did you stay on 0.8 for now? I tried installing master (which includes 9e3fc71) but I still have the exact same error message when calling auth0.authorize_access_token().
@pawelad I'm still using 0.8 with my work-around, i.e. setting state variables in the session. I'm not that familiar with PIP (and requirements.txt) to be able to install something that isn't officially released. According to the changelog [1] save_authorize_state will be available in v0.10.
[1] https://github.com/lepture/authlib/blob/master/docs/changelog.rst
OK, I think I got it to work - thanks for the help @roku6185 !
I then bumped into #94 and decided to try and use v0.8, but for future reference, this works:
@auth.route('/login')
def login():
auth0 = app.config['auth0']
# This is needed because we're using Auth0 lock.js library
# See: https://github.com/lepture/authlib/issues/81
state = uuid.uuid4().hex
redirect_url = url_for("auth.callback", _external=True)
auth0.save_authorize_state(redirect_uri=redirect_url, state=state)
return render_template('login.html')
@auth.route('/callback')
def callback():
auth0 = app.config['auth0']
token = auth0.authorize_access_token()
...
return redirect('/')
plus the Lock.js side:
auth: {
redirectUrl: '{{ session._auth0_callback_ }}',
params: {
state: '{{ session._auth0_state_ }}',
scope: 'openid email',
},
},