Skip to main content

crates_docs/server/auth/
types.rs

1//! Authentication types
2
3/// OAuth provider type
4#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, PartialEq)]
5pub enum OAuthProvider {
6    /// Custom OAuth provider
7    Custom,
8    /// GitHub OAuth
9    GitHub,
10    /// Google OAuth
11    Google,
12    /// Keycloak
13    Keycloak,
14}
15
16/// Authentication provider type
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum AuthProvider {
19    /// No authentication
20    None,
21    /// OAuth authentication
22    OAuth,
23    /// API Key authentication
24    #[cfg(feature = "api-key")]
25    ApiKey,
26}
27
28/// API Key generation result.
29///
30/// The plain-text key should be shown once to the operator and stored securely.
31/// The hash should be persisted in configuration or external secret storage.
32#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
33#[cfg(feature = "api-key")]
34pub struct GeneratedApiKey {
35    /// Plain-text API key for one-time display
36    pub key: String,
37    /// Stable key identifier derived from the key
38    pub key_id: String,
39    /// Argon2 PHC hash to store and verify against
40    pub hash: String,
41}
42
43/// Authentication context
44#[derive(Debug, Clone)]
45pub struct AuthContext {
46    /// Authentication provider used
47    pub provider: AuthProvider,
48    /// User ID (if available)
49    pub user_id: Option<String>,
50    /// User email (if available)
51    pub user_email: Option<String>,
52    /// API key identifier (if API key auth)
53    #[cfg(feature = "api-key")]
54    pub api_key_id: Option<String>,
55}
56
57impl AuthContext {
58    /// Create a new authentication context
59    #[must_use]
60    pub fn new(provider: AuthProvider) -> Self {
61        Self {
62            provider,
63            user_id: None,
64            user_email: None,
65            #[cfg(feature = "api-key")]
66            api_key_id: None,
67        }
68    }
69
70    /// Check if authentication is authenticated
71    #[must_use]
72    pub fn is_authenticated(&self) -> bool {
73        !matches!(self.provider, AuthProvider::None)
74    }
75}