Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(singleton): allow oauth listener to be called with the latest config #13642

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions packages/aws-amplify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -293,31 +293,31 @@
"name": "[Analytics] record (Pinpoint)",
"path": "./dist/esm/analytics/index.mjs",
"import": "{ record }",
"limit": "17.09 kB"
"limit": "17.15 kB"
},
{
"name": "[Analytics] record (Kinesis)",
"path": "./dist/esm/analytics/kinesis/index.mjs",
"import": "{ record }",
"limit": "48.56 kB"
"limit": "48.59 kB"
},
{
"name": "[Analytics] record (Kinesis Firehose)",
"path": "./dist/esm/analytics/kinesis-firehose/index.mjs",
"import": "{ record }",
"limit": "45.68 kB"
"limit": "45.72 kB"
},
{
"name": "[Analytics] record (Personalize)",
"path": "./dist/esm/analytics/personalize/index.mjs",
"import": "{ record }",
"limit": "49.50 kB"
"limit": "49.54 kB"
},
{
"name": "[Analytics] identifyUser (Pinpoint)",
"path": "./dist/esm/analytics/index.mjs",
"import": "{ identifyUser }",
"limit": "15.59 kB"
"limit": "15.65 kB"
},
{
"name": "[Analytics] enable",
Expand All @@ -335,7 +335,7 @@
"name": "[API] generateClient (AppSync)",
"path": "./dist/esm/api/index.mjs",
"import": "{ generateClient }",
"limit": "40.09 kB"
"limit": "40.11 kB"
},
{
"name": "[API] REST API handlers",
Expand All @@ -353,13 +353,13 @@
"name": "[Auth] resetPassword (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ resetPassword }",
"limit": "12.44 kB"
"limit": "12.48 kB"
},
{
"name": "[Auth] confirmResetPassword (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ confirmResetPassword }",
"limit": "12.39 kB"
"limit": "12.43 kB"
},
{
"name": "[Auth] signIn (Cognito)",
Expand All @@ -371,7 +371,7 @@
"name": "[Auth] resendSignUpCode (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ resendSignUpCode }",
"limit": "12.40 kB"
"limit": "12.44 kB"
},
{
"name": "[Auth] confirmSignUp (Cognito)",
Expand All @@ -383,31 +383,31 @@
"name": "[Auth] confirmSignIn (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ confirmSignIn }",
"limit": "28.27 kB"
"limit": "28.33 kB"
},
{
"name": "[Auth] updateMFAPreference (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ updateMFAPreference }",
"limit": "11.74 kB"
"limit": "11.78 kB"
},
{
"name": "[Auth] fetchMFAPreference (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ fetchMFAPreference }",
"limit": "11.78 kB"
"limit": "11.82 kB"
},
{
"name": "[Auth] verifyTOTPSetup (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ verifyTOTPSetup }",
"limit": "12.6 kB"
"limit": "12.65 kB"
},
{
"name": "[Auth] updatePassword (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ updatePassword }",
"limit": "12.63 kB"
"limit": "12.67 kB"
},
{
"name": "[Auth] setUpTOTP (Cognito)",
Expand All @@ -419,19 +419,19 @@
"name": "[Auth] updateUserAttributes (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ updateUserAttributes }",
"limit": "11.87 kB"
"limit": "11.90 kB"
},
{
"name": "[Auth] getCurrentUser (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ getCurrentUser }",
"limit": "7.75 kB"
"limit": "7.80 kB"
},
{
"name": "[Auth] confirmUserAttribute (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ confirmUserAttribute }",
"limit": "12.61 kB"
"limit": "12.65 kB"
},
{
"name": "[Auth] signInWithRedirect (Cognito)",
Expand All @@ -443,55 +443,55 @@
"name": "[Auth] fetchUserAttributes (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ fetchUserAttributes }",
"limit": "11.69 kB"
"limit": "11.73 kB"
},
{
"name": "[Auth] Basic Auth Flow (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ signIn, signOut, fetchAuthSession, confirmSignIn }",
"limit": "30.06 kB"
"limit": "30.12 kB"
},
{
"name": "[Auth] OAuth Auth Flow (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ signInWithRedirect, signOut, fetchAuthSession }",
"limit": "21.47 kB"
"limit": "21.53 kB"
},
{
"name": "[Storage] copy (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ copy }",
"limit": "14.54 kB"
"limit": "14.57 kB"
},
{
"name": "[Storage] downloadData (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ downloadData }",
"limit": "15.17 kB"
"limit": "15.20 kB"
},
{
"name": "[Storage] getProperties (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ getProperties }",
"limit": "14.43 kB"
"limit": "14.47 kB"
},
{
"name": "[Storage] getUrl (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ getUrl }",
"limit": "15.51 kB"
"limit": "15.55 kB"
},
{
"name": "[Storage] list (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ list }",
"limit": "15.04 kB"
"limit": "15.08 kB"
},
{
"name": "[Storage] remove (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ remove }",
"limit": "14.29 kB"
"limit": "14.33 kB"
},
{
"name": "[Storage] uploadData (S3)",
Expand Down
64 changes: 63 additions & 1 deletion packages/core/__tests__/singleton/Singleton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import { Amplify } from '../../src/singleton';
import { AMPLIFY_SYMBOL, Hub } from '../../src/Hub';
import { AuthClass as Auth } from '../../src/singleton/Auth';
import { decodeJWT } from '../../src/singleton/Auth/utils';
import { CredentialsAndIdentityId } from '../../src/singleton/Auth/types';
import {
CognitoUserPoolConfig,
CredentialsAndIdentityId,
} from '../../src/singleton/Auth/types';
import { ResourcesConfig, fetchAuthSession } from '../../src';
import { ADD_OAUTH_LISTENER } from '../../src/libraryUtils';

Object.assign(global, { TextDecoder, TextEncoder });

Expand Down Expand Up @@ -255,6 +259,64 @@ describe('Amplify.configure() and Amplify.getConfig()', () => {
expect(config3).not.toBe(config);
expect(config3).not.toBe(config2);
});

describe('with oAuthListener', () => {
const mockCognitoConfig: CognitoUserPoolConfig = {
userPoolId: 'xxxxxxxx',
userPoolClientId: 'xxxxxxx',
};
const mockOAuthConfig: CognitoUserPoolConfig = {
...mockCognitoConfig,
loginWith: {
oauth: {
domain: 'domain',
redirectSignIn: ['localhost:3000'],
redirectSignOut: ['localhost:3000'],
scopes: ['profile'],
responseType: 'code',
},
},
};
const mockInvalidOAuthConfig: CognitoUserPoolConfig = {
...mockCognitoConfig,
loginWith: {
oauth: {
domain: 'invalidDomain',
redirectSignIn: ['localhost:3000'],
redirectSignOut: ['localhost:3000'],
scopes: ['profile'],
responseType: 'code',
},
},
};
const mockOAuthlistener = jest.fn();

beforeEach(() => {
Amplify[ADD_OAUTH_LISTENER](mockOAuthlistener);
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
});
it('should not call oAuthListener when OAuth config is not defined', async () => {
Amplify.configure({ Auth: { Cognito: mockCognitoConfig } });
expect(mockOAuthlistener).not.toHaveBeenCalled();
});

it('should call oAuthListener when OAuth config and oAuthListener are defined', async () => {
Amplify.configure({ Auth: { Cognito: mockOAuthConfig } });
jest.runAllTimers();
expect(mockOAuthlistener).toHaveBeenCalledWith(mockOAuthConfig);
});

it('should call oAuthListener with the latest config', async () => {
Amplify.configure({ Auth: { Cognito: mockInvalidOAuthConfig } });
Amplify.configure({ Auth: { Cognito: mockOAuthConfig } });
jest.runAllTimers();
expect(mockOAuthlistener).toHaveBeenCalledWith(mockOAuthConfig);
});
});
});

describe('Session tests', () => {
Expand Down
40 changes: 26 additions & 14 deletions packages/core/src/singleton/Amplify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import {
} from './types';
import { AuthClass } from './Auth';
import { ADD_OAUTH_LISTENER } from './constants';
import { OAuthListener } from './Auth/types';

export class AmplifyClass {
private oAuthListener:
| ((authConfig: AuthConfig['Cognito']) => void)
| undefined = undefined;
private oAuthListener: OAuthListener | undefined = undefined;

resourcesConfig: ResourcesConfig;
libraryOptions: LibraryOptions;
private listenerTimeout?: ReturnType<typeof setTimeout>;

/**
* Cross-category Auth utilities.
Expand Down Expand Up @@ -53,7 +53,6 @@ export class AmplifyClass {
libraryOptions?: LibraryOptions,
): void {
const resolvedResourceConfig = parseAmplifyConfig(resourcesConfig);

this.resourcesConfig = resolvedResourceConfig;

if (libraryOptions) {
Expand All @@ -75,7 +74,10 @@ export class AmplifyClass {
AMPLIFY_SYMBOL,
);

this.notifyOAuthListener();
const config = this.resourcesConfig.Auth?.Cognito;
if (config?.loginWith?.oauth && this.oAuthListener) {
this.notifyOAuthListener(this, config, this.oAuthListener);
}
}

/**
Expand All @@ -98,17 +100,27 @@ export class AmplifyClass {
}
}

private notifyOAuthListener() {
if (
!this.resourcesConfig.Auth?.Cognito.loginWith?.oauth ||
!this.oAuthListener
) {
return;
private notifyOAuthListener(
amplify: AmplifyClass,
config: AuthConfig['Cognito'],
oAuthListener: OAuthListener,
Comment on lines +104 to +106
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making this pure would make sense if it were removed completely from the class, but since it is a private member of the class, this seems confusing. For example, it's not really clear to me why oAuthListener is passed separately from this. The clearListenerTimeout is then also tacked onto the instance but it's obfuscated here.

) {
if (amplify.listenerTimeout) {
amplify.clearListenerTimeout();
}
// The setTimeout will only be called as long as an oAuthLister and oauth config
// are defined. The code executed by the time out will be only fired once.
Comment on lines +111 to +112
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like it only works with certain preconditions, e.g. assuming Amplify.configure() gets called back to back. While this trick seemed to be working, I'm hesitant to make this a solution...

With Amplify Gen2, amplify_outputs.json can be overridden from the backend.ts (reference). With Amplify Gen1, should be able to change the url in amplifyconfiguration.json as well, would be these be legit workarounds to solve what the PR is trying to solve?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to echo this sentiment. When I looked at this PR this was my initial concern as well. Code after the configure call that may have previously executed after the this.oAuthListener call was made may now execute after the call due to it being pulled out of the current execution cycle. While I'm not certain that is practically concerning, it feels quite risky.

amplify.listenerTimeout = setTimeout(() => {
oAuthListener(config);
// the listener should only be notified once with a valid oauth config
amplify.oAuthListener = undefined;
amplify.clearListenerTimeout();
});
}

this.oAuthListener(this.resourcesConfig.Auth?.Cognito);
// the listener should only be notified once with a valid oauth config
this.oAuthListener = undefined;
private clearListenerTimeout() {
clearTimeout(this.listenerTimeout);
this.listenerTimeout = undefined;
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/singleton/Auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,5 @@ type AuthFlowType =
| 'CUSTOM_WITH_SRP'
| 'CUSTOM_WITHOUT_SRP'
| 'USER_PASSWORD_AUTH';

export type OAuthListener = (authConfig: AuthConfig['Cognito']) => void;
6 changes: 3 additions & 3 deletions packages/interactions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@
"name": "Interactions (default to Lex v2)",
"path": "./dist/esm/index.mjs",
"import": "{ Interactions }",
"limit": "52.52 kB"
"limit": "52.57 kB"
},
{
"name": "Interactions (Lex v2)",
"path": "./dist/esm/lex-v2/index.mjs",
"import": "{ Interactions }",
"limit": "52.52 kB"
"limit": "52.57 kB"
},
{
"name": "Interactions (Lex v1)",
"path": "./dist/esm/lex-v1/index.mjs",
"import": "{ Interactions }",
"limit": "47.33 kB"
"limit": "47.38 kB"
}
]
}
Loading