-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef0efed
commit 63cd56e
Showing
12 changed files
with
167 additions
and
51 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
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,24 @@ | ||
import Foundation | ||
import HTTPTypes | ||
import OpenAPIRuntime | ||
|
||
struct AuthenticationMiddleware: ClientMiddleware { | ||
private let keychain = Keychain(service: "connection.token") | ||
|
||
func intercept( | ||
_ request: HTTPRequest, | ||
body: HTTPBody?, | ||
baseURL: URL, | ||
operationID _: String, | ||
next: @Sendable (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?) | ||
) async throws -> (HTTPResponse, HTTPBody?) { | ||
var request = request | ||
let token = keychain.get() | ||
|
||
if !token.isEmpty { | ||
request.headerFields[.authorization] = "Bearer \(token)" | ||
} | ||
|
||
return try await next(request, body, baseURL) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import BackgroundTasks | ||
|
||
let tokenRefreshDelaySeconds: Double = 60 * 60 * 24 | ||
|
||
@available(macOS, unavailable) | ||
func tokenRefreshBackgroundTask() async { | ||
let keychain = Keychain(service: "connection.token") | ||
guard !keychain.get().isEmpty else { return } | ||
scheduleTokenRefresh() | ||
|
||
let environment: ServerEnvironment | ||
|
||
if let environmentString = UserDefaults.standard.string(forKey: "connection.environment") { | ||
environment = .init(rawValue: environmentString)! | ||
} else { | ||
environment = .default | ||
} | ||
|
||
if let newToken = await refreshToken(using: Config.default.app.api.client(for: environment)) { | ||
_ = await keychain.set(newToken) | ||
} | ||
} | ||
|
||
@available(macOS, unavailable) | ||
func scheduleTokenRefresh() { | ||
let request = BGAppRefreshTaskRequest(identifier: "app.fyreplace.Fyreplace.tokenRefresh") | ||
request.earliestBeginDate = .init(timeIntervalSinceNow: tokenRefreshDelaySeconds) | ||
try? BGTaskScheduler.shared.submit(request) | ||
} | ||
|
||
func refreshToken(using client: APIProtocol) async -> String? { | ||
guard let response = try? await client.getNewToken().ok, | ||
let newToken = try? await String(collecting: response.body.plainText, upTo: 1024) | ||
else { return nil } | ||
return newToken | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import SwiftUI | ||
|
||
struct EnvironmentView: View { | ||
let eventBus: EventBus | ||
|
||
#if os(macOS) | ||
@Environment(\.controlActiveState) | ||
private var status | ||
#else | ||
@Environment(\.scenePhase) | ||
private var status | ||
#endif | ||
|
||
@Environment(\.config) | ||
private var config | ||
|
||
@AppStorage("connection.environment") | ||
private var environment = ServerEnvironment.default | ||
|
||
var body: some View { | ||
MainView() | ||
.environment(\.isInForeground, status != .inactive) | ||
.environment(\.api, config.app.api.client(for: environment)) | ||
.environmentObject(eventBus) | ||
} | ||
} |
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
Oops, something went wrong.