Overview
Theoidc middleware provides OpenID Connect authentication, extending OAuth 2.0 with standardized identity tokens and user information endpoints.
Use it when you need:
- Enterprise SSO integration
- Standardized identity claims
- ID token validation
Installation
Quick Start
Configuration
Config
| Field | Type | Description |
|---|---|---|
Provider | *Provider | OIDC provider |
ClientID | string | Client ID |
ClientSecret | string | Client secret |
RedirectURL | string | Callback URL |
Scopes | []string | Requested scopes |
Examples
Google OIDC
Auth0
Keycloak
Token Validation Middleware
API Reference
Functions
Standard Claims
Technical Details
Token Verification Process
The OIDC middleware implements a comprehensive JWT token verification process:-
Token Extraction: Tokens are extracted from the
Authorizationheader using the Bearer scheme by default. A customTokenExtractorfunction can be provided for alternative extraction methods (e.g., from query parameters or cookies). - Token Structure Validation: The token is split into three parts (header, payload, signature) and validated for correct JWT format.
-
Header Decoding: The JWT header is decoded to extract the algorithm (
alg), key ID (kid), and token type (typ). - Claims Decoding: The payload is decoded into standard OIDC claims including issuer, subject, audience, expiration, and custom claims.
-
Validation Checks:
- Issuer Validation: Verifies the token was issued by the expected OIDC provider
- Audience Validation: Ensures the token is intended for this application (supports both string and array audiences)
- Expiration Check: Validates the token has not expired using the
expclaim - Not Before Check: Validates the token is valid using the
nbfclaim if present
-
Context Storage: Validated claims are stored in the request context for downstream handlers to access via
GetClaims().
Claims Structure
The middleware supports both standard OIDC claims and custom claims:- Standard Claims:
iss,sub,aud,exp,iat,nbf,email,name - Authorization Claims:
groups,roles,scope - Raw Claims: All claims are also stored in a
Rawmap for accessing custom provider-specific claims
Authorization Helpers
Three specialized middleware functions provide fine-grained access control:- RequireGroup: Validates the user belongs to a specific group (checks
groupsclaim) - RequireRole: Validates the user has a specific role (checks
rolesclaim) - RequireScope: Validates the token includes a specific OAuth scope (checks space-delimited
scopeclaim)
Error Handling
The middleware provides customizable error handling through theOnError callback:
- Default Behavior: Returns a 401 Unauthorized JSON response with error details
- Custom Handler: Can be configured to log errors, return custom responses, or redirect to login
Path Skipping
TheSkipPaths option allows certain routes to bypass authentication (e.g., health checks, public endpoints) while keeping the middleware globally applied.
Best Practices
- Use OIDC discovery (/.well-known/openid-configuration)
- Validate ID tokens for security
- Store only necessary claims in sessions
- Implement proper logout with end_session_endpoint
- Configure appropriate
RefreshIntervalfor JWKS key rotation - Use
SkipPathsfor public endpoints instead of conditional middleware application - Implement custom
OnErrorhandlers for security-sensitive logging - Leverage
RequireGroup,RequireRole, andRequireScopefor authorization
Testing
The OIDC middleware includes comprehensive test coverage for various scenarios:| Test Case | Description | Expected Behavior |
|---|---|---|
TestNew | Basic middleware initialization without token | Returns 401 Unauthorized |
TestWithValidToken | Valid token with correct issuer, audience, and expiration | Returns 200 OK, claims accessible via GetClaims |
TestExpiredToken | Token with expiration time in the past | Returns 401 Unauthorized |
TestInvalidIssuer | Token with incorrect issuer claim | Returns 401 Unauthorized |
TestInvalidAudience | Token with incorrect audience claim | Returns 401 Unauthorized |
TestSkipPaths | Configured paths that bypass authentication | Skipped paths return 200 OK without token, protected paths require token |
TestCustomTokenExtractor | Custom token extraction from query parameters | Successfully authenticates using token from query string |
TestCustomOnError | Custom error handler configuration | Returns custom error response (403 Forbidden) |
TestClaimsHasAudience | String and array audience validation | Correctly identifies audience in both string and array formats |
TestClaimsHasGroup | Group membership validation | Correctly identifies user group membership |
TestClaimsHasRole | Role membership validation | Correctly identifies user role membership |
TestRequireGroup | Group-based authorization middleware | Allows access with required group, denies without (403 Forbidden) |
TestRequireScope | Scope-based authorization middleware | Allows access with required scope, denies without (403 Forbidden) |
TestInvalidToken | Malformed token format | Returns 401 Unauthorized |