auth_framework/api/
mod.rs1pub mod admin;
7pub mod auth;
8pub mod error_codes;
9pub mod health;
10pub mod metrics;
11pub mod mfa;
12pub mod middleware;
13pub mod oauth;
14pub mod openapi;
15pub mod responses;
16pub mod server;
17pub mod users;
18pub mod validation;
19pub mod versioning;
20
21#[cfg(feature = "enhanced-rbac")]
22#[cfg(feature = "role-system")]
23pub mod rbac_endpoints;
24
25pub use responses::{ApiError, ApiResponse, ApiResult};
26pub use server::ApiServer;
27
28use crate::AuthFramework;
29use crate::errors::AuthError;
30use std::sync::Arc;
31
32#[derive(Clone)]
34pub struct ApiState {
35 pub auth_framework: Arc<AuthFramework>,
36 #[cfg(feature = "enhanced-rbac")]
37 pub authorization_service: Arc<crate::authorization_enhanced::AuthorizationService>,
38}
39
40impl ApiState {
41 pub async fn new(auth_framework: Arc<AuthFramework>) -> crate::errors::Result<Self> {
42 Ok(Self {
43 auth_framework,
44 #[cfg(feature = "enhanced-rbac")]
45 authorization_service: Arc::new(
46 crate::authorization_enhanced::AuthorizationService::new().await?,
47 ),
48 })
49 }
50
51 #[cfg(feature = "enhanced-rbac")]
52 pub fn with_authorization_service(
53 auth_framework: Arc<AuthFramework>,
54 authorization_service: Arc<crate::authorization_enhanced::AuthorizationService>,
55 ) -> Self {
56 Self {
57 auth_framework,
58 authorization_service,
59 }
60 }
61}
62
63pub fn extract_bearer_token(headers: &axum::http::HeaderMap) -> Option<String> {
65 headers
66 .get("authorization")
67 .and_then(|header| header.to_str().ok())
68 .and_then(|auth_str| auth_str.strip_prefix("Bearer "))
69 .map(|token| token.to_string())
70}
71
72pub async fn validate_api_token(
74 auth_framework: &AuthFramework,
75 token: &str,
76) -> Result<crate::tokens::AuthToken, AuthError> {
77 let token_obj = auth_framework
80 .token_manager()
81 .validate_session_jwt_token(token)?;
82
83 Ok(crate::tokens::AuthToken {
85 token_id: token_obj.jti.clone(),
86 user_id: token_obj.sub.clone(),
87 access_token: token.to_string(),
88 token_type: Some("Bearer".to_string()),
89 subject: Some(token_obj.sub.clone()),
90 issuer: Some(token_obj.iss.clone()),
91 refresh_token: None,
92 issued_at: chrono::DateTime::from_timestamp(token_obj.iat, 0)
93 .unwrap_or_else(chrono::Utc::now),
94 expires_at: chrono::DateTime::from_timestamp(token_obj.exp, 0)
95 .unwrap_or_else(chrono::Utc::now),
96 scopes: token_obj
97 .scope
98 .split_whitespace()
99 .map(|s| s.to_string())
100 .collect(),
101 auth_method: "jwt".to_string(),
102 client_id: token_obj.client_id,
103 user_profile: None,
104 permissions: token_obj.permissions.unwrap_or_default(),
105 roles: token_obj.roles.unwrap_or_default(),
106 metadata: crate::tokens::TokenMetadata {
107 session_id: None, ..Default::default()
109 },
110 })
111}