use auth_framework::{
AuthConfig, AuthFramework,
methods::{AuthMethodEnum, JwtMethod},
server::OAuth2Server,
storage::memory::InMemoryStorage,
};
use std::sync::Arc;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("🚀 Starting Simple OAuth 2.0 Server...");
tracing_subscriber::fmt::init();
let config = AuthConfig::new()
.secret("oauth-server-secret".to_string())
.issuer("https://auth.localhost:8080".to_string())
.audience("oauth-clients".to_string())
.token_lifetime(Duration::from_secs(3600))
.refresh_token_lifetime(Duration::from_secs(86400 * 7));
let mut auth_framework = AuthFramework::new(config);
let jwt_method = JwtMethod::new()
.secret_key("oauth-server-secret")
.issuer("https://auth.localhost:8080");
auth_framework.register_method("oauth", AuthMethodEnum::Jwt(jwt_method));
auth_framework.initialize().await?;
println!("✅ Auth framework initialized successfully!");
let storage = Arc::new(InMemoryStorage::new());
println!("✅ In-memory storage created");
let oauth_server = OAuth2Server::new(storage).await?;
println!("✅ OAuth 2.0 server created successfully!");
let server_config = oauth_server.get_server_configuration().await?;
println!("📋 Server Configuration:");
println!(
" Issuer: {}",
server_config
.get("issuer")
.unwrap_or(&serde_json::json!("Not configured"))
);
let demo_token = auth_framework
.create_auth_token(
"demo_client",
vec!["read".to_string(), "write".to_string()],
"oauth",
None,
)
.await?;
println!("🔑 Demo OAuth Token Created:");
println!(" Token: {}", demo_token.access_token);
println!(" User ID: {}", demo_token.user_id);
println!(" Scopes: {:?}", demo_token.scopes);
if auth_framework.validate_token(&demo_token).await? {
println!("✅ Token validation successful!");
if auth_framework
.check_permission(&demo_token, "read", "api")
.await?
{
println!("✅ Permission check passed for 'read' on 'api'!");
}
}
println!("\n🎉 OAuth 2.0 server is running and functional!");
println!("💡 This is a basic example showing working components.");
println!("💡 For production use, implement proper OAuth flows and client management.");
Ok(())
}