From 91e03b04eea25cdfe2c1e061f27f437a254c8073 Mon Sep 17 00:00:00 2001 From: Hui Zhao Date: Tue, 29 Oct 2024 11:21:42 -0700 Subject: [PATCH] chore: adding warnings regarding using custom endpoints --- .../__tests__/singleton/Auth/index.test.ts | 52 +++++++++++++++++++ packages/core/src/singleton/Auth/index.ts | 17 ++++++ packages/core/src/singleton/Auth/types.ts | 6 +++ 3 files changed, 75 insertions(+) create mode 100644 packages/core/__tests__/singleton/Auth/index.test.ts diff --git a/packages/core/__tests__/singleton/Auth/index.test.ts b/packages/core/__tests__/singleton/Auth/index.test.ts new file mode 100644 index 00000000000..e08f838cfe5 --- /dev/null +++ b/packages/core/__tests__/singleton/Auth/index.test.ts @@ -0,0 +1,52 @@ +import { ConsoleLogger } from '../../../src/Logger'; +import { AuthClass } from '../../../src/singleton/Auth'; + +jest.mock('../../../src/Logger', () => { + const warn = jest.fn(); + + return { + ConsoleLogger: jest.fn(() => ({ + warn, + })), + }; +}); + +describe('Auth', () => { + const auth = new AuthClass(); + const logger = new ConsoleLogger('Auth'); + const mockedWarn = logger.warn as jest.Mock; + + describe('configure', () => { + const mockConfig = { + userPoolClientId: 'userPoolClientId', + userPoolId: 'userPoolId', + }; + + it('prints warning when use custom endpoint for Cognito User Pool', () => { + auth.configure({ + Cognito: { + ...mockConfig, + userPoolEndpoint: 'https://custom-endpoint.com', + }, + }); + + expect(mockedWarn).toHaveBeenCalledWith( + expect.stringContaining('Amazon Cognito User Pool'), + ); + }); + + it('prints warning when use custom endpoint for Cognito Identity Pool', () => { + auth.configure({ + Cognito: { + ...mockConfig, + identityPoolId: 'identityPoolId', + identityPoolEndpoint: 'https://custom-endpoint.com', + }, + }); + + expect(mockedWarn).toHaveBeenCalledWith( + expect.stringContaining('Amazon Cognito Identity Pool'), + ); + }); + }); +}); diff --git a/packages/core/src/singleton/Auth/index.ts b/packages/core/src/singleton/Auth/index.ts index 245cc704b0c..e45ca7135f6 100644 --- a/packages/core/src/singleton/Auth/index.ts +++ b/packages/core/src/singleton/Auth/index.ts @@ -1,5 +1,7 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 +import { ConsoleLogger } from '../../Logger'; + import { AuthConfig, AuthSession, @@ -9,6 +11,8 @@ import { LibraryAuthOptions, } from './types'; +const logger = new ConsoleLogger('Auth'); + export function isTokenExpired({ expiresAt, clockDrift, @@ -41,6 +45,16 @@ export class AuthClass { ): void { this.authConfig = authResourcesConfig; this.authOptions = authOptions; + + if (authResourcesConfig.Cognito.userPoolEndpoint) { + logger.warn(getCustomEndpointWarningMessage('Amazon Cognito User Pool')); + } + + if (authResourcesConfig.Cognito.identityPoolEndpoint) { + logger.warn( + getCustomEndpointWarningMessage('Amazon Cognito Identity Pool'), + ); + } } /** @@ -106,3 +120,6 @@ export class AuthClass { ); } } + +const getCustomEndpointWarningMessage = (target: string): string => + `You are using a custom Amazon ${target} endpoint, ensure the endpoint is correct.`; diff --git a/packages/core/src/singleton/Auth/types.ts b/packages/core/src/singleton/Auth/types.ts index acd61f8d91d..8eb903c0487 100644 --- a/packages/core/src/singleton/Auth/types.ts +++ b/packages/core/src/singleton/Auth/types.ts @@ -135,6 +135,9 @@ export interface AuthIdentityPoolConfig { export interface CognitoIdentityPoolConfig { identityPoolId: string; + /** + * Use this field to specify a custom endpoint for the Amazon Cognito identity pool. Ensure this endpoint is correct and valid. + */ identityPoolEndpoint?: string; allowGuestAccess?: boolean; } @@ -152,6 +155,9 @@ export type CognitoUserPoolConfigMfaStatus = 'on' | 'off' | 'optional'; export interface CognitoUserPoolConfig { userPoolClientId: string; userPoolId: string; + /** + * Use this field to specify a custom endpoint for the Amazon Cognito user pool. Ensure this endpoint is correct and valid. + */ userPoolEndpoint?: string; signUpVerificationMethod?: 'code' | 'link'; loginWith?: {