-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaxum_oauth2_github.rs
More file actions
80 lines (69 loc) · 2.78 KB
/
axum_oauth2_github.rs
File metadata and controls
80 lines (69 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! # Axum GitHub OAuth2 Example
//!
//! This example demonstrates how to set up AuthEngine with Axum for GitHub OAuth2 login.
//!
//! To run this example, you'll need:
//! - `AUTHKESTRA_GITHUB_CLIENT_ID`
//! - `AUTHKESTRA_GITHUB_CLIENT_SECRET`
use authkestra::flow::{AuthEngine, OAuth2Flow};
use authkestra_axum::{AuthSession, AuthkestraAxumError, AuthkestraAxumExt, AuthkestraState};
use authkestra_engine::{Configured, SessionConfig};
use authkestra_providers::github::GithubProvider;
use authkestra_session::SessionStore;
use axum::{
response::{IntoResponse, Json},
routing::get,
Router,
};
use serde_json::json;
use std::sync::Arc;
use tower_cookies::CookieManagerLayer;
use tower_http::services::ServeDir;
/// AuthEngine state with support for session only.
type AppState = AuthkestraState<Configured<Arc<dyn SessionStore>>>;
#[tokio::main]
async fn main() {
// Load environment variables from .env file
dotenvy::dotenv().ok();
let client_id = std::env::var("AUTHKESTRA_GITHUB_CLIENT_ID")
.expect("AUTHKESTRA_GITHUB_CLIENT_ID must be set");
let client_secret = std::env::var("AUTHKESTRA_GITHUB_CLIENT_SECRET")
.expect("AUTHKESTRA_GITHUB_CLIENT_SECRET must be set");
let redirect_uri = std::env::var("AUTHKESTRA_GITHUB_REDIRECT_URI")
.unwrap_or_else(|_| "http://localhost:3000/auth/github/callback".to_string());
let github_provider = GithubProvider::new(client_id, client_secret, redirect_uri);
// Session Store
let session_store: Arc<dyn SessionStore> =
Arc::new(authkestra_session_memory::MemoryStore::default());
let auth_engine = AuthEngine::builder()
.provider(OAuth2Flow::new(github_provider))
.session_store(session_store)
.session_config(SessionConfig {
secure: false,
..Default::default()
})
.build();
let state = AppState {
authkestra: auth_engine.clone(),
};
let app = Router::new()
.fallback_service(ServeDir::new("authkestra-examples/static"))
.route("/api/user", get(get_user))
.merge(auth_engine.axum_router())
.layer(CookieManagerLayer::new())
.with_state(state);
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
println!("🚀 Axum GitHub OAuth2 running on http://localhost:3000");
axum::serve(listener, app).await.unwrap();
}
async fn get_user(session: Result<AuthSession, AuthkestraAxumError>) -> impl IntoResponse {
match session {
Ok(AuthSession(session)) => Json(json!({
"id": session.identity.external_id,
"username": session.identity.username,
"email": session.identity.email,
"provider": session.identity.provider_id,
})),
Err(_) => Json(json!({ "error": "Not authenticated" })),
}
}