-
Notifications
You must be signed in to change notification settings - Fork 809
Description
Describe the bug
I'm using next-auth on the frontend and the latest django-oauth-toolkit with OIDC enabled and HS256 as the algorithm on the backend. After authorizing my oauth application I get the error "failed to validate JWT signature" in next-auth.
The JWT token provided by django-oauth-toolkit seems to be incorrect. It's prepared in oauth2_provider.oauth2_validators.finalize_id_token
. The culprit is the jwk_key
that's used for signing the token. It's generated with the client secret, which is being hashed since v2.0.0
.
I'm assuming that hashing was added under the assumption that the raw secret isn't needed anymore after it's created, but that doesn't seem to be the case (see line 224).
To Reproduce
I've created a test repo: https://github.com/dnsv/auth-test
-
Setup Django (I'm using Python 3.11):
poetry install
poetry run python manage.py runserver
poetry run python manage.py migrate
poetry run python manage.py createsuperuser
.
-
Login into admin.
-
Create a new Oauth application:
- Redirect urls: http://localhost:3000/api/auth/callback/test
- Client type: Confidential
- Authorization grant type: Authorization code
- Algorithm: HMAC with SHA-2 256
-
Create the file
frontend/.env
:OAUTH_CLIENT_ID=... OAUTH_CLIENT_SECRET=...
-
Setup Next.js (I'm using node v18.12.1, but i probably works the same with lower versions).
cd frontend
yarn install
yarn dev
-
Go to http://localhost:3000/ and try to sign it. You'll get the error shown in the console after the authorization step.
Highlights from the backend setup:
# backend/base/settings.py
OAUTH2_PROVIDER = {
"OIDC_ENABLED": True,
"OAUTH2_VALIDATOR_CLASS": "backend.oauth2.utils.CustomOAuth2Validator",
"SCOPES": {
"openid": "OpenID Connect scope",
},
}
# backend/oauth2/utils.py
from oauth2_provider.oauth2_validators import OAuth2Validator
class CustomOAuth2Validator(OAuth2Validator):
oidc_claim_scope = None
def get_additional_claims(self, request):
return {
"id": request.user.id,
"given_name": request.user.first_name,
"family_name": request.user.last_name,
"name": f"{request.user.first_name} {request.user.last_name}",
"email": request.user.email,
}
Expected behavior
Being able to login :)
Version
- I have tested with the latest published release and it's still a problem.
- I have tested with the master branch and it's still a problem.
Additional context
N/A