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