aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFinn Behrens <me@kloenk.dev>2021-09-30 09:53:27 +0200
committerFinn Behrens <me@kloenk.dev>2021-09-30 09:53:27 +0200
commit8b57b61e66ab5ce51698c74fa3bf7b69423a3741 (patch)
treef1880d7b486283e2a5751f1b26c299e17cfe6828
parent339d7b50a9f5e248b124690e61e1673f9726df89 (diff)
downloadMatrixCore-8b57b61e66ab5ce51698c74fa3bf7b69423a3741.tar.gz
MatrixCore-8b57b61e66ab5ce51698c74fa3bf7b69423a3741.tar.xz
MatrixCore-8b57b61e66ab5ce51698c74fa3bf7b69423a3741.zip
add logout
-rw-r--r--Sources/MatrixClient/API/Logout.swift30
-rw-r--r--Sources/MatrixClient/API/Request.swift2
-rw-r--r--Sources/MatrixClient/MatrixClient.swift30
3 files changed, 62 insertions, 0 deletions
diff --git a/Sources/MatrixClient/API/Logout.swift b/Sources/MatrixClient/API/Logout.swift
new file mode 100644
index 0000000..a7ef3b1
--- /dev/null
+++ b/Sources/MatrixClient/API/Logout.swift
@@ -0,0 +1,30 @@
+//
+// File.swift
+// File
+//
+// Created by Finn Behrens on 30.09.21.
+//
+
+import Foundation
+
+public struct MatrixLogoutRequest: MatrixRequest {
+ public typealias Response = MatrixLogout
+
+ public typealias URLParameters = Bool
+
+ public func path(with all: Bool) -> String {
+ if all {
+ return "/_matrix/client/r0/logout/all"
+ } else {
+ return "/_matrix/client/r0/logout"
+ }
+ }
+
+ public static var httpMethod = HttpMethode.POST
+
+ public static var requiresAuth = true
+}
+
+public struct MatrixLogout: MatrixResponse {
+
+}
diff --git a/Sources/MatrixClient/API/Request.swift b/Sources/MatrixClient/API/Request.swift
index 3d60af1..7009cc2 100644
--- a/Sources/MatrixClient/API/Request.swift
+++ b/Sources/MatrixClient/API/Request.swift
@@ -69,8 +69,10 @@ public protocol MatrixResponse: Codable {
}
public extension MatrixResponse {
+ /// Init from http json data
init(fromMatrixRequestData data: Data) throws {
let decoder = JSONDecoder()
self = try decoder.decode(Self.self, from: data)
}
}
+
diff --git a/Sources/MatrixClient/MatrixClient.swift b/Sources/MatrixClient/MatrixClient.swift
index 6a46c47..440057b 100644
--- a/Sources/MatrixClient/MatrixClient.swift
+++ b/Sources/MatrixClient/MatrixClient.swift
@@ -103,6 +103,36 @@ public struct MatrixClient {
public func login(request: MatrixLoginRequest) async throws -> MatrixLogin {
return try await request.repsonse(on: homeserver, with: (), withUrlSession: urlSession)
}
+
+ // 5.5.3 POST /_matrix/client/r0/logout
+ /// Invalidates an existing access token, so that it can no longer be used for authorization. The device associated with the
+ /// access token is also deleted. [Device keys](https://matrix.org/docs/spec/client_server/latest#device-keys) for the device
+ /// are deleted alongside the device.
+ ///
+ ///
+ ///```markdown
+ /// Rate-limited: No.
+ /// Requires auth: Yes.
+ ///```
+ public func logout() async throws {
+ try await MatrixLogoutRequest().repsonse(on: homeserver, with: false, withUrlSession: urlSession)
+ }
+
+ /// Invalidates all access tokens for a user, so that they can no longer be used for authorization. This includes the access token that made this request.
+ /// All devices for the user are also deleted. [Device keys](https://matrix.org/docs/spec/client_server/latest#device-keys) for
+ /// the device are deleted alongside the device.
+ ///
+ /// This endpoint does not require UI authorization because UI authorization is designed to protect against attacks where the someone gets hold of a
+ /// single access token then takes over the account. This endpoint invalidates all access tokens for the user, including the token used in the request,
+ /// and therefore the attacker is unable to take over the account in this way.
+ ///
+ ///```markdown
+ /// Rate-limited: No.
+ /// Requires auth: Yes.
+ ///```
+ public func logoutAll() async throws {
+ try await MatrixLogoutRequest().repsonse(on: homeserver, with: true, withUrlSession: urlSession)
+ }
}