diff --git a/Amplify/Categories/API/AuthProvider/APIAuthProviderFactory.swift b/Amplify/Categories/API/AuthProvider/APIAuthProviderFactory.swift index fe56f3cf96..16a9ba4c2e 100644 --- a/Amplify/Categories/API/AuthProvider/APIAuthProviderFactory.swift +++ b/Amplify/Categories/API/AuthProvider/APIAuthProviderFactory.swift @@ -13,7 +13,12 @@ open class APIAuthProviderFactory { /// Empty public initializer public init() { } - + + /// Retrieve the UserPools auth provider + open func userPoolsAuthProvider() -> AmplifyUserPoolsAuthProvider? { + return nil + } + /// Retrieve the OIDC auth provider open func oidcAuthProvider() -> AmplifyOIDCAuthProvider? { return nil @@ -30,6 +35,9 @@ public protocol AmplifyAuthTokenProvider { func getLatestAuthToken() async throws -> String } +/// Amplify CognitoUserPools Auth Provider +public protocol AmplifyUserPoolsAuthProvider: AmplifyAuthTokenProvider {} + /// Amplify OIDC Auth Provider public protocol AmplifyOIDCAuthProvider: AmplifyAuthTokenProvider {} diff --git a/AmplifyPlugins/API/Sources/AWSAPIPlugin/Configuration/AWSAPIEndpointInterceptors.swift b/AmplifyPlugins/API/Sources/AWSAPIPlugin/Configuration/AWSAPIEndpointInterceptors.swift index 99191e40dc..6dd70b78cf 100644 --- a/AmplifyPlugins/API/Sources/AWSAPIPlugin/Configuration/AWSAPIEndpointInterceptors.swift +++ b/AmplifyPlugins/API/Sources/AWSAPIPlugin/Configuration/AWSAPIEndpointInterceptors.swift @@ -81,14 +81,21 @@ struct AWSAPIEndpointInterceptors { endpointType: endpointType) postludeInterceptors.append(interceptor) case .amazonCognitoUserPools: - guard let authService = authService else { - throw PluginError.pluginConfigurationError("AuthService not set for cognito user pools", - "") + if let userPoolsAuthProvider = apiAuthProviderFactory.userPoolsAuthProvider() { + let wrappedAuthProvider = AuthTokenProviderWrapper(tokenAuthProvider: userPoolsAuthProvider) + let interceptor = AuthTokenURLRequestInterceptor(authTokenProvider: wrappedAuthProvider, + isTokenExpired: expiryValidator) + preludeInterceptors.append(interceptor) + } else { + guard let authService = authService else { + throw PluginError.pluginConfigurationError("AuthService not set for cognito user pools", + "") + } + let provider = BasicUserPoolTokenProvider(authService: authService) + let interceptor = AuthTokenURLRequestInterceptor(authTokenProvider: provider, + isTokenExpired: expiryValidator) + preludeInterceptors.append(interceptor) } - let provider = BasicUserPoolTokenProvider(authService: authService) - let interceptor = AuthTokenURLRequestInterceptor(authTokenProvider: provider, - isTokenExpired: expiryValidator) - preludeInterceptors.append(interceptor) case .openIDConnect: guard let oidcAuthProvider = apiAuthProviderFactory.oidcAuthProvider() else { throw PluginError.pluginConfigurationError("AuthService not set for OIDC",