Skip to content

Commit cb80b91

Browse files
authored
fix(auth): clear credentials values only if namespacing has changed (#3827)
* fix(auth): clear credentials values only if namespacing has changed * fix session errors
1 parent 610dccd commit cb80b91

File tree

3 files changed

+34
-70
lines changed

3 files changed

+34
-70
lines changed

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/CredentialStorage/AWSCognitoAuthCredentialStore.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ struct AWSCognitoAuthCredentialStore {
6565
newIdentityConfigData != nil &&
6666
oldIdentityPoolConfiguration == newIdentityConfigData
6767
{
68-
6968
// retrieve data from the old namespace and save with the new namespace
7069
if let oldCognitoCredentialsData = try? keychain._getData(oldNameSpace) {
7170
try? keychain._set(oldCognitoCredentialsData, key: newNameSpace)
7271
}
73-
} else if oldAuthConfigData != currentAuthConfig {
72+
} else if oldAuthConfigData != currentAuthConfig &&
73+
oldNameSpace != newNameSpace {
7474
// Clear the old credentials
7575
try? keychain._remove(oldNameSpace)
7676
}

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift

Lines changed: 23 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import Foundation
99
import Amplify
1010

11-
class FetchAuthSessionOperationHelper: DefaultLogger {
11+
class FetchAuthSessionOperationHelper {
1212

1313
typealias FetchAuthSessionCompletion = (Result<AuthSession, AuthError>) -> Void
1414

@@ -108,85 +108,41 @@ class FetchAuthSessionOperationHelper: DefaultLogger {
108108
"Auth plugin is in an invalid state")
109109
}
110110

111-
func sessionResultWithError(_ error: AuthorizationError,
112-
authenticationState: AuthenticationState)
113-
throws -> AuthSession {
114-
log.verbose("Received error - \(error)")
111+
func sessionResultWithError(
112+
_ error: AuthorizationError,
113+
authenticationState: AuthenticationState
114+
) throws -> AuthSession {
115+
log.verbose("Received fetch auth session error - \(error)")
115116

116117
var isSignedIn = false
117-
if case .signedIn = authenticationState {
118-
isSignedIn = true
119-
}
120-
switch error {
121-
case .sessionError(let fetchError, let credentials):
122-
return try sessionResultWithFetchError(fetchError,
123-
authenticationState: authenticationState,
124-
existingCredentials: credentials)
125-
case .sessionExpired(let error):
126-
let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession(
127-
underlyingError: error)
128-
return session
129-
default:
130-
let message = "Unknown error occurred"
131-
let error = AuthError.unknown(message)
132-
let session = AWSAuthCognitoSession(isSignedIn: isSignedIn,
133-
identityIdResult: .failure(error),
134-
awsCredentialsResult: .failure(error),
135-
cognitoTokensResult: .failure(error))
136-
return session
137-
}
138-
}
139-
140-
func sessionResultWithFetchError(_ error: FetchSessionError,
141-
authenticationState: AuthenticationState,
142-
existingCredentials: AmplifyCredentials)
143-
throws -> AuthSession {
118+
var authError: AuthError = error.authError
144119

145-
var isSignedIn = false
146120
if case .signedIn = authenticationState {
147121
isSignedIn = true
148122
}
149123

150124
switch error {
151-
152-
case .notAuthorized, .noCredentialsToRefresh:
153-
if !isSignedIn {
125+
case .sessionError(let fetchError, _):
126+
if (fetchError == .notAuthorized || fetchError == .noCredentialsToRefresh) && !isSignedIn {
154127
return AuthCognitoSignedOutSessionHelper.makeSessionWithNoGuestAccess()
155-
}
156-
157-
case .service(let error):
158-
var authError: AuthError
159-
if let convertedAuthError = (error as? AuthErrorConvertible)?.authError {
160-
authError = convertedAuthError
161128
} else {
162-
authError = AuthError.service(
163-
"Unknown service error occurred",
164-
"See the attached error for more details",
165-
error)
129+
authError = fetchError.authError
166130
}
167-
let session = AWSAuthCognitoSession(
168-
isSignedIn: isSignedIn,
169-
identityIdResult: .failure(authError),
170-
awsCredentialsResult: .failure(authError),
171-
cognitoTokensResult: .failure(authError))
131+
case .sessionExpired(let error):
132+
let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession(
133+
underlyingError: error)
172134
return session
173-
default: break
174-
135+
default:
136+
break
175137
}
176-
let message = "Unknown error occurred"
177-
let error = AuthError.unknown(message)
178-
let session = AWSAuthCognitoSession(isSignedIn: isSignedIn,
179-
identityIdResult: .failure(error),
180-
awsCredentialsResult: .failure(error),
181-
cognitoTokensResult: .failure(error))
182-
return session
183-
}
184138

185-
public static var log: Logger {
186-
Amplify.Logging.logger(forCategory: CategoryType.auth.displayName, forNamespace: String(describing: self))
187-
}
188-
189-
public var log: Logger {
190-
Self.log
139+
let session = AWSAuthCognitoSession(
140+
isSignedIn: isSignedIn,
141+
identityIdResult: .failure(authError),
142+
awsCredentialsResult: .failure(authError),
143+
cognitoTokensResult: .failure(authError))
144+
return session
191145
}
192146
}
147+
148+
extension FetchAuthSessionOperationHelper: DefaultLogger { }

AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/CredentialStore/CredentialStoreConfigurationTests.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,16 @@ class CredentialStoreConfigurationTests: AWSAuthBaseTest {
8989
XCTFail("Unable to save credentials")
9090
}
9191

92+
// When configuration changed
93+
let updatedConfig = AuthConfiguration.userPoolsAndIdentityPools(
94+
UserPoolConfigurationData(poolId: Defaults.userPoolId,
95+
clientId: Defaults.appClientId,
96+
region: Defaults.regionString,
97+
clientSecret: Defaults.appClientSecret,
98+
pinpointAppId: "somethingNew"),
99+
Defaults.makeIdentityConfigData())
92100
// When configuration don't change changed
93-
let newCredentialStore = AWSCognitoAuthCredentialStore(authConfiguration: initialAuthConfig)
101+
let newCredentialStore = AWSCognitoAuthCredentialStore(authConfiguration: updatedConfig)
94102

95103
// Then
96104
guard let credentials = try? newCredentialStore.retrieveCredential(),

0 commit comments

Comments
 (0)