ruma_client_api/session/
sso_login.rs

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