1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! [GET /_matrix/client/r0/login/sso/redirect/{idp_id}](https://github.com/matrix-org/matrix-doc/blob/master/proposals/2858-Multiple-SSO-Identity-Providers.md)
//!
//! This uses the unstable prefix in [MSC2858](https://github.com/matrix-org/matrix-doc/pull/2858).

use ruma_api::ruma_api;

ruma_api! {
    metadata: {
        description: "Get the SSO login identity provider url.",
        method: GET,
        name: "sso_login_with_provider",
        path: "/_matrix/client/unstable/org.matrix.msc2858/login/sso/redirect/:idp_id",
        rate_limited: false,
        authentication: None,
    }

    request: {
        /// The ID of the provider to use for SSO login.
        #[ruma_api(path)]
        pub idp_id: &'a str,

        /// URL to which the homeserver should return the user after completing
        /// authentication with the SSO identity provider.
        #[ruma_api(query)]
        #[serde(rename = "redirectUrl")]
        pub redirect_url: &'a str,
    }

    response: {
        /// Redirect URL to the SSO identity provider.
        #[ruma_api(header = LOCATION)]
        pub location: String,
    }

    error: crate::Error
}

impl<'a> Request<'a> {
    /// Creates a new `Request` with the given identity provider ID and redirect URL.
    pub fn new(idp_id: &'a str, redirect_url: &'a str) -> Self {
        Self { idp_id, redirect_url }
    }
}

impl Response {
    /// Creates a new `Response` with the given SSO URL.
    pub fn new(location: String) -> Self {
        Self { location }
    }
}

#[cfg(all(test, feature = "client"))]
mod tests {
    use ruma_api::{OutgoingRequest as _, SendAccessToken};

    use super::Request;

    #[test]
    fn serialize_sso_login_with_provider_request_uri() {
        let req = Request { idp_id: "provider", redirect_url: "https://example.com/sso" }
            .try_into_http_request::<Vec<u8>>("https://homeserver.tld", SendAccessToken::None)
            .unwrap();

        assert_eq!(
            req.uri().to_string(),
            "https://homeserver.tld/_matrix/client/unstable/org.matrix.msc2858/login/sso/redirect/provider?redirectUrl=https%3A%2F%2Fexample.com%2Fsso"
        );
    }
}