ruma_client_api/uiaa/
get_uiaa_fallback_page.rs

1//! `GET /_matrix/client/*/auth/{auth_type}/fallback/web?session={session_id}`
2//!
3//! Get UIAA fallback web page.
4
5pub mod v3 {
6    //! `/v3/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#fallback
9
10    use ruma_common::{
11        api::{request, Metadata},
12        metadata,
13    };
14
15    use crate::uiaa::AuthType;
16
17    const METADATA: Metadata = metadata! {
18        method: GET,
19        rate_limited: false,
20        authentication: None,
21        history: {
22            1.0 => "/_matrix/client/r0/auth/:auth_type/fallback/web",
23            1.1 => "/_matrix/client/v3/auth/:auth_type/fallback/web",
24        }
25    };
26
27    /// Request type for the `authorize_fallback` endpoint.
28    #[request(error = crate::Error)]
29    pub struct Request {
30        /// The type name (`m.login.dummy`, etc.) of the UIAA stage to get a fallback page for.
31        #[ruma_api(path)]
32        pub auth_type: AuthType,
33
34        /// The ID of the session given by the homeserver.
35        #[ruma_api(query)]
36        pub session: String,
37    }
38
39    impl Request {
40        /// Creates a new `Request` with the given auth type and session ID.
41        pub fn new(auth_type: AuthType, session: String) -> Self {
42            Self { auth_type, session }
43        }
44    }
45
46    /// Response type for the `authorize_fallback` endpoint.
47    #[derive(Debug, Clone)]
48    #[allow(clippy::exhaustive_enums)]
49    pub enum Response {
50        /// The response is a redirect.
51        Redirect(Redirect),
52
53        /// The response is an HTML page.
54        Html(HtmlPage),
55    }
56
57    /// The data of a redirect.
58    #[derive(Debug, Clone)]
59    #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
60    pub struct Redirect {
61        /// The URL to redirect the user to.
62        pub url: String,
63    }
64
65    /// The data of a HTML page.
66    #[derive(Debug, Clone)]
67    #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
68    pub struct HtmlPage {
69        /// The body of the HTML page.
70        pub body: Vec<u8>,
71    }
72
73    impl Response {
74        /// Creates a new HTML `Response` with the given HTML body.
75        pub fn html(body: Vec<u8>) -> Self {
76            Self::Html(HtmlPage { body })
77        }
78
79        /// Creates a new HTML `Response` with the given redirect URL.
80        pub fn redirect(url: String) -> Self {
81            Self::Redirect(Redirect { url })
82        }
83    }
84
85    #[cfg(feature = "server")]
86    impl ruma_common::api::OutgoingResponse for Response {
87        fn try_into_http_response<T: Default + bytes::BufMut>(
88            self,
89        ) -> Result<http::Response<T>, ruma_common::api::error::IntoHttpError> {
90            match self {
91                Response::Redirect(Redirect { url }) => Ok(http::Response::builder()
92                    .status(http::StatusCode::FOUND)
93                    .header(http::header::LOCATION, url)
94                    .body(T::default())?),
95                Response::Html(HtmlPage { body }) => Ok(http::Response::builder()
96                    .status(http::StatusCode::OK)
97                    .header(http::header::CONTENT_TYPE, "text/html; charset=utf-8")
98                    .body(ruma_common::serde::slice_to_buf(&body))?),
99            }
100        }
101    }
102
103    #[cfg(feature = "client")]
104    impl ruma_common::api::IncomingResponse for Response {
105        type EndpointError = crate::Error;
106
107        fn try_from_http_response<T: AsRef<[u8]>>(
108            response: http::Response<T>,
109        ) -> Result<Self, ruma_common::api::error::FromHttpResponseError<Self::EndpointError>>
110        {
111            use ruma_common::api::{
112                error::{DeserializationError, FromHttpResponseError, HeaderDeserializationError},
113                EndpointError,
114            };
115
116            if response.status().as_u16() >= 400 {
117                return Err(FromHttpResponseError::Server(
118                    Self::EndpointError::from_http_response(response),
119                ));
120            }
121
122            if response.status() == http::StatusCode::FOUND {
123                let Some(location) = response.headers().get(http::header::LOCATION) else {
124                    return Err(DeserializationError::Header(
125                        HeaderDeserializationError::MissingHeader(
126                            http::header::LOCATION.to_string(),
127                        ),
128                    )
129                    .into());
130                };
131
132                let url = location.to_str()?;
133                return Ok(Self::Redirect(Redirect { url: url.to_owned() }));
134            }
135
136            let body = response.into_body().as_ref().to_owned();
137            Ok(Self::Html(HtmlPage { body }))
138        }
139    }
140
141    #[cfg(all(test, any(feature = "client", feature = "server")))]
142    mod tests {
143        use assert_matches2::assert_matches;
144        use http::header::{CONTENT_TYPE, LOCATION};
145        #[cfg(feature = "client")]
146        use ruma_common::api::IncomingResponse;
147        #[cfg(feature = "server")]
148        use ruma_common::api::OutgoingResponse;
149
150        use super::Response;
151
152        #[cfg(feature = "client")]
153        #[test]
154        fn incoming_redirect() {
155            use super::Redirect;
156
157            let http_response = http::Response::builder()
158                .status(http::StatusCode::FOUND)
159                .header(LOCATION, "http://localhost/redirect")
160                .body(Vec::<u8>::new())
161                .unwrap();
162
163            let response = Response::try_from_http_response(http_response).unwrap();
164            assert_matches!(response, Response::Redirect(Redirect { url }));
165            assert_eq!(url, "http://localhost/redirect");
166        }
167
168        #[cfg(feature = "client")]
169        #[test]
170        fn incoming_html() {
171            use super::HtmlPage;
172
173            let http_response = http::Response::builder()
174                .status(http::StatusCode::OK)
175                .header(CONTENT_TYPE, "text/html; charset=utf-8")
176                .body(b"<h1>My Page</h1>")
177                .unwrap();
178
179            let response = Response::try_from_http_response(http_response).unwrap();
180            assert_matches!(response, Response::Html(HtmlPage { body }));
181            assert_eq!(body, b"<h1>My Page</h1>");
182        }
183
184        #[cfg(feature = "server")]
185        #[test]
186        fn outgoing_redirect() {
187            let response = Response::redirect("http://localhost/redirect".to_owned());
188
189            let http_response = response.try_into_http_response::<Vec<u8>>().unwrap();
190
191            assert_eq!(http_response.status(), http::StatusCode::FOUND);
192            assert_eq!(
193                http_response.headers().get(LOCATION).unwrap().to_str().unwrap(),
194                "http://localhost/redirect"
195            );
196            assert!(http_response.into_body().is_empty());
197        }
198
199        #[cfg(feature = "server")]
200        #[test]
201        fn outgoing_html() {
202            let response = Response::html(b"<h1>My Page</h1>".to_vec());
203
204            let http_response = response.try_into_http_response::<Vec<u8>>().unwrap();
205
206            assert_eq!(http_response.status(), http::StatusCode::OK);
207            assert_eq!(
208                http_response.headers().get(CONTENT_TYPE).unwrap().to_str().unwrap(),
209                "text/html; charset=utf-8"
210            );
211            assert_eq!(http_response.into_body(), b"<h1>My Page</h1>");
212        }
213    }
214}