Skip to content

Commit c5fb63a

Browse files
committed
pr review feedback
1 parent 11d7fd5 commit c5fb63a

File tree

4 files changed

+62
-56
lines changed

4 files changed

+62
-56
lines changed

AuthenticationExample/AuthenticationExample/AuthenticationApp.swift

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ struct AuthenticationApp: App {
2626
// If you want to use OAuth, uncomment this code:
2727
//oAuthUserConfigurations: [.arcgisDotCom]
2828
)
29-
// Set the ArcGIS and Network challenge handlers to be the authenticator we just created.
30-
authenticator.setupAuthenticationChallengeHandlers()
29+
// Sets authenticator as ArcGIS and Network challenge handlers to handle authentication
30+
// challenges.
31+
ArcGISEnvironment.authenticationManager.handleAuthenticationChallenges(using: authenticator)
3132
}
3233

3334
var body: some SwiftUI.Scene {
@@ -54,7 +55,7 @@ struct AuthenticationApp: App {
5455
// It also means that a user can sign in without having to be prompted for
5556
// credentials. Once credentials are cleared from the stores ("sign-out"),
5657
// then the user will need to be prompted once again.
57-
try? await authenticator.setupPersistentCredentialStorage(access: .whenUnlockedThisDeviceOnly)
58+
try? await ArcGISEnvironment.authenticationManager.setupPersistentCredentialStorage(access: .whenUnlockedThisDeviceOnly)
5859
isSettingUp = false
5960
}
6061
}

AuthenticationExample/AuthenticationExample/ProfileView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct ProfileView: View {
6161
func signOut() {
6262
isSigningOut = true
6363
Task {
64-
await authenticator.clearCredentialStores()
64+
await ArcGISEnvironment.authenticationManager.clearCredentialStores()
6565
isSigningOut = false
6666
signOutAction()
6767
}

Documentation/Authenticator/README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The `Authenticator` can be configured to support securely persisting credentials
2323
@ViewBuilder func authenticator(_ authenticator: Authenticator) -> some View
2424
```
2525

26-
To securely store credentials in the keychain, use the following instance method on `Authenticator`:
26+
To securely store credentials in the keychain, use the following extension method of `AuthenticationManager`:
2727

2828
```swift
2929
/// Sets up new credential stores that will be persisted to the keychain.
@@ -56,8 +56,9 @@ init() {
5656
// If you want to use OAuth, uncomment this code:
5757
//oAuthConfigurations: [.arcgisDotCom]
5858
)
59-
// Set the ArcGIS and Network challenge handlers to be the authenticator we just created.
60-
authenticator.setupAuthenticationChallengeHandlers()
59+
// Sets authenticator as ArcGIS and Network challenge handlers to handle authentication
60+
// challenges.
61+
ArcGISEnvironment.authenticationManager.handleAuthenticationChallenges(using: authenticator)
6162
}
6263

6364
var body: some SwiftUI.Scene {

Sources/ArcGISToolkit/Components/Authentication/Authenticator.swift

+53-49
Original file line numberDiff line numberDiff line change
@@ -37,55 +37,6 @@ public final class Authenticator: ObservableObject {
3737
self.oAuthUserConfigurations = oAuthUserConfigurations
3838
}
3939

40-
/// Sets the ArcGIS and Network challenge handlers to be this authenticator.
41-
public func setupAuthenticationChallengeHandlers() {
42-
ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler = self
43-
ArcGISEnvironment.authenticationManager.networkAuthenticationChallengeHandler = self
44-
}
45-
46-
/// Sets up new credential stores that will be persisted to the keychain.
47-
/// - Remark: The credentials will be stored in the default access group of the keychain.
48-
/// You can find more information about what the default group would be here:
49-
/// https://developer.apple.com/documentation/security/keychain_services/keychain_items/sharing_access_to_keychain_items_among_a_collection_of_apps
50-
/// - Parameters:
51-
/// - access: When the credentials stored in the keychain can be accessed.
52-
/// - synchronizesWithiCloud: A Boolean value indicating whether the credentials are synchronized with iCloud.
53-
public func setupPersistentCredentialStorage(
54-
access: ArcGIS.KeychainAccess,
55-
synchronizesWithiCloud: Bool = false
56-
) async throws {
57-
let previousArcGISCredentialStore = ArcGISEnvironment.authenticationManager.arcGISCredentialStore
58-
59-
// Set a persistent ArcGIS credential store on the ArcGIS environment.
60-
ArcGISEnvironment.authenticationManager.arcGISCredentialStore = try await .makePersistent(
61-
access: access,
62-
synchronizesWithiCloud: synchronizesWithiCloud
63-
)
64-
65-
do {
66-
// Set a persistent network credential store on the ArcGIS environment.
67-
await ArcGISEnvironment.authenticationManager.setNetworkCredentialStore(
68-
try await .makePersistent(access: access, synchronizesWithiCloud: synchronizesWithiCloud)
69-
)
70-
} catch {
71-
// If making the shared network credential store persistent fails,
72-
// then restore the ArcGIS credential store.
73-
ArcGISEnvironment.authenticationManager.arcGISCredentialStore = previousArcGISCredentialStore
74-
throw error
75-
}
76-
}
77-
78-
/// Clears all ArcGIS and network credentials from the respective stores.
79-
/// Note: This sets up new `URLSessions` so that removed network credentials are respected
80-
/// right away.
81-
public func clearCredentialStores() async {
82-
// Clear ArcGIS Credentials.
83-
ArcGISEnvironment.authenticationManager.arcGISCredentialStore.removeAll()
84-
85-
// Clear network credentials.
86-
await ArcGISEnvironment.authenticationManager.networkCredentialStore.removeAll()
87-
}
88-
8940
/// The current challenge.
9041
/// This property is not set for OAuth challenges.
9142
@Published var currentChallenge: ChallengeContinuation?
@@ -137,3 +88,56 @@ extension Authenticator: NetworkAuthenticationChallengeHandler {
13788
return await challengeContinuation.value
13889
}
13990
}
91+
92+
public extension AuthenticationManager {
93+
/// Sets authenticator as ArcGIS and Network challenge handlers to handle authentication
94+
/// challenges.
95+
/// - Parameter authenticator: The authenticator to be used for handling challenges.
96+
func handleAuthenticationChallenges(using authenticator: Authenticator) {
97+
ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler = authenticator
98+
ArcGISEnvironment.authenticationManager.networkAuthenticationChallengeHandler = authenticator
99+
}
100+
101+
/// Sets up new credential stores that will be persisted to the keychain.
102+
/// - Remark: The credentials will be stored in the default access group of the keychain.
103+
/// You can find more information about what the default group would be here:
104+
/// https://developer.apple.com/documentation/security/keychain_services/keychain_items/sharing_access_to_keychain_items_among_a_collection_of_apps
105+
/// - Parameters:
106+
/// - access: When the credentials stored in the keychain can be accessed.
107+
/// - synchronizesWithiCloud: A Boolean value indicating whether the credentials are synchronized with iCloud.
108+
func setupPersistentCredentialStorage(
109+
access: ArcGIS.KeychainAccess,
110+
synchronizesWithiCloud: Bool = false
111+
) async throws {
112+
let previousArcGISCredentialStore = ArcGISEnvironment.authenticationManager.arcGISCredentialStore
113+
114+
// Set a persistent ArcGIS credential store on the ArcGIS environment.
115+
ArcGISEnvironment.authenticationManager.arcGISCredentialStore = try await .makePersistent(
116+
access: access,
117+
synchronizesWithiCloud: synchronizesWithiCloud
118+
)
119+
120+
do {
121+
// Set a persistent network credential store on the ArcGIS environment.
122+
await ArcGISEnvironment.authenticationManager.setNetworkCredentialStore(
123+
try await .makePersistent(access: access, synchronizesWithiCloud: synchronizesWithiCloud)
124+
)
125+
} catch {
126+
// If making the shared network credential store persistent fails,
127+
// then restore the ArcGIS credential store.
128+
ArcGISEnvironment.authenticationManager.arcGISCredentialStore = previousArcGISCredentialStore
129+
throw error
130+
}
131+
}
132+
133+
/// Clears all ArcGIS and network credentials from the respective stores.
134+
/// Note: This sets up new `URLSessions` so that removed network credentials are respected
135+
/// right away.
136+
func clearCredentialStores() async {
137+
// Clear ArcGIS Credentials.
138+
ArcGISEnvironment.authenticationManager.arcGISCredentialStore.removeAll()
139+
140+
// Clear network credentials.
141+
await ArcGISEnvironment.authenticationManager.networkCredentialStore.removeAll()
142+
}
143+
}

0 commit comments

Comments
 (0)