OAuth token service
A lightweight Rust library for managing OAuth access tokens with automatic renewal support. Implements the client credentials flow using base64-encoded credentials. Built specifically for Authentik but designed to work with any OAuth2-compatible identity provider.
Features
- Automatic token renewal when expired
- Thread-safe token management using
Arc<Mutex<>> - Client credentials OAuth2 flow
- Configurable identity provider URL
- Built on top of the
oauth2crate - Safe redirect policy to prevent SSRF
- Async/await support
Installation
Add this to your Cargo.toml:
[]
= "0.1.2"
Quick start
use ;
async
Configuration
The TokenServiceConfig requires the following fields:
identity_service_base_url: Base URL of your identity provider (e.g., "https://identity.example.com")username: Username for client credentialstoken: Token/password for client credentialsclient_id: Your OAuth client ID
The service will automatically construct the necessary OAuth endpoints by appending /authorize/ and /token/ to the base URL.
Error Handling
The service provides a TokenServiceError enum with two variants:
TokenError: When no valid token was returned from the identity providerNetworkError: Specifically handles HTTP client errors