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::{auth_scheme::NoAuthentication, request, response},
11        metadata,
12    };
13
14    use crate::session::SsoRedirectAction;
15
16    metadata! {
17        method: GET,
18        rate_limited: false,
19        authentication: NoAuthentication,
20        history: {
21            1.0 => "/_matrix/client/r0/login/sso/redirect",
22            1.1 => "/_matrix/client/v3/login/sso/redirect",
23        }
24    }
25
26    /// Request type for the `sso_login` endpoint.
27    #[request(error = crate::Error)]
28    pub struct Request {
29        /// URL to which the homeserver should return the user after completing
30        /// authentication with the SSO identity provider.
31        #[ruma_api(query)]
32        #[serde(rename = "redirectUrl")]
33        pub redirect_url: String,
34
35        /// The action that the user wishes to take at the SSO redirect.
36        #[ruma_api(query)]
37        #[serde(skip_serializing_if = "Option::is_none")]
38        pub action: Option<SsoRedirectAction>,
39    }
40
41    /// Response type for the `sso_login` endpoint.
42    #[response(error = crate::Error, status = FOUND)]
43    pub struct Response {
44        /// Redirect URL to the SSO identity provider.
45        #[ruma_api(header = LOCATION)]
46        pub location: String,
47
48        /// Cookie storing state to secure the SSO process.
49        #[ruma_api(header = SET_COOKIE)]
50        pub cookie: Option<String>,
51    }
52
53    impl Request {
54        /// Creates a new `Request` with the given redirect URL.
55        pub fn new(redirect_url: String) -> Self {
56            Self { redirect_url, action: None }
57        }
58    }
59
60    impl Response {
61        /// Creates a new `Response` with the given SSO URL.
62        pub fn new(location: String) -> Self {
63            Self { location, cookie: None }
64        }
65    }
66
67    #[cfg(all(test, feature = "client"))]
68    mod tests {
69        use std::borrow::Cow;
70
71        use ruma_common::api::{
72            MatrixVersion, OutgoingRequest, SupportedVersions, auth_scheme::SendAccessToken,
73        };
74
75        use super::Request;
76
77        #[test]
78        fn serialize_sso_login_request_uri() {
79            let supported = SupportedVersions {
80                versions: [MatrixVersion::V1_1].into(),
81                features: Default::default(),
82            };
83            let req: http::Request<Vec<u8>> = Request::new("https://example.com/sso".to_owned())
84                .try_into_http_request(
85                    "https://homeserver.tld",
86                    SendAccessToken::None,
87                    Cow::Owned(supported),
88                )
89                .unwrap();
90
91            assert_eq!(
92                req.uri().to_string(),
93                "https://homeserver.tld/_matrix/client/v3/login/sso/redirect?redirectUrl=https%3A%2F%2Fexample.com%2Fsso"
94            );
95        }
96    }
97}