Generic OAuth2 + JWT Authentication
This backend allows users to authenticate against any Identity Provider using OAuth 2.0 and JWT tokens.
How it Works
- Authorization Redirect: When a user goes to
/oauth2/login/, they are redirected to the Identity Provider authorization endpoint. - Authorization Code Grant: After successful login at the Identity Provider, the user is redirected back to the callback view (
/oauth2/callback/) with an authorization code. - JWT Access Token Exchange: The callback view sends a POST request to the Provider token endpoint to exchange the authorization code for a JWT access token.
- Token Decoding: The custom
staffOAuth2JWTBackenddecodes the JWT payload. - By default, it decodes with
verify_signature=False. - Alternatively, it supports signature verification (like RS256) if
JWT_OAUTH2_VERIFY_SIGNATUREis set toTrue. - User Mapping: The backend updates user fields and roles in
configure_user: - Synchronizes first name, last name, and email based on claims list.
- Determines
is_staffusingJWT_OAUTH2_STAFF_ATTRIBUTES. - Determines
is_superuserif thesuperuserclaim isTrue. - Checks access restrictions via
JWT_OAUTH2_REQUIRED_ACCESS_ATTRIBUTES.
Configuration
To enable generic OAuth2 + JWT authentication:
- Register the OAuth Application in your Identity Provider and retrieve the Client ID and Client Secret. Configure the Redirect URI:
-
Redirect URI:
https://<your-portal-domain>/oauth2/callback/ -
Enable the application and backend in
userportal/settings/43-oauth2jwt.pyby uncommenting the setup lines:
JWT_OAUTH2_AUTH_ENABLED = True
AUTHENTICATION_BACKENDS = ['userportal.authentication.staffOAuth2JWTBackend'] + AUTHENTICATION_BACKENDS
LOGIN_URL = '/oauth2/login/'
- Configure your Credentials and Endpoints in
userportal/settings/43-oauth2jwt.py:
JWT_OAUTH2_PROVIDER_URL = 'https://idp.example.com'
JWT_OAUTH2_CLIENT_ID = 'your-client-id'
JWT_OAUTH2_CLIENT_SECRET = 'your-client-secret'
You can also specify JWT_OAUTH2_AUTHORIZATION_URL and JWT_OAUTH2_TOKEN_URL explicitly, which by default have the value of JWT_OAUTH2_PROVIDER_URL/oauth2/authorize/ and JWT_OAUTH2_PROVIDER_URL/oauth2/access_token/ respectively.
If needed, you can specify extra token parameters with JWT_OAUTH2_EXTRA_TOKEN_PARAMS. This allows to pass additional information to the OAUTH2 endpoint. For OpenEdX, define
JWT_OAUTH2_EXTRA_TOKEN_PARAMS = {'token_type': 'jwt'}
- Map access and staff roles based on the claims structure:
# Only allow users with specific claims to log in
JWT_OAUTH2_REQUIRED_ACCESS_ATTRIBUTES = []
# Automatically set user.is_staff based on claim values
JWT_OAUTH2_STAFF_ATTRIBUTES = [
('administrator', True)
]