-
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.
Merge pull request #13 from nodes-ios/async_tokens
Make async token refresh mechanism
- Loading branch information
Showing
112 changed files
with
1,510 additions
and
2,266 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// APITokensProtocolConformance.swift | ||
// PROJECT_NAME | ||
// | ||
// Created by Jakob Mygind on 30/01/2023. | ||
// | ||
|
||
import MLTokenHandler | ||
import Model | ||
import Foundation | ||
|
||
extension APITokensEnvelope: APITokensEnvelopeProtocol { | ||
public var getAccessToken: String { | ||
token.rawValue | ||
} | ||
|
||
public var getRefreshToken: String { | ||
refreshToken.token.rawValue | ||
} | ||
} |
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,58 @@ | ||
// | ||
// EnvVars.swift | ||
// PROJECT_NAME | ||
// | ||
// Created by Jakob Mygind on 24/01/2023. | ||
// | ||
|
||
import Dependencies | ||
import Foundation | ||
import NStackSDK | ||
import PersistenceClient | ||
|
||
public struct EnvVars { | ||
|
||
public struct NStackVars { | ||
let appId: String | ||
let restAPIKey: String | ||
let environment: NStackSDK.Configuration.NStackEnvironment | ||
} | ||
|
||
var baseURL: URL | ||
var refreshURL: URL | ||
var persistenceKeyPrefix: String | ||
var nstackVars: NStackVars | ||
} | ||
|
||
extension EnvVars: DependencyKey { | ||
#warning("Set up environment variables here") | ||
public static var liveValue: EnvVars { | ||
.init( | ||
baseURL: Configuration.API.baseURL, | ||
refreshURL: unimplemented(), | ||
persistenceKeyPrefix: Bundle.main.bundleIdentifier!, | ||
nstackVars: .init( | ||
appId: unimplemented(), | ||
restAPIKey: unimplemented(), | ||
environment: currentNStackEnvironment() | ||
) | ||
) | ||
} | ||
|
||
static func currentNStackEnvironment() -> NStackSDK.Configuration.NStackEnvironment { | ||
#if RELEASE | ||
return .production | ||
#elseif TEST | ||
return .test | ||
#else | ||
return .debug | ||
#endif | ||
} | ||
} | ||
|
||
extension DependencyValues { | ||
public var envVars: EnvVars { | ||
get { self[EnvVars.self] } | ||
set { self[EnvVars.self] = newValue } | ||
} | ||
} |
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,82 @@ | ||
// | ||
// Environment.swift | ||
// PROJECT_NAME | ||
// | ||
// Created by Jakob Mygind on 24/01/2023. | ||
// | ||
|
||
import APIClient | ||
import APIClientLive | ||
import AppVersion | ||
import Dependencies | ||
import Foundation | ||
import Localizations | ||
import Model | ||
import NetworkClient | ||
import NStackSDK | ||
import PersistenceClient | ||
import MLTokenHandler | ||
|
||
extension APIClient: DependencyKey { | ||
public static var liveValue: APIClient { | ||
|
||
@Dependency(\.envVars) var envVars | ||
@Dependency(\.persistenceClient) var persistenceClient | ||
|
||
var continuation: AsyncStream<APITokensEnvelope?>.Continuation! | ||
|
||
let tokenValuesStream: AsyncStream<APITokensEnvelope?> = .init { cont in | ||
continuation = cont | ||
} | ||
let authHandler = AuthenticationHandlerAsync<APITokensEnvelope>( | ||
refreshURL: envVars.refreshURL, | ||
getTokens: persistenceClient.tokens.load, | ||
saveTokens: { tokens in | ||
persistenceClient.tokens.save(tokens) | ||
continuation.yield(tokens) | ||
} | ||
) | ||
|
||
return APIClient.live( | ||
baseURL: envVars.baseURL, | ||
authenticationHandler: authHandler, | ||
tokensUpdateStream: tokenValuesStream | ||
) | ||
} | ||
} | ||
|
||
extension PersistenceClient: DependencyKey { | ||
|
||
public static var liveValue: PersistenceClient { | ||
@Dependency(\.envVars) var envVars | ||
|
||
return .live(keyPrefix: envVars.persistenceKeyPrefix) | ||
} | ||
} | ||
|
||
extension AppVersion: DependencyKey { | ||
public static var liveValue: AppVersion { | ||
.live | ||
} | ||
} | ||
|
||
/// Handles setup of [NStack](https://nstack.io) services. | ||
/// This example demonstrates [Localization](https://nstack-io.github.io/docs/docs/features/localize.html) service activation. | ||
extension ObservableLocalizations: DependencyKey { | ||
public static var liveValue: ObservableLocalizations { | ||
|
||
@Dependency(\.envVars) var envVars | ||
return startNStackSDK( | ||
appId: envVars.nstackVars.appId, | ||
restAPIKey: envVars.nstackVars.restAPIKey, | ||
environment: envVars.nstackVars.environment | ||
) | ||
} | ||
} | ||
|
||
extension NetworkClient: DependencyKey { | ||
public static var liveValue: NetworkClient { | ||
.live(queue: .main) | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#include "Production.xcconfig" | ||
|
||
API_BASE_URL_DEV = some.apiurl.dev.com | ||
API_BASE_URL_STAGING = some.apiurl.dev.com | ||
|
||
DEFAULT_BASE_URL = API_BASE_URL_PROD |
77 changes: 77 additions & 0 deletions
77
Modules/.swiftpm/xcode/xcshareddata/xcschemes/LoginFeature.xcscheme
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,77 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "1420" | ||
version = "1.3"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES"> | ||
<BuildActionEntries> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "LoginFeature" | ||
BuildableName = "LoginFeature" | ||
BlueprintName = "LoginFeature" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
</BuildActionEntries> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "" | ||
selectedLauncherIdentifier = "Xcode.IDEFoundation.Launcher.PosixSpawn" | ||
shouldUseLaunchSchemeArgsEnv = "YES"> | ||
<Testables> | ||
<TestableReference | ||
skipped = "NO"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "LoginFeatureTests" | ||
BuildableName = "LoginFeatureTests" | ||
BlueprintName = "LoginFeatureTests" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</TestableReference> | ||
</Testables> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
<MacroExpansion> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "LoginFeature" | ||
BuildableName = "LoginFeature" | ||
BlueprintName = "LoginFeature" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</MacroExpansion> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
Oops, something went wrong.