I was running into an issue when setting the custom admin role in Rules. I had set it all up perfectly in code and in the Rules section in my Auth0 Dashboard. When I tried logging in to an account that I already had pre-created, I got this error:
Exception Type: KeyError
Exception Value:‘https://www.django-webapp.com/role’
This happens because we don’t populate the key value with the role string when the user has no email or the user’s email is not verified. So, to make sure we don’t run into this issue, I added this check into my ./src/webapp/authentication/auth0.py:
…
audience = self.setting(‘KEY’) # CLIENT_ID
payload = jwt.decode(id_token, jwks.read(), algorithms=[‘RS256’], audience=audience, issuer=issuer)
email_verified = payload[‘email_verified’]
new_role = ‘user’
if email_verified is True:
new_role = payload[‘https://django-webapp/role’]
return {
‘username’: payload[‘nickname’],
‘first_name’: payload[‘name’],
‘picture’: payload[‘picture’],
‘user_id’: payload[‘sub’],
‘role’: new_role,
}
1 Like