1use async_trait::async_trait;
8use serde::{Deserialize, Serialize};
9
10use crate::Result;
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
14#[serde(rename_all = "camelCase")]
15pub struct HostAuthStatus {
16 pub host: String,
18 pub logged_in: bool,
20 #[serde(skip_serializing_if = "Option::is_none")]
22 pub user: Option<String>,
23}
24
25#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(rename_all = "camelCase")]
31pub struct AuthStatus {
32 pub logged_in: bool,
34 #[serde(skip_serializing_if = "Option::is_none")]
36 pub user: Option<String>,
37 #[serde(default)]
39 pub scopes: Vec<String>,
40 #[serde(default, skip_serializing_if = "Vec::is_empty")]
42 pub hosts: Vec<HostAuthStatus>,
43}
44
45#[async_trait]
57pub trait AuthProvider: std::fmt::Debug + Send + Sync {
58 async fn login(&self, token: Option<&str>) -> Result<()>;
69
70 async fn logout(&self) -> Result<()>;
76
77 async fn status(&self) -> Result<AuthStatus>;
83
84 async fn token(&self) -> Result<String>;
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 #[test]
99 fn test_should_omit_empty_hosts_field_when_serializing() {
100 let status = AuthStatus {
101 logged_in: true,
102 user: Some("alice".to_string()),
103 scopes: vec![],
104 hosts: vec![],
105 };
106 let json = serde_json::to_string(&status).expect("serialize");
107 assert!(!json.contains("hosts"));
108 }
109
110 #[test]
111 fn test_should_include_hosts_field_when_non_empty() {
112 let status = AuthStatus {
113 logged_in: true,
114 user: Some("alice".to_string()),
115 scopes: vec![],
116 hosts: vec![HostAuthStatus {
117 host: "gitlab.com".to_string(),
118 logged_in: true,
119 user: Some("alice".to_string()),
120 }],
121 };
122 let json = serde_json::to_string(&status).expect("serialize");
123 assert!(json.contains("\"hosts\""));
124 assert!(json.contains("\"gitlab.com\""));
125 }
126
127 #[test]
128 fn test_should_deserialize_auth_status_logged_in() {
129 let json = r#"{
130 "loggedIn": true,
131 "user": "octocat",
132 "scopes": ["repo", "read:org"]
133 }"#;
134 let status: AuthStatus = serde_json::from_str(json).expect("valid AuthStatus JSON");
135
136 assert!(status.logged_in);
137 assert_eq!(status.user.as_deref(), Some("octocat"));
138 assert_eq!(
139 status.scopes,
140 vec!["repo".to_string(), "read:org".to_string()]
141 );
142 }
143
144 #[test]
145 fn test_should_deserialize_auth_status_not_logged_in() {
146 let json = r#"{
147 "loggedIn": false
148 }"#;
149 let status: AuthStatus = serde_json::from_str(json).expect("valid AuthStatus JSON");
150
151 assert!(!status.logged_in);
152 assert!(status.user.is_none());
153 assert!(status.scopes.is_empty());
154 }
155
156 #[test]
157 fn test_should_roundtrip_auth_status_via_serde() {
158 let json = r#"{
159 "loggedIn": true,
160 "user": "alice",
161 "scopes": ["repo"]
162 }"#;
163 let status: AuthStatus = serde_json::from_str(json).expect("deserialize");
164 let re_serialized = serde_json::to_string(&status).expect("serialize");
165 let round_tripped: AuthStatus =
166 serde_json::from_str(&re_serialized).expect("re-deserialize");
167
168 assert_eq!(round_tripped.logged_in, status.logged_in);
169 assert_eq!(round_tripped.user, status.user);
170 assert_eq!(round_tripped.scopes, status.scopes);
171 }
172
173 #[test]
174 fn test_should_serialize_auth_status_camel_case() {
175 let status = AuthStatus {
176 logged_in: true,
177 user: Some("bob".into()),
178 scopes: vec!["gist".into()],
179 hosts: vec![],
180 };
181 let json = serde_json::to_string(&status).expect("serialize");
182 assert!(json.contains("\"loggedIn\":true"));
183 assert!(json.contains("\"user\":\"bob\""));
184 assert!(json.contains("\"scopes\""));
185 assert!(!json.contains("\"logged_in\""));
186 }
187
188 #[test]
189 fn test_should_omit_none_user_on_serialize() {
190 let status = AuthStatus {
191 logged_in: false,
192 user: None,
193 scopes: vec![],
194 hosts: vec![],
195 };
196 let json = serde_json::to_string(&status).expect("serialize");
197 assert!(!json.contains("null"));
198 assert!(!json.contains("\"user\":"));
199 assert!(json.contains("\"loggedIn\":false"));
200 }
201
202 #[test]
203 fn test_should_default_empty_scopes() {
204 let json = r#"{"loggedIn":true,"user":"u"}"#;
205 let status: AuthStatus = serde_json::from_str(json).expect("deserialize");
206 assert!(status.scopes.is_empty());
207 }
208
209 #[test]
210 fn test_should_format_debug_for_auth_status() {
211 let status = AuthStatus {
212 logged_in: true,
213 user: Some("test".into()),
214 scopes: vec!["repo".into()],
215 hosts: vec![],
216 };
217 let debug = format!("{status:?}");
218 assert!(debug.contains("AuthStatus"));
219 assert!(debug.contains("test"));
220 }
221
222 #[test]
223 fn test_should_clone_auth_status() {
224 let original = AuthStatus {
225 logged_in: true,
226 user: Some("alice".into()),
227 scopes: vec!["repo".into(), "gist".into()],
228 hosts: vec![],
229 };
230 let cloned = original.clone();
231
232 assert_eq!(cloned.logged_in, original.logged_in);
233 assert_eq!(cloned.user, original.user);
234 assert_eq!(cloned.scopes, original.scopes);
235
236 assert!(!std::ptr::eq(
238 std::ptr::from_ref(&cloned.scopes),
239 std::ptr::from_ref(&original.scopes)
240 ));
241 }
242}