data_modelling_sdk/auth/
mod.rs1use serde::{Deserialize, Serialize};
9
10#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
31pub enum AuthMode {
32 #[default]
34 None,
35 Web,
37 Local,
39 Online { api_url: String },
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
45pub struct GitHubEmail {
46 pub email: String,
47 pub verified: bool,
48 pub primary: bool,
49}
50
51#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
71pub struct AuthState {
72 pub mode: AuthMode,
74 pub authenticated: bool,
76 pub email: Option<String>,
78 pub available_emails: Option<Vec<GitHubEmail>>,
80 pub github_token: Option<String>,
82 pub api_url: Option<String>,
84 #[serde(default = "default_auth_source")]
86 pub auth_source: String,
87}
88
89fn default_auth_source() -> String {
90 "web".to_string()
91}
92
93impl Default for AuthState {
94 fn default() -> Self {
95 Self {
96 mode: AuthMode::None,
97 authenticated: false,
98 email: None,
99 available_emails: None,
100 github_token: None,
101 api_url: None,
102 auth_source: "web".to_string(),
103 }
104 }
105}
106
107#[derive(Debug, Clone, Serialize, Deserialize)]
109pub struct InitiateOAuthRequest {
110 pub redirect_uri: String,
111 pub source: String,
112}
113
114#[derive(Debug, Clone, Serialize, Deserialize)]
116pub struct InitiateOAuthResponse {
117 pub oauth_url: String,
118 pub state: String,
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize)]
123pub struct SelectEmailRequest {
124 pub email: String,
125}
126
127#[cfg(test)]
128mod tests {
129 use super::*;
130
131 #[test]
132 fn test_auth_mode_default() {
133 let mode = AuthMode::default();
134 assert_eq!(mode, AuthMode::None);
135 }
136
137 #[test]
138 fn test_auth_state_default() {
139 let state = AuthState::default();
140 assert!(!state.authenticated);
141 assert_eq!(state.mode, AuthMode::None);
142 assert_eq!(state.auth_source, "web");
143 }
144
145 #[test]
146 fn test_auth_state_serialization() {
147 let state = AuthState {
148 mode: AuthMode::Online {
149 api_url: "http://localhost:8080".to_string(),
150 },
151 authenticated: true,
152 email: Some("test@example.com".to_string()),
153 available_emails: Some(vec![GitHubEmail {
154 email: "test@example.com".to_string(),
155 verified: true,
156 primary: true,
157 }]),
158 github_token: None,
159 api_url: Some("http://localhost:8080".to_string()),
160 auth_source: "desktop".to_string(),
161 };
162
163 let json = serde_json::to_string(&state).unwrap();
164 let parsed: AuthState = serde_json::from_str(&json).unwrap();
165 assert_eq!(state, parsed);
166 }
167}