Skip to content

Commit 270f06d

Browse files
committed
📝(oidc) add "how-to" documentation about backends
This provides a very simple documentation about how to integrate the backends in a project. This is surely not enough.
1 parent e8a56a2 commit 270f06d

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Using the OIDC Authentication Backend
2+
3+
This guide explains how to integrate and configure the `OIDCAuthenticationBackend` in your Django project for OpenID Connect (OIDC) authentication.
4+
5+
## Installation
6+
7+
1. Ensure you have the necessary packages installed:
8+
9+
```bash
10+
pip install django-lasuite
11+
```
12+
13+
## Configuration
14+
15+
### Settings
16+
17+
Add the following to your Django settings:
18+
19+
```python
20+
# Add the authentication backend
21+
AUTHENTICATION_BACKENDS = [
22+
'lasuite.oidc_login.backends.OIDCAuthenticationBackend',
23+
]
24+
25+
# Required OIDC settings
26+
OIDC_RP_CLIENT_ID = "your-client-id"
27+
OIDC_RP_CLIENT_SECRET = "your-client-secret"
28+
OIDC_OP_TOKEN_ENDPOINT = "https://your-provider.com/token"
29+
OIDC_OP_USER_ENDPOINT = "https://your-provider.com/userinfo"
30+
OIDC_OP_LOGOUT_ENDPOINT = "https://your-provider.com/logout"
31+
32+
# Optional settings
33+
OIDC_USER_SUB_FIELD = "sub" # Field to store the OIDC subject identifier, defaults to "sub"
34+
USER_OIDC_FIELDS_TO_FULLNAME = ["first_name", "last_name"] # Fields used to compute user's full name
35+
OIDC_FALLBACK_TO_EMAIL_FOR_IDENTIFICATION = True # Allow fallback to email for user identification
36+
OIDC_CREATE_USER = True # Automatically create users if they don't exist
37+
ALLOW_LOGOUT_GET_METHOD = True # Allow GET method for logout
38+
```
39+
40+
### URLs
41+
42+
Include the OIDC URLs in your project's `urls.py`:
43+
44+
```python
45+
from django.urls import include, path
46+
47+
urlpatterns = [
48+
# Your other URLs
49+
path('', include('lasuite.oidc_login.urls')),
50+
]
51+
```
52+
53+
## User Model Requirements
54+
55+
Your User model should include the following fields:
56+
57+
1. `sub` - To store the OIDC subject identifier, you may replace this with
58+
another field if needed but needs to set the `OIDC_USER_SUB_FIELD` setting
59+
2. `email` - For user identification (especially if fallback to email is enabled)
60+
3. `name` - To store user's full name (computed from fields defined in `USER_OIDC_FIELDS_TO_FULLNAME`)
61+
62+
## Authentication Flow
63+
64+
1. User is redirected to the OIDC provider login page
65+
2. After successful authentication, the provider redirects back to your app
66+
3. The backend verifies the authentication and:
67+
- Retrieves an existing user based on the `sub` field or falls back to email
68+
- Creates a new user if no match is found (when `OIDC_CREATE_USER=True`)
69+
- Updates user information if needed
70+
4. User is now authenticated in your application
71+
72+
## Logout Functionality
73+
74+
The package includes custom logout views that will properly sign the user out from both your application and the OIDC provider.
75+
76+
## Customization
77+
78+
To customize the behavior of the OIDC authentication backend, you can create your own subclass:
79+
80+
```python
81+
from lasuite.oidc_login.backends import OIDCAuthenticationBackend
82+
83+
84+
class CustomOIDCAuthenticationBackend(OIDCAuthenticationBackend):
85+
def get_extra_claims(self, user_info):
86+
# Add custom claims processing
87+
claims = super().get_extra_claims(user_info)
88+
claims['custom_field'] = user_info.get('custom_field')
89+
return claims
90+
91+
def post_get_or_create_user(self, user, claims):
92+
# Add custom post-processing
93+
super().post_get_or_create_user(user, claims)
94+
# Additional processing...
95+
```
96+
97+
Then update your `AUTHENTICATION_BACKENDS` setting to use your custom class.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Using the OIDC Resource Server Backend
2+
3+
This guide explains how to integrate and configure the `ResourceServerBackend` in your Django project for secure API access using OpenID Connect (OIDC) token introspection.
4+
5+
## Overview
6+
7+
The `ResourceServerBackend` allows your application to act as an OAuth 2.0 resource server, validating access tokens through introspection with an authorization server. This enables secure API access control using OIDC standards.
8+
9+
## Installation
10+
11+
1. Ensure you have the necessary packages installed:
12+
13+
```bash
14+
pip install django-lasuite
15+
```
16+
17+
## Configuration
18+
19+
### Settings
20+
21+
Add the following to your Django settings:
22+
23+
```python
24+
# Resource Server Backend
25+
OIDC_RS_BACKEND_CLASS = "lasuite.oidc_resource_server.backend.ResourceServerBackend"
26+
27+
# Resource Server Configuration
28+
OIDC_RS_AUDIENCE_CLAIM = "client_id" # The claim used to identify the audience
29+
OIDC_RS_ENCRYPTION_ENCODING = "A256GCM" # Encryption encoding algorithm
30+
OIDC_RS_ENCRYPTION_ALGO = "RSA-OAEP" # Encryption algorithm
31+
OIDC_RS_SIGNING_ALGO = "ES256" # Signing algorithm
32+
OIDC_RS_SCOPES = ["groups"] # Required scopes for authentication
33+
34+
# Private key for encryption/decryption
35+
OIDC_RS_PRIVATE_KEY_STR = """-----BEGIN PRIVATE KEY-----
36+
YOUR_PRIVATE_KEY_HERE
37+
-----END PRIVATE KEY-----"""
38+
OIDC_RS_ENCRYPTION_KEY_TYPE = "RSA" # Key type (RSA, EC, etc.)
39+
40+
# Client credentials
41+
OIDC_RP_CLIENT_ID = "your-client-id"
42+
OIDC_RP_CLIENT_SECRET = "your-client-secret"
43+
44+
# Authorization server endpoints
45+
OIDC_OP_TOKEN_ENDPOINT = "https://your-provider.com/token"
46+
OIDC_OP_USER_ENDPOINT = "https://your-provider.com/userinfo"
47+
```
48+
49+
### URLs Configuration
50+
51+
Include the OIDC Resource Server URLs in your project's `urls.py`:
52+
53+
```python
54+
from django.urls import include, path
55+
56+
urlpatterns = [
57+
# Your other URLs
58+
path('', include('lasuite.oidc_resource_server.urls')),
59+
]
60+
```
61+
62+
This will expose the JWKS endpoint (`/jwks`) which provides the public key used for token verification.
63+
64+
## Usage in Views
65+
66+
To secure your API views, use the authorization backend with Django REST Framework:
67+
68+
```python
69+
from rest_framework.permissions import IsAuthenticated
70+
from rest_framework.response import Response
71+
from rest_framework.views import APIView
72+
73+
from lasuite.oidc_resource_server.authentication import ResourceServerAuthentication
74+
75+
class SecureAPIView(APIView):
76+
authentication_classes = [ResourceServerAuthentication]
77+
permission_classes = [IsAuthenticated]
78+
79+
def get(self, request):
80+
# Your secure view logic here
81+
return Response({"message": "Authenticated access"})
82+
```
83+
84+
## Token Verification Flow
85+
86+
1. Client sends request with access token in Authorization header
87+
2. `ResourceServerBackend` intercepts the request
88+
3. Backend sends token to authorization server for introspection
89+
4. Backend validates returned claims (issuer, audience, etc.)
90+
5. If valid, request is processed; otherwise, authentication fails
91+
92+
## Advanced: JWT Resource Server
93+
94+
For JWT-based introspection (RFC 9701), use the `JWTResourceServerBackend`:
95+
96+
```python
97+
OIDC_RS_BACKEND_CLASS = "lasuite.oidc_resource_server.backend.JWTResourceServerBackend"
98+
```
99+
100+
This implementation handles JWT format introspection responses that are signed and encrypted, providing an additional layer of security.
101+
102+
## Key Management
103+
104+
The resource server requires a key pair:
105+
- The private key is used for decryption and stored securely in your settings
106+
- The public key is exposed via the JWKS endpoint for the authorization server
107+
108+
Generate a suitable RSA key, like using OpenSSL:
109+
110+
```bash
111+
openssl genrsa -out private_key.pem 2048
112+
```

0 commit comments

Comments
 (0)