ruma_client_api/session/
get_login_token.rs

1//! `GET /_matrix/client/*/login/get_token`
2//!
3//! Generate a single-use, time-limited, `m.login.token` token.
4
5pub mod v1 {
6    //! `/v1/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#post_matrixclientv1loginget_token
9
10    use std::time::Duration;
11
12    use ruma_common::{
13        api::{request, response, Metadata},
14        metadata,
15    };
16
17    use crate::uiaa::{AuthData, UiaaResponse};
18
19    const METADATA: Metadata = metadata! {
20        method: POST,
21        rate_limited: true,
22        authentication: AccessToken,
23        history: {
24            unstable => "/_matrix/client/unstable/org.matrix.msc3882/login/get_token",
25            1.7 => "/_matrix/client/v1/login/get_token",
26        }
27    };
28
29    /// Request type for the `login` endpoint.
30    #[request(error = UiaaResponse)]
31    #[derive(Default)]
32    pub struct Request {
33        /// Additional authentication information for the user-interactive authentication API.
34        #[serde(skip_serializing_if = "Option::is_none")]
35        pub auth: Option<AuthData>,
36    }
37
38    /// Response type for the `login` endpoint.
39    #[response(error = UiaaResponse)]
40    pub struct Response {
41        /// The time remaining in milliseconds until the homeserver will no longer accept the
42        /// token.
43        ///
44        /// 2 minutes is recommended as a default.
45        #[serde(with = "ruma_common::serde::duration::ms", rename = "expires_in_ms")]
46        pub expires_in: Duration,
47
48        /// The login token for the `m.login.token` login flow.
49        pub login_token: String,
50    }
51
52    impl Request {
53        /// Creates a new empty `Request`.
54        pub fn new() -> Self {
55            Self::default()
56        }
57    }
58
59    impl Response {
60        /// Creates a new `Response` with the given expiration duration and login token.
61        pub fn new(expires_in: Duration, login_token: String) -> Self {
62            Self { expires_in, login_token }
63        }
64
65        /// Creates a new `Response` with the default expiration duration and the given login token.
66        pub fn with_default_expiration_duration(login_token: String) -> Self {
67            Self::new(Self::default_expiration_duration(), login_token)
68        }
69
70        fn default_expiration_duration() -> Duration {
71            // 2 minutes.
72            Duration::from_secs(2 * 60)
73        }
74    }
75}