-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into fix/cache-key-for-tests
- Loading branch information
Showing
150 changed files
with
2,902 additions
and
443 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
WireAPI/Sources/WireAPI/Authentication/AuthenticationManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// | ||
// Wire | ||
// Copyright (C) 2024 Wire Swiss GmbH | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see http://www.gnu.org/licenses/. | ||
// | ||
|
||
import Foundation | ||
import WireFoundation | ||
|
||
// sourcery: AutoMockable | ||
protocol AuthenticationManagerProtocol { | ||
|
||
func getValidAccessToken() async throws -> AccessToken | ||
func refreshAccessToken() async throws -> AccessToken | ||
|
||
} | ||
|
||
actor AuthenticationManager: AuthenticationManagerProtocol { | ||
|
||
enum Failure: Error, Equatable { | ||
|
||
case invalidCredentials | ||
|
||
} | ||
|
||
private enum CurrentToken { | ||
|
||
case cached(AccessToken) | ||
case renewing(Task<AccessToken, any Error>) | ||
|
||
} | ||
|
||
private var currentToken: CurrentToken? | ||
private let clientID: String | ||
private let cookieStorage: any CookieStorageProtocol | ||
private let networkService: NetworkService | ||
|
||
init( | ||
clientID: String, | ||
cookieStorage: any CookieStorageProtocol, | ||
networkService: NetworkService | ||
) { | ||
self.clientID = clientID | ||
self.cookieStorage = cookieStorage | ||
self.networkService = networkService | ||
} | ||
|
||
/// Get a valid access token to make authenticated requests. | ||
/// | ||
/// If a valid token exists in the cache then it will be returned, | ||
/// otherwise a new token will be retrieved from the backend. | ||
/// | ||
/// - Returns: A valid (non-expired) access token. | ||
|
||
func getValidAccessToken() async throws -> AccessToken { | ||
switch currentToken { | ||
case let .renewing(task): | ||
// A new token will come soon, wait | ||
try await task.value | ||
|
||
case let .cached(accessToken) where !accessToken.isExpiring: | ||
// This one is still good. | ||
accessToken | ||
|
||
default: | ||
// Time for a new token. | ||
try await refreshAccessToken() | ||
} | ||
} | ||
|
||
/// Get a new access token from the backend. | ||
/// | ||
/// This method will fetch a new access token from the backend | ||
/// and then store it in the cache. Only a single request is made | ||
/// at a time, and repeated calls will await the result of any | ||
/// in-flight requests. | ||
/// | ||
/// - Returns: A new access token. | ||
|
||
func refreshAccessToken() async throws -> AccessToken { | ||
if case let .renewing(task) = currentToken { | ||
// A new token will come soon, wait | ||
return try await task.value | ||
} | ||
|
||
var lastKnownToken: AccessToken? | ||
if case let .cached(token) = currentToken { | ||
lastKnownToken = token | ||
} | ||
|
||
let task = makeRenewTokenTask(lastKnownToken: lastKnownToken) | ||
currentToken = .renewing(task) | ||
|
||
do { | ||
let newToken = try await task.value | ||
currentToken = .cached(newToken) | ||
return newToken | ||
} catch { | ||
currentToken = nil | ||
throw error | ||
} | ||
} | ||
|
||
private func makeRenewTokenTask( | ||
lastKnownToken: AccessToken? | ||
) -> Task<AccessToken, any Error> { | ||
Task { | ||
let cookies = try await cookieStorage.fetchCookies() | ||
|
||
var request = try URLRequestBuilder(path: "/access") | ||
.withQueryItem(name: "client_id", value: clientID) | ||
.withMethod(.post) | ||
.withAcceptType(.json) | ||
.withCookies(cookies) | ||
.build() | ||
|
||
if let lastKnownToken { | ||
request.setAccessToken(lastKnownToken) | ||
} | ||
|
||
let (data, response) = try await networkService.executeRequest(request) | ||
|
||
let decoder = JSONDecoder() | ||
decoder.keyDecodingStrategy = .convertFromSnakeCase | ||
|
||
return try ResponseParser(decoder: decoder) | ||
.success(code: .ok, type: AccessTokenPayload.self) | ||
.failure(code: .forbidden, label: "invalid-credentials", error: Failure.invalidCredentials) | ||
.parse(code: response.statusCode, data: data) | ||
} | ||
} | ||
|
||
} | ||
|
||
extension AccessToken { | ||
|
||
var isExpiring: Bool { | ||
let secondsRemaining = expirationDate.timeIntervalSinceNow | ||
return secondsRemaining < 40 | ||
} | ||
|
||
} | ||
|
||
private struct AccessTokenPayload: Decodable, ToAPIModelConvertible { | ||
|
||
let user: UUID | ||
let accessToken: String | ||
let tokenType: String | ||
let expiresIn: Int | ||
|
||
func toAPIModel() -> AccessToken { | ||
AccessToken( | ||
userID: user, | ||
token: accessToken, | ||
type: tokenType, | ||
expirationDate: Date(timeIntervalSinceNow: TimeInterval(expiresIn)) | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.