A robust, production-ready OAuth 2.0 server built in Rust that provides secure authentication flows for multiple OAuth providers. This server implements the OAuth 2.0 Authorization Code flow with PKCE (Proof Key for Code Exchange) for enhanced security.
This OAuth 2.0 server is designed to be a standalone authentication service that can be easily integrated into any application requiring OAuth authentication. It provides a clean, secure, and extensible solution for handling OAuth flows with multiple providers.
- π Secure OAuth 2.0 Implementation: Full OAuth 2.0 Authorization Code flow with PKCE
- π‘οΈ Security First: CSRF protection, session management, and secure token handling
- π Multiple Providers: Google, GitHub, Twitter, Discord, and Spotify support
- β‘ High Performance: Built with Rust and Axum for excellent performance
- π§ Extensible Architecture: Easy to add new OAuth providers
- π Health Monitoring: Built-in health checks and logging
- π³ Docker Ready: Complete Docker support for easy deployment
- π Comprehensive Documentation: Well-documented codebase with examples
Perfect for single-page applications (SPAs) that need secure user authentication without managing OAuth complexity on the frontend.
Serve as a dedicated authentication service in a microservices environment, providing centralized OAuth handling.
Use as an authentication layer in API gateways to handle OAuth flows before routing requests to backend services.
Great for development environments where you need to test OAuth integrations without setting up complex authentication systems.
Add modern OAuth authentication to existing applications without major refactoring.
- OAuth Providers: Modular provider implementations for each OAuth service
- Session Management: Secure session handling with tower-sessions
- HTTP Server: High-performance server built with Axum
- Configuration Management: Flexible configuration via TOML files or environment variables
- Error Handling: Comprehensive error handling and logging
- PKCE (Proof Key for Code Exchange): Prevents authorization code interception attacks
- CSRF Protection: Random state tokens for each OAuth flow
- Session Security: Secure session storage with configurable TTL
- HTTPS Ready: Designed for production deployment with SSL/TLS
- Rust 1.75+
- Cargo
- Docker (optional)
git clone <your-repo-url>
cd oauth2.0
Create a Settings.toml
file with your OAuth provider configurations:
port = 4427
[oauth.google]
client_id = "your-google-client-id"
client_secret = "your-google-client-secret"
auth_url = "https://accounts.google.com/o/oauth2/v2/auth"
token_url = "https://www.googleapis.com/oauth2/v3/token"
redirect_uri = "http://localhost:4427/callback"
user_info_url = "https://www.googleapis.com/oauth2/v2/userinfo"
[oauth.github]
client_id = "your-github-client-id"
client_secret = "your-github-client-secret"
auth_url = "https://github.com/login/oauth/authorize"
token_url = "https://github.com/login/oauth/access_token"
redirect_uri = "http://localhost:4427/callback"
user_info_url = "https://api.github.com/user"
# Add other providers as needed
# Development
cargo run
# Production
cargo build --release
./target/release/oauth-server
The server will start on http://localhost:4427
Visit http://localhost:4427/
to see the OAuth provider buttons and test the authentication flow.
Endpoint | Method | Description |
---|---|---|
/ |
GET | Home page with OAuth provider buttons |
/authorize |
GET | Initiates OAuth flow (requires provider query param) |
/callback |
GET | OAuth callback handler (requires code and state query params) |
/health |
GET | Health check endpoint |
- Initiate Flow:
GET /authorize?provider=google
- User Authorization: User is redirected to the OAuth provider
- Callback Processing: Provider redirects back to
/callback
- User Information: Returns user data in JSON format
{
"user_id": "user@example.com",
"provider": "google"
}
For production deployments, you can use environment variables:
export PORT=4427
export GOOGLE_CLIENT_ID="your-client-id"
export GOOGLE_CLIENT_SECRET="your-client-secret"
export GOOGLE_REDIRECT_URI="https://yourdomain.com/callback"
src/
βββ main.rs # Application entry point
βββ settings.rs # Configuration management
βββ traits.rs # OAuth provider traits
βββ primitives.rs # Core data structures
βββ types.rs # Type definitions
βββ providers/ # OAuth provider implementations
β βββ mod.rs # Provider registry
β βββ google.rs # Google OAuth
β βββ github.rs # GitHub OAuth
β βββ twitter.rs # Twitter OAuth
β βββ discord.rs # Discord OAuth
β βββ spotify.rs # Spotify OAuth
βββ server/ # HTTP server components
βββ mod.rs # Server module
βββ server.rs # Server implementation
βββ handlers.rs # Request handlers
βββ errors.rs # Error handling
-
Create Provider Implementation:
// src/providers/new_provider.rs pub struct NewProvider { /* ... */ } impl OAuthProvider for NewProvider { /* ... */ } pub struct NewProviderFactory; impl OAuthProviderFactory for NewProviderFactory { /* ... */ }
-
Register in Provider Registry:
// In src/providers/mod.rs m.insert("new_provider", Arc::new(NewProviderFactory));
-
Add Configuration:
[oauth.new_provider] client_id = "..." client_secret = "..." # ... other required fields
cargo test
cargo doc --open
We welcome contributions from the community! This project is completely open to improvements, new features, bug fixes, and other contributions.
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature
- Make your changes:
- Add new OAuth providers
- Improve security features
- Enhance documentation
- Add tests
- Optimize performance
- Fix bugs
- Test your changes:
cargo test
- Commit your changes:
git commit -m 'Add amazing feature'
- Push to the branch:
git push origin feature/amazing-feature
- Open a Pull Request
- Follow Rust coding conventions
- Add comprehensive documentation
- Include tests for new features
- Update documentation when adding new features
- Ensure all tests pass before submitting PR
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with Rust and Axum
- OAuth 2.0 implementation using oauth2
- Session management with tower-sessions
- HTTP client using reqwest