Skip to content

Commit ee8fec2

Browse files
committed
fix(auth): add protected data unavailable error
1 parent 540f1f2 commit ee8fec2

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin+Configure.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import AWSClientRuntime
1616
@_spi(InternalHttpEngineProxy) import AWSPluginsCore
1717
import SmithyRetriesAPI
1818
import SmithyRetries
19+
#if canImport(UIKit)
20+
import UIKit
21+
#endif
1922

2023
extension AWSCognitoAuthPlugin {
2124

@@ -38,6 +41,15 @@ extension AWSCognitoAuthPlugin {
3841
AuthPluginErrorConstants.decodeConfigurationError.recoverySuggestion)
3942
}
4043

44+
#if canImport(UIKit)
45+
guard UIApplication.shared.isProtectedDataAvailable else {
46+
throw PluginError.pluginConfigurationError(
47+
AuthPluginErrorConstants.protectedDataUnavailableError.errorDescription,
48+
AuthPluginErrorConstants.protectedDataUnavailableError.recoverySuggestion,
49+
AWSCognitoAuthError.protectedDataUnavailable)
50+
}
51+
#endif
52+
4153
let credentialStoreResolver = CredentialStoreState.Resolver().eraseToAnyResolver()
4254
let credentialEnvironment = credentialStoreEnvironment(authConfiguration: authConfiguration)
4355
let credentialStoreMachine = StateMachine(resolver: credentialStoreResolver,

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Models/Errors/AWSCognitoAuthError.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ public enum AWSCognitoAuthError: Error {
111111

112112
/// The WebAuthn configuration is missing or incomplete
113113
case webAuthnConfigurationMissing
114+
115+
/// Protected data is not yet available (iOS prewarming or locked state)
116+
case protectedDataUnavailable
114117
}
115118

116119
extension AWSCognitoAuthError: LocalizedError {
@@ -185,6 +188,8 @@ extension AWSCognitoAuthError: LocalizedError {
185188
message = "The relying party ID doesn't match."
186189
case .webAuthnConfigurationMissing:
187190
message = "The WebAuthn configuration is missing or incomplete."
191+
case .protectedDataUnavailable:
192+
message = "Protected data is not yet available."
188193
}
189194
return "\(String(describing: Self.self)).\(self): \(message)"
190195
}

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/Constants/AuthPluginErrorConstants.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ typealias AuthPluginValidationErrorString = (field: Field,
1616

1717
enum AuthPluginErrorConstants {
1818

19+
static let protectedDataUnavailableError: AuthPluginErrorString = (
20+
"Protected data is not yet available",
21+
"Delay Amplify calls until `UIApplication.shared.isProtectedDataAvailable` returns true or listen to `UIApplication.protectedDataDidBecomeAvailableNotification`"
22+
)
23+
1924
static let decodeConfigurationError: AuthPluginErrorString = (
2025
"Unable to decode configuration",
2126
"Make sure the plugin configuration is JSONValue")

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Task/Protocols/AmplifyAuthTask.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import Foundation
88
import Amplify
99

10+
#if canImport(UIKit)
11+
import UIKit
12+
#endif
13+
1014
protocol AmplifyAuthTask {
1115

1216
associatedtype Success
@@ -30,6 +34,14 @@ extension AmplifyAuthTask where Self: DefaultLogger {
3034
get async throws {
3135
do {
3236
log.info("Starting execution for \(eventName)")
37+
#if canImport(UIKit)
38+
guard await UIApplication.shared.isProtectedDataAvailable else {
39+
throw AuthError.configuration(
40+
AuthPluginErrorConstants.protectedDataUnavailableError.errorDescription,
41+
AuthPluginErrorConstants.protectedDataUnavailableError.recoverySuggestion,
42+
AWSCognitoAuthError.protectedDataUnavailable)
43+
}
44+
#endif
3345
let valueReturned = try await execute()
3446
log.info("Successfully completed execution for \(eventName) with result:\n\(valueReturned)")
3547
dispatch(result: .success(valueReturned))

0 commit comments

Comments
 (0)