|
| 1 | +# Auth0 API Python Examples |
| 2 | + |
| 3 | +This document provides examples for using the `auth0-api-python` package to validate Auth0 tokens in your API. |
| 4 | + |
| 5 | +## Bearer Authentication |
| 6 | + |
| 7 | +Bearer authentication is the standard OAuth 2.0 token authentication method. |
| 8 | + |
| 9 | +### Using verify_access_token |
| 10 | + |
| 11 | +```python |
| 12 | +import asyncio |
| 13 | +from auth0_api_python import ApiClient, ApiClientOptions |
| 14 | + |
| 15 | +async def validate_bearer_token(headers): |
| 16 | + api_client = ApiClient(ApiClientOptions( |
| 17 | + domain="your-tenant.auth0.com", |
| 18 | + audience="https://api.example.com" |
| 19 | + )) |
| 20 | + |
| 21 | + try: |
| 22 | + # Extract the token from the Authorization header |
| 23 | + auth_header = headers.get("authorization", "") |
| 24 | + if not auth_header.startswith("Bearer "): |
| 25 | + return {"error": "Missing or invalid authorization header"}, 401 |
| 26 | + |
| 27 | + token = auth_header.split(" ")[1] |
| 28 | + |
| 29 | + # Verify the access token |
| 30 | + claims = await api_client.verify_access_token(token) |
| 31 | + return {"success": True, "user": claims["sub"]} |
| 32 | + except Exception as e: |
| 33 | + return {"error": str(e)}, getattr(e, "get_status_code", lambda: 401)() |
| 34 | + |
| 35 | +# Example usage |
| 36 | +headers = {"authorization": "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9..."} |
| 37 | +result = asyncio.run(validate_bearer_token(headers)) |
| 38 | +``` |
| 39 | + |
| 40 | +### Using verify_request |
| 41 | + |
| 42 | +```python |
| 43 | +import asyncio |
| 44 | +from auth0_api_python import ApiClient, ApiClientOptions |
| 45 | +from auth0_api_python.errors import BaseAuthError |
| 46 | + |
| 47 | +async def validate_request(headers): |
| 48 | + api_client = ApiClient(ApiClientOptions( |
| 49 | + domain="your-tenant.auth0.com", |
| 50 | + audience="https://api.example.com" |
| 51 | + )) |
| 52 | + |
| 53 | + try: |
| 54 | + # Verify the request with Bearer token |
| 55 | + claims = await api_client.verify_request( |
| 56 | + headers=headers |
| 57 | + ) |
| 58 | + return {"success": True, "user": claims["sub"]} |
| 59 | + except BaseAuthError as e: |
| 60 | + return {"error": str(e)}, e.get_status_code(), e.get_headers() |
| 61 | + |
| 62 | +# Example usage |
| 63 | +headers = {"authorization": "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9..."} |
| 64 | +result = asyncio.run(validate_request(headers)) |
| 65 | +``` |
| 66 | + |
| 67 | + |
| 68 | +## DPoP Authentication |
| 69 | + |
| 70 | +[DPoP](https://www.rfc-editor.org/rfc/rfc9449.html) (Demonstrating Proof of Posession) is an application-level mechanism for sender-constraining OAuth 2.0 access and refresh tokens by proving that the client application is in possession of a certain private key. |
| 71 | + |
| 72 | +This guide covers the DPoP implementation in `auth0-api-python` with complete examples for both operational modes. |
| 73 | + |
| 74 | +For more information about DPoP specification, see [RFC 9449](https://tools.ietf.org/html/rfc9449). |
| 75 | + |
| 76 | +## Configuration Modes |
| 77 | + |
| 78 | +### 1. Allowed Mode (Default) |
| 79 | +```python |
| 80 | +from auth0_api_python import ApiClient, ApiClientOptions |
| 81 | + |
| 82 | +api_client = ApiClient(ApiClientOptions( |
| 83 | + domain="your-tenant.auth0.com", |
| 84 | + audience="https://api.example.com", |
| 85 | + dpop_enabled=True, # Default: enables DPoP support |
| 86 | + dpop_required=False # Default: allows both Bearer and DPoP |
| 87 | +)) |
| 88 | +``` |
| 89 | + |
| 90 | +### 2. Required Mode |
| 91 | +```python |
| 92 | +api_client = ApiClient(ApiClientOptions( |
| 93 | + domain="your-tenant.auth0.com", |
| 94 | + audience="https://api.example.com", |
| 95 | + dpop_required=True # Enforces DPoP-only authentication |
| 96 | +)) |
| 97 | +``` |
| 98 | + |
| 99 | +## Getting Started |
| 100 | + |
| 101 | +### Basic Usage with verify_request() |
| 102 | + |
| 103 | +The `verify_request()` method automatically detects the authentication scheme: |
| 104 | + |
| 105 | +```python |
| 106 | +import asyncio |
| 107 | +from auth0_api_python import ApiClient, ApiClientOptions |
| 108 | + |
| 109 | +async def handle_api_request(headers, http_method, http_url): |
| 110 | + api_client = ApiClient(ApiClientOptions( |
| 111 | + domain="your-tenant.auth0.com", |
| 112 | + audience="https://api.example.com" |
| 113 | + )) |
| 114 | + |
| 115 | + try: |
| 116 | + # Automatically handles both Bearer and DPoP schemes |
| 117 | + claims = await api_client.verify_request( |
| 118 | + headers=headers, |
| 119 | + http_method=http_method, |
| 120 | + http_url=http_url |
| 121 | + ) |
| 122 | + return {"success": True, "user": claims["sub"]} |
| 123 | + except Exception as e: |
| 124 | + return {"error": str(e)}, e.get_status_code() |
| 125 | + |
| 126 | +# Example usage |
| 127 | +headers = { |
| 128 | + "authorization": "DPoP eyJ0eXAiOiJKV1Q...", |
| 129 | + "dpop": "eyJ0eXAiOiJkcG9wK2p3dC..." |
| 130 | +} |
| 131 | +result = asyncio.run(handle_api_request(headers, "GET", "https://api.example.com/data")) |
| 132 | +``` |
| 133 | + |
| 134 | +### Direct DPoP Proof Verification |
| 135 | + |
| 136 | +For more control, use `verify_dpop_proof()` directly: |
| 137 | + |
| 138 | +```python |
| 139 | +async def verify_dpop_token(access_token, dpop_proof, http_method, http_url): |
| 140 | + api_client = ApiClient(ApiClientOptions( |
| 141 | + domain="your-tenant.auth0.com", |
| 142 | + audience="https://api.example.com" |
| 143 | + )) |
| 144 | + |
| 145 | + # First verify the access token |
| 146 | + token_claims = await api_client.verify_access_token(access_token) |
| 147 | + |
| 148 | + # Then verify the DPoP proof |
| 149 | + proof_claims = await api_client.verify_dpop_proof( |
| 150 | + access_token=access_token, |
| 151 | + proof=dpop_proof, |
| 152 | + http_method=http_method, |
| 153 | + http_url=http_url |
| 154 | + ) |
| 155 | + |
| 156 | + return { |
| 157 | + "token_claims": token_claims, |
| 158 | + "proof_claims": proof_claims |
| 159 | + } |
| 160 | +``` |
0 commit comments