ruma_client_api/session/
sso_login_with_provider.rs

1//! `GET /_matrix/client/*/login/sso/redirect/{idpId}`
2//!
3//! Get the SSO login identity provider url.
4
5pub mod v3 {
6    //! `/v3/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3loginssoredirectidpid
9
10    use http::header::{LOCATION, SET_COOKIE};
11    use ruma_common::{
12        api::{request, response, Metadata},
13        metadata,
14    };
15
16    #[cfg(feature = "unstable-msc3824")]
17    use crate::session::SsoRedirectOidcAction;
18
19    const METADATA: Metadata = metadata! {
20        method: GET,
21        rate_limited: false,
22        authentication: None,
23        history: {
24            unstable => "/_matrix/client/unstable/org.matrix.msc2858/login/sso/redirect/:idp_id",
25            1.1 => "/_matrix/client/v3/login/sso/redirect/:idp_id",
26        }
27    };
28
29    /// Request type for the `sso_login_with_provider` endpoint.
30    #[request(error = crate::Error)]
31    pub struct Request {
32        /// The ID of the provider to use for SSO login.
33        #[ruma_api(path)]
34        pub idp_id: String,
35
36        /// URL to which the homeserver should return the user after completing
37        /// authentication with the SSO identity provider.
38        #[ruma_api(query)]
39        #[serde(rename = "redirectUrl")]
40        pub redirect_url: String,
41
42        /// The purpose for using the SSO redirect URL for OIDC-aware compatibility.
43        ///
44        /// This field uses the unstable prefix defined in [MSC3824].
45        ///
46        /// [MSC3824]: https://github.com/matrix-org/matrix-spec-proposals/pull/3824
47        #[cfg(feature = "unstable-msc3824")]
48        #[ruma_api(query)]
49        #[serde(skip_serializing_if = "Option::is_none", rename = "org.matrix.msc3824.action")]
50        pub action: Option<SsoRedirectOidcAction>,
51    }
52
53    /// Response type for the `sso_login_with_provider` endpoint.
54    #[response(error = crate::Error, status = FOUND)]
55    pub struct Response {
56        /// Redirect URL to the SSO identity provider.
57        #[ruma_api(header = LOCATION)]
58        pub location: String,
59
60        /// Cookie storing state to secure the SSO process.
61        #[ruma_api(header = SET_COOKIE)]
62        pub cookie: Option<String>,
63    }
64
65    impl Request {
66        /// Creates a new `Request` with the given identity provider ID and redirect URL.
67        pub fn new(idp_id: String, redirect_url: String) -> Self {
68            Self {
69                idp_id,
70                redirect_url,
71                #[cfg(feature = "unstable-msc3824")]
72                action: None,
73            }
74        }
75    }
76
77    impl Response {
78        /// Creates a new `Response` with the given SSO URL.
79        pub fn new(location: String) -> Self {
80            Self { location, cookie: None }
81        }
82    }
83
84    #[cfg(all(test, feature = "client"))]
85    mod tests {
86        use ruma_common::api::{MatrixVersion, OutgoingRequest as _, SendAccessToken};
87
88        use super::Request;
89
90        #[test]
91        fn serialize_sso_login_with_provider_request_uri() {
92            let req = Request::new("provider".to_owned(), "https://example.com/sso".to_owned())
93                .try_into_http_request::<Vec<u8>>(
94                    "https://homeserver.tld",
95                    SendAccessToken::None,
96                    &[MatrixVersion::V1_1],
97                )
98                .unwrap();
99
100            assert_eq!(
101            req.uri().to_string(),
102            "https://homeserver.tld/_matrix/client/v3/login/sso/redirect/provider?redirectUrl=https%3A%2F%2Fexample.com%2Fsso"
103        );
104        }
105    }
106}