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