aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFinn Behrens <me@kloenk.dev>2021-09-29 11:09:34 +0200
committerFinn Behrens <me@kloenk.dev>2021-09-29 11:09:34 +0200
commit339d7b50a9f5e248b124690e61e1673f9726df89 (patch)
treedd58e9d6532223fbc3cfabd136734969eb7ce912
parente03e5d13c189dbf0ff8bbadcce9a8af0914a5b44 (diff)
downloadMatrixCore-339d7b50a9f5e248b124690e61e1673f9726df89.tar.gz
MatrixCore-339d7b50a9f5e248b124690e61e1673f9726df89.tar.xz
MatrixCore-339d7b50a9f5e248b124690e61e1673f9726df89.zip
add login
-rw-r--r--Sources/MatrixClient/API/Login.swift18
-rw-r--r--Sources/MatrixClient/MatrixClient.swift43
2 files changed, 59 insertions, 2 deletions
diff --git a/Sources/MatrixClient/API/Login.swift b/Sources/MatrixClient/API/Login.swift
index 624dcdb..a8dc015 100644
--- a/Sources/MatrixClient/API/Login.swift
+++ b/Sources/MatrixClient/API/Login.swift
@@ -191,7 +191,7 @@ public enum MatrixLoginUserIdentifier: Codable {
}
}
-public struct MatrixLoginRequest: Codable {
+public struct MatrixLoginRequest {
/// The login type being used. One of: ["m.login.password", "m.login.token"]
public var type: String
@@ -236,7 +236,21 @@ public struct MatrixLoginRequest: Codable {
}
}
-public struct MatrixLoginResponse: Codable {
+extension MatrixLoginRequest: MatrixRequest {
+ public typealias Response = MatrixLogin
+
+ public typealias URLParameters = ()
+
+ public func path(with parameters: ()) -> String {
+ return "/_matrix/client/r0/login"
+ }
+
+ public static var httpMethod = HttpMethode.POST
+
+ public static var requiresAuth = false
+}
+
+public struct MatrixLogin: MatrixResponse {
/// The fully-qualified Matrix ID that has been registered.
public var userId: String?
diff --git a/Sources/MatrixClient/MatrixClient.swift b/Sources/MatrixClient/MatrixClient.swift
index f11d328..6a46c47 100644
--- a/Sources/MatrixClient/MatrixClient.swift
+++ b/Sources/MatrixClient/MatrixClient.swift
@@ -60,6 +60,49 @@ public struct MatrixClient {
return flows.flows.map({$0.type})
}
+
+ /// Authenticates the user, and issues an access token they can use to authorize themself in subsequent requests.
+ ///
+ /// If the client does not supply a device_id, the server must auto-generate one.
+ ///
+ /// The returned access token must be associated with the device_id supplied by the client or generated by the server. The server may invalidate
+ /// any access token previously associated with that device. See [Relationship between access tokens and devices](https://matrix.org/docs/spec/client_server/latest#relationship-between-access-tokens-and-devices).
+ ///
+ ///```markdown
+ /// Rate-limited: Yes.
+ /// Requires auth: No.
+ ///```
+ public func login(token: Bool = false, username: String, password: String, displayName: String? = nil, deviceId: String? = nil) async throws -> MatrixLogin {
+ let flow: MatrixLoginFlow
+ if token {
+ flow = .token
+ } else {
+ flow = .password
+ }
+ var request = MatrixLoginRequest(type: flow.value, identifier: MatrixLoginUserIdentifier.user(id: username), deviceId: deviceId, initialDeviceDisplayName: displayName)
+ if token {
+ request.token = password
+ } else {
+ request.password = password
+ }
+
+ return try await login(request: request)
+ }
+
+ /// Authenticates the user, and issues an access token they can use to authorize themself in subsequent requests.
+ ///
+ /// If the client does not supply a device_id, the server must auto-generate one.
+ ///
+ /// The returned access token must be associated with the device_id supplied by the client or generated by the server. The server may invalidate
+ /// any access token previously associated with that device. See [Relationship between access tokens and devices](https://matrix.org/docs/spec/client_server/latest#relationship-between-access-tokens-and-devices).
+ ///
+ ///```markdown
+ /// Rate-limited: Yes.
+ /// Requires auth: No.
+ ///```
+ public func login(request: MatrixLoginRequest) async throws -> MatrixLogin {
+ return try await request.repsonse(on: homeserver, with: (), withUrlSession: urlSession)
+ }
}