Skip to main content

xai_rust/models/
auth.rs

1//! Authentication model types.
2
3use serde::Deserialize;
4
5/// API-key introspection response.
6#[derive(Debug, Clone, Deserialize)]
7pub struct ApiKeyInfo {
8    /// The masked API key as returned by the service.
9    #[serde(default)]
10    pub api_key: String,
11    /// Optional object type.
12    #[serde(default)]
13    pub object: String,
14    /// Optional organization identifier.
15    #[serde(default)]
16    pub organization_id: Option<String>,
17    /// Optional creation timestamp.
18    #[serde(default)]
19    pub created_at: Option<i64>,
20    /// Optional last-used timestamp.
21    #[serde(default)]
22    pub last_used_at: Option<i64>,
23    /// Optional expiration timestamp.
24    #[serde(default)]
25    pub expires_at: Option<i64>,
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn api_key_info_deserializes_minimal_shape() {
34        let payload = serde_json::json!({"api_key": "k_123", "object": "api_key"});
35        let info: ApiKeyInfo = serde_json::from_value(payload).unwrap();
36        assert_eq!(info.api_key, "k_123");
37        assert_eq!(info.object, "api_key");
38    }
39
40    #[test]
41    fn api_key_info_allows_optional_timestamps() {
42        let payload = serde_json::json!({
43            "api_key": "k_456",
44            "created_at": 1700000000,
45            "last_used_at": 1700000100,
46            "expires_at": 1900000000
47        });
48        let info: ApiKeyInfo = serde_json::from_value(payload).unwrap();
49        assert_eq!(info.created_at, Some(1700000000));
50        assert_eq!(info.last_used_at, Some(1700000100));
51        assert_eq!(info.expires_at, Some(1900000000));
52    }
53}