OAuth42 Rust SDK (o42sdk)
Official Rust SDK for OAuth42, providing comprehensive OAuth2/OIDC client functionality with framework integrations for Actix-web and Axum.
Features
- ✅ OAuth2 Authorization Code Flow with PKCE
- ✅ Token management and automatic refresh
- ✅ OIDC Discovery support
- ✅ Session management with pluggable stores
- ✅ Framework middleware for Actix-web and Axum
- ✅ Type-safe API with comprehensive error handling
- ✅ Example applications demonstrating best practices
Installation
Add to your Cargo.toml:
[]
= { = "0.1", = ["actix"] } # For Actix-web
# or
= { = "0.1", = ["axum"] } # For Axum
Quick Start
Basic Usage
use ;
async
Actix-web Integration
use ;
use ;
use Arc;
async
async
Axum Integration
use ;
use ;
use Arc;
async
async
Configuration
Environment Variables
OAUTH42_CLIENT_ID=your-client-id
OAUTH42_CLIENT_SECRET=your-client-secret
# Issuer URL formats:
# Multi-tenant (subdomain): https://{tenant}.oauth42.com
# Single-tenant/Demo: https://oauth42.com
# Local development: https://localhost:8443
OAUTH42_ISSUER=https://acme.oauth42.com
OAUTH42_REDIRECT_URI=http://localhost:3000/callback
OAUTH42_SCOPES=openid
OAUTH42_AUDIENCE=https://api.example.com # Optional
Programmatic Configuration
use Config;
let config = builder
.client_id
.client_secret
.issuer? // Tenant subdomain
.redirect_uri?
.add_scope
.add_scope
.audience
.build?;
Example Applications
The SDK includes two complete example applications:
o42client1 - Web Application (Actix-web)
- Server-side rendered templates
- Session-based authentication
- Login/logout flow with PKCE
- User dashboard
o42client2 - API Service (Axum)
- RESTful API with JWT validation
- Stateless authentication
- Protected endpoints
- Token introspection
Running Examples
# Web application example
# API service example
Testing
# Run all tests
# Unit tests only
# Integration tests (requires mock server)
API Documentation
Key types and functions:
OAuth42Client- Main client for OAuth2 operationsConfig- Client configurationTokenResponse- Access/refresh token responseUserInfo- OpenID Connect user informationOAuth42Auth- Actix-web middlewareoauth42_auth()- Axum layer factoryOAuth42User- Authenticated user extractor
License
Copyright (c) 2024 OAuth42, Inc. All rights reserved.
This SDK is proprietary software provided by OAuth42, Inc. for use with OAuth42 services. See LICENSE file for terms and conditions.