|
| 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