libQuotient
A Qt library for building matrix clients
login.h
Go to the documentation of this file.
1 // THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN
2 
3 #pragma once
4 
5 #include <Quotient/csapi/definitions/user_identifier.h>
6 #include <Quotient/csapi/definitions/wellknown/full.h>
7 
8 #include <Quotient/jobs/basejob.h>
9 
10 namespace Quotient {
11 
12 //! \brief Get the supported login types to authenticate users
13 //!
14 //! Gets the homeserver's supported login types to authenticate users. Clients
15 //! should pick one of these and supply it as the `type` when logging in.
16 class QUOTIENT_API GetLoginFlowsJob : public BaseJob {
17 public:
18  // Inner data structures
19 
20  struct QUOTIENT_API LoginFlow {
21  //! The login type. This is supplied as the `type` when
22  //! logging in.
23  QString type;
24 
25  //! If `type` is `m.login.token`, an optional field to indicate
26  //! to the unauthenticated client that the homeserver supports
27  //! the [`POST /login/get_token`](/client-server-api/#post_matrixclientv1loginget_token)
28  //! endpoint. Note that supporting the endpoint does not
29  //! necessarily indicate that the user attempting to log in will
30  //! be able to generate such a token.
31  bool getLoginToken{ false };
32  };
33 
34  // Construction/destruction
35 
36  explicit GetLoginFlowsJob();
37 
38  //! \brief Construct a URL without creating a full-fledged job object
39  //!
40  //! This function can be used when a URL for GetLoginFlowsJob
41  //! is necessary but the job itself isn't.
42  static QUrl makeRequestUrl(const HomeserverData& hsData);
43 
44  // Result properties
45 
46  //! The homeserver's supported login types
47  QVector<LoginFlow> flows() const { return loadFromJson<QVector<LoginFlow>>("flows"_L1); }
48 };
49 
50 inline auto collectResponse(const GetLoginFlowsJob* job) { return job->flows(); }
51 
52 template <>
53 struct QUOTIENT_API JsonObjectConverter<GetLoginFlowsJob::LoginFlow> {
54  static void fillFrom(const QJsonObject& jo, GetLoginFlowsJob::LoginFlow& result)
55  {
56  fillFromJson(jo.value("type"_L1), result.type);
57  fillFromJson(jo.value("get_login_token"_L1), result.getLoginToken);
58  }
59 };
60 
61 //! \brief Authenticates the user.
62 //!
63 //! Authenticates the user, and issues an access token they can
64 //! use to authorize themself in subsequent requests.
65 //!
66 //! If the client does not supply a `device_id`, the server must
67 //! auto-generate one.
68 //!
69 //! The returned access token must be associated with the `device_id`
70 //! supplied by the client or generated by the server. The server may
71 //! invalidate any access token previously associated with that device. See
72 //! [Relationship between access tokens and
73 //! devices](/client-server-api/#relationship-between-access-tokens-and-devices).
74 class QUOTIENT_API LoginJob : public BaseJob {
75 public:
76  //! \param type
77  //! The login type being used.
78  //!
79  //! This must be a type returned in one of the flows of the
80  //! response of the [`GET /login`](/client-server-api/#get_matrixclientv3login)
81  //! endpoint, like `m.login.password` or `m.login.token`.
82  //!
83  //!
84  //! \param password
85  //! Required when `type` is `m.login.password`. The user's
86  //! password.
87  //!
88  //! \param token
89  //! Required when `type` is `m.login.token`. Part of Token-based login.
90  //!
91  //! \param deviceId
92  //! ID of the client device. If this does not correspond to a
93  //! known client device, a new device will be created. The given
94  //! device ID must not be the same as a
95  //! [cross-signing](/client-server-api/#cross-signing) key ID.
96  //! The server will auto-generate a device_id
97  //! if this is not specified.
98  //!
99  //! \param initialDeviceDisplayName
100  //! A display name to assign to the newly-created device. Ignored
101  //! if `device_id` corresponds to a known device.
102  //!
103  //! \param refreshToken
104  //! If true, the client supports refresh tokens.
105  explicit LoginJob(const QString& type,
106  const std::optional<UserIdentifier>& identifier = std::nullopt,
107  const QString& password = {}, const QString& token = {},
108  const QString& deviceId = {}, const QString& initialDeviceDisplayName = {},
109  std::optional<bool> refreshToken = std::nullopt);
110 
111  // Result properties
112 
113  //! The fully-qualified Matrix ID for the account.
114  QString userId() const { return loadFromJson<QString>("user_id"_L1); }
115 
116  //! An access token for the account.
117  //! This access token can then be used to authorize other requests.
118  QString accessToken() const { return loadFromJson<QString>("access_token"_L1); }
119 
120  //! A refresh token for the account. This token can be used to
121  //! obtain a new access token when it expires by calling the
122  //! `/refresh` endpoint.
123  QString refreshToken() const { return loadFromJson<QString>("refresh_token"_L1); }
124 
125  //! The lifetime of the access token, in milliseconds. Once
126  //! the access token has expired a new access token can be
127  //! obtained by using the provided refresh token. If no
128  //! refresh token is provided, the client will need to re-log in
129  //! to obtain a new access token. If not given, the client can
130  //! assume that the access token will not expire.
131  std::optional<int> expiresInMs() const
132  {
133  return loadFromJson<std::optional<int>>("expires_in_ms"_L1);
134  }
135 
136  //! ID of the logged-in device. Will be the same as the
137  //! corresponding parameter in the request, if one was specified.
138  QString deviceId() const { return loadFromJson<QString>("device_id"_L1); }
139 
140  //! Optional client configuration provided by the server. If present,
141  //! clients SHOULD use the provided object to reconfigure themselves,
142  //! optionally validating the URLs within. This object takes the same
143  //! form as the one returned from .well-known autodiscovery.
144  std::optional<DiscoveryInformation> wellKnown() const
145  {
146  return loadFromJson<std::optional<DiscoveryInformation>>("well_known"_L1);
147  }
148 
149  struct Response {
150  //! The fully-qualified Matrix ID for the account.
151  QString userId{};
152 
153  //! An access token for the account.
154  //! This access token can then be used to authorize other requests.
155  QString accessToken{};
156 
157  //! A refresh token for the account. This token can be used to
158  //! obtain a new access token when it expires by calling the
159  //! `/refresh` endpoint.
160  QString refreshToken{};
161 
162  //! The lifetime of the access token, in milliseconds. Once
163  //! the access token has expired a new access token can be
164  //! obtained by using the provided refresh token. If no
165  //! refresh token is provided, the client will need to re-log in
166  //! to obtain a new access token. If not given, the client can
167  //! assume that the access token will not expire.
168  std::optional<int> expiresInMs{};
169 
170  //! ID of the logged-in device. Will be the same as the
171  //! corresponding parameter in the request, if one was specified.
172  QString deviceId{};
173 
174  //! Optional client configuration provided by the server. If present,
175  //! clients SHOULD use the provided object to reconfigure themselves,
176  //! optionally validating the URLs within. This object takes the same
177  //! form as the one returned from .well-known autodiscovery.
178  std::optional<DiscoveryInformation> wellKnown{};
179  };
180 };
181 
182 template <std::derived_from<LoginJob> JobT>
183 constexpr inline auto doCollectResponse<JobT> = [](JobT* j) -> LoginJob::Response {
184  return { j->userId(), j->accessToken(), j->refreshToken(),
185  j->expiresInMs(), j->deviceId(), j->wellKnown() };
186 };
187 
188 } // namespace Quotient