Skip to main content

uptrakit_openapi_client/
oidc_auth.rs

1use crate::Result;
2use crate::UptrakitClient;
3use crate::types_impl::auth::AuthResponse;
4use crate::types_impl::oidc_auth::{
5    OidcAuthorizeResponse, OidcCompleteRegistrationRequest, OidcExchangeRequest, OidcLinkRequest,
6};
7use uuid::Uuid;
8
9impl UptrakitClient {
10    /// Get the OIDC authorization URL for a provider.
11    ///
12    /// This endpoint does not require authentication.
13    pub async fn oidc_authorize(&self, provider_id: &Uuid) -> Result<OidcAuthorizeResponse> {
14        self.get_unauth(&crate::paths::oidc_auth::authorize(provider_id))
15            .await
16    }
17
18    /// Exchange an OIDC authorization code for tokens.
19    ///
20    /// This endpoint does not require authentication.
21    pub async fn oidc_exchange(&self, req: &OidcExchangeRequest) -> Result<AuthResponse> {
22        self.post_json_unauth(crate::paths::oidc_auth::EXCHANGE, req)
23            .await
24    }
25
26    /// Link an OIDC account to an existing user.
27    pub async fn oidc_link(&self, req: &OidcLinkRequest) -> Result<AuthResponse> {
28        self.post_json(crate::paths::oidc_auth::LINK, req).await
29    }
30
31    /// Complete OIDC registration with a registration code and token.
32    ///
33    /// This endpoint does not require authentication.
34    pub async fn oidc_complete_registration(
35        &self,
36        req: &OidcCompleteRegistrationRequest,
37    ) -> Result<AuthResponse> {
38        self.post_json_unauth(crate::paths::oidc_auth::COMPLETE_REGISTRATION, req)
39            .await
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use crate::types_impl::SecretString;
46    use crate::types_impl::oidc_auth::{
47        OidcCompleteRegistrationRequest, OidcExchangeRequest, OidcLinkRequest,
48    };
49
50    #[test]
51    fn oidc_exchange_request_serialization() {
52        let req = OidcExchangeRequest {
53            code: "auth-code-xyz".to_string(),
54        };
55        let json = serde_json::to_value(&req).expect("serialize");
56        assert_eq!(json["code"], "auth-code-xyz");
57    }
58
59    #[test]
60    fn oidc_link_request_serialization() {
61        let req = OidcLinkRequest {
62            link_token: SecretString::new("link-tok-abc"),
63            password: Some(SecretString::new("SecurePass123")),
64        };
65        let json = serde_json::to_value(&req).expect("serialize");
66        assert_eq!(json["link_token"], "link-tok-abc");
67        assert_eq!(json["password"], "SecurePass123");
68    }
69
70    #[test]
71    fn oidc_link_request_without_password() {
72        let req = OidcLinkRequest {
73            link_token: SecretString::new("link-tok-abc"),
74            password: None,
75        };
76        let json = serde_json::to_value(&req).expect("serialize");
77        assert_eq!(json["link_token"], "link-tok-abc");
78        assert!(json["password"].is_null());
79    }
80
81    #[test]
82    fn oidc_complete_registration_request_serialization() {
83        let req = OidcCompleteRegistrationRequest {
84            registration_code: SecretString::new("reg-code-123"),
85            registration_token: SecretString::new("reg-tok-456"),
86        };
87        let json = serde_json::to_value(&req).expect("serialize");
88        assert_eq!(json["registration_code"], "reg-code-123");
89        assert_eq!(json["registration_token"], "reg-tok-456");
90    }
91}