Skip to content

Commit

Permalink
fix: update sdk error handling in AWSCognitoAuthPlugin (#3251)
Browse files Browse the repository at this point in the history
  • Loading branch information
atierian authored Sep 29, 2023
1 parent 50a8e36 commit f15e978
Show file tree
Hide file tree
Showing 49 changed files with 561 additions and 1,877 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ extension AWSCognitoAuthPlugin {
private func makeUserPool() throws -> CognitoUserPoolBehavior {
switch authConfiguration {
case .userPools(let userPoolConfig), .userPoolsAndIdentityPools(let userPoolConfig, _):
// TODO: FrameworkMetadata Replacement
let configuration = try CognitoIdentityProviderClient.CognitoIdentityProviderClientConfiguration(
endpointResolver: userPoolConfig.endpoint?.resolver,
frameworkMetadata: AmplifyAWSServiceConfiguration.frameworkMetaData(),
region: userPoolConfig.region
region: userPoolConfig.region,
serviceSpecific: .init(endpointResolver: userPoolConfig.endpoint?.resolver)
)

if var httpClientEngineProxy = httpClientEngineProxy {
Expand Down Expand Up @@ -121,8 +121,8 @@ extension AWSCognitoAuthPlugin {
private func makeIdentityClient() throws -> CognitoIdentityBehavior {
switch authConfiguration {
case .identityPools(let identityPoolConfig), .userPoolsAndIdentityPools(_, let identityPoolConfig):
// TODO: FrameworkMetadata Replacement
let configuration = try CognitoIdentityClient.CognitoIdentityClientConfiguration(
frameworkMetadata: AmplifyAWSServiceConfiguration.frameworkMetaData(),
region: identityPoolConfig.region
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ struct FetchAuthIdentityId: Action {
}

func isNotAuthorizedError(_ error: Error) -> Bool {

if let getIdError: GetIdOutputError = error.internalAWSServiceError(),
case .notAuthorizedException = getIdError {
return true
}
return false
error is AWSCognitoIdentity.NotAuthorizedException
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,8 @@ struct InformSessionError: Action {
}

func isNotAuthorizedError(_ error: Error) -> Bool {


if let serviceError: GetCredentialsForIdentityOutputError = error.internalAWSServiceError(),
case .notAuthorizedException = serviceError {
return true
}
if let serviceError: InitiateAuthOutputError = error.internalAWSServiceError(),
case .notAuthorizedException = serviceError {
return true
}
return false
error is AWSCognitoIdentity.NotAuthorizedException
|| error is AWSCognitoIdentityProvider.NotAuthorizedException
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,7 @@ struct VerifyPasswordSRP: Action {
return false
}

if let serviceError: RespondToAuthChallengeOutputError = error.internalAWSServiceError(),
case .resourceNotFoundException = serviceError {
return true
}
return false
return error is AWSCognitoIdentityProvider.ResourceNotFoundException
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,7 @@ struct VerifySignInChallenge: Action {
return false
}

if let serviceError: RespondToAuthChallengeOutputError = error.internalAWSServiceError(),
case .resourceNotFoundException = serviceError {
return true
}
return false
return error is AWSCognitoIdentityProvider.ResourceNotFoundException
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import Amplify
import AWSCognitoIdentity
import AWSClientRuntime

// AWSCognitoIdentity
extension AWSCognitoIdentity.ExternalServiceException: AuthErrorConvertible {
var fallbackDescription: String { "External service threw error." }

var authError: AuthError {
.service(
properties.message ?? fallbackDescription,
AuthPluginErrorConstants.externalServiceException,
AWSCognitoAuthError.externalServiceException
)
}
}

extension AWSCognitoIdentity.InternalErrorException: AuthErrorConvertible {
var fallbackDescription: String { "Internal exception occurred" }

var authError: AuthError {
.unknown(properties.message ?? fallbackDescription)
}
}

// AWSCognitoIdentity
extension AWSCognitoIdentity.InvalidIdentityPoolConfigurationException: AuthErrorConvertible {
var fallbackDescription: String { "Invalid IdentityPool Configuration error." }

var authError: AuthError {
.configuration(
properties.message ?? fallbackDescription,
AuthPluginErrorConstants.configurationError
)
}
}

extension AWSCognitoIdentity.InvalidParameterException: AuthErrorConvertible {
var fallbackDescription: String { "Invalid parameter error" }

var authError: AuthError {
.service(
properties.message ?? fallbackDescription,
AuthPluginErrorConstants.invalidParameterError,
AWSCognitoAuthError.invalidParameter
)
}
}

extension AWSCognitoIdentity.NotAuthorizedException: AuthErrorConvertible {
var fallbackDescription: String { "Not authorized error." }

var authError: AuthError {
.notAuthorized(
properties.message ?? fallbackDescription,
AuthPluginErrorConstants.notAuthorizedError
)
}
}

extension AWSCognitoIdentity.ResourceConflictException: AuthErrorConvertible {
var fallbackDescription: String { "Resource conflict error." }

var authError: AuthError {
.service(
properties.message ?? fallbackDescription,
AuthPluginErrorConstants.resourceConflictException,
AWSCognitoAuthError.resourceConflictException
)
}
}

extension AWSCognitoIdentity.ResourceNotFoundException: AuthErrorConvertible {
var fallbackDescription: String { "Resource not found error." }

var authError: AuthError {
.service(
properties.message ?? fallbackDescription,
AuthPluginErrorConstants.resourceNotFoundError,
AWSCognitoAuthError.resourceNotFound
)
}
}

extension AWSCognitoIdentity.TooManyRequestsException: AuthErrorConvertible {
var fallbackDescription: String { "Too many requests error." }

var authError: AuthError {
.service(
properties.message ?? fallbackDescription,
AuthPluginErrorConstants.tooManyRequestError,
AWSCognitoAuthError.requestLimitExceeded
)
}
}


extension AWSCognitoIdentity.LimitExceededException: AuthErrorConvertible {
var fallbackDescription: String { "Too many requests error." }

var authError: AuthError {
.service(
properties.message ?? fallbackDescription,
AuthPluginErrorConstants.limitExceededException,
AWSCognitoAuthError.limitExceededException
)
}
}
Loading

0 comments on commit f15e978

Please sign in to comment.