Skip to content

feat(auth): allow to force login on idp for exiting user #14413

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('signInWithRedirect', () => {
const [oauthUrl, redirectSignIn, preferPrivateSession] =
mockOpenAuthSession.mock.calls[0];
expect(oauthUrl).toStrictEqual(
'https://oauth.domain.com/oauth2/authorize?redirect_uri=http%3A%2F%2Flocalhost%3A3000%2F&response_type=code&client_id=userPoolClientId&identity_provider=Google&scope=phone%20email%20openid%20profile%20aws.cognito.signin.user.admin&state=oauth_state&code_challenge=code_challenge&code_challenge_method=S256',
'https://oauth.domain.com/oauth2/authorize?prompt=none&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2F&response_type=code&client_id=userPoolClientId&identity_provider=Google&scope=phone%20email%20openid%20profile%20aws.cognito.signin.user.admin&state=oauth_state&code_challenge=code_challenge&code_challenge_method=S256',
);
expect(redirectSignIn).toEqual(
mockAuthConfigWithOAuth.Auth.Cognito.loginWith.oauth.redirectSignIn,
Expand All @@ -170,7 +170,7 @@ describe('signInWithRedirect', () => {
await signInWithRedirect();
const [oauthUrl] = mockOpenAuthSession.mock.calls[0];
expect(oauthUrl).toStrictEqual(
`https://oauth.domain.com/oauth2/authorize?redirect_uri=http%3A%2F%2Flocalhost%3A3000%2F&response_type=code&client_id=userPoolClientId&identity_provider=${expectedDefaultProvider}&scope=phone%20email%20openid%20profile%20aws.cognito.signin.user.admin&state=oauth_state&code_challenge=code_challenge&code_challenge_method=S256`,
`https://oauth.domain.com/oauth2/authorize?prompt=none&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2F&response_type=code&client_id=userPoolClientId&identity_provider=${expectedDefaultProvider}&scope=phone%20email%20openid%20profile%20aws.cognito.signin.user.admin&state=oauth_state&code_challenge=code_challenge&code_challenge_method=S256`,
);
});

Expand All @@ -179,7 +179,7 @@ describe('signInWithRedirect', () => {
await signInWithRedirect({ provider: { custom: expectedCustomProvider } });
const [oauthUrl] = mockOpenAuthSession.mock.calls[0];
expect(oauthUrl).toStrictEqual(
`https://oauth.domain.com/oauth2/authorize?redirect_uri=http%3A%2F%2Flocalhost%3A3000%2F&response_type=code&client_id=userPoolClientId&identity_provider=${expectedCustomProvider}&scope=phone%20email%20openid%20profile%20aws.cognito.signin.user.admin&state=oauth_state&code_challenge=code_challenge&code_challenge_method=S256`,
`https://oauth.domain.com/oauth2/authorize?prompt=none&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2F&response_type=code&client_id=userPoolClientId&identity_provider=${expectedCustomProvider}&scope=phone%20email%20openid%20profile%20aws.cognito.signin.user.admin&state=oauth_state&code_challenge=code_challenge&code_challenge_method=S256`,
);
});

Expand All @@ -189,6 +189,18 @@ describe('signInWithRedirect', () => {
expect(mockUrlSafeEncode).toHaveBeenCalledWith(expectedCustomState);
});

it('uses "login" as the prompt query param value to when require sign in is true', async () => {
const expectedCustomProvider = 'PieAuth';
await signInWithRedirect({
provider: { custom: expectedCustomProvider },
options: { requireSignIn: true },
});
const [oauthUrl] = mockOpenAuthSession.mock.calls[0];
expect(oauthUrl).toStrictEqual(
`https://oauth.domain.com/oauth2/authorize?prompt=login&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2F&response_type=code&client_id=userPoolClientId&identity_provider=${expectedCustomProvider}&scope=phone%20email%20openid%20profile%20aws.cognito.signin.user.admin&state=oauth_state&code_challenge=code_challenge&code_challenge_method=S256`,
);
});

describe('specifications on Web', () => {
describe('side effect', () => {
it('attaches oauth listener to the Amplify singleton', async () => {
Expand Down Expand Up @@ -324,7 +336,7 @@ describe('signInWithRedirect', () => {
mockOpenAuthSession.mock.calls[0];

expect(oauthUrl).toStrictEqual(
'https://oauth.domain.com/oauth2/authorize?redirect_uri=http%3A%2F%2Flocalhost%3A3000%2F&response_type=code&client_id=userPoolClientId&identity_provider=Google&scope=phone%20email%20openid%20profile%20aws.cognito.signin.user.admin&login_hint=someone%40gmail.com&lang=en&nonce=88388838883&state=oauth_state&code_challenge=code_challenge&code_challenge_method=S256',
'https://oauth.domain.com/oauth2/authorize?prompt=none&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2F&response_type=code&client_id=userPoolClientId&identity_provider=Google&scope=phone%20email%20openid%20profile%20aws.cognito.signin.user.admin&login_hint=someone%40gmail.com&lang=en&nonce=88388838883&state=oauth_state&code_challenge=code_challenge&code_challenge_method=S256',
);
expect(redirectSignIn).toEqual(
mockAuthConfigWithOAuth.Auth.Cognito.loginWith.oauth.redirectSignIn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export async function signInWithRedirect(
oauthConfig: authConfig.loginWith.oauth,
clientId: authConfig.userPoolClientId,
provider,
requireSignIn: input?.options?.requireSignIn,
customState: input?.customState,
preferPrivateSession: input?.options?.preferPrivateSession,
options: {
Expand All @@ -69,13 +70,15 @@ const oauthSignIn = async ({
oauthConfig,
provider,
clientId,
requireSignIn,
customState,
preferPrivateSession,
options,
}: {
oauthConfig: OAuthConfig;
provider: string;
clientId: string;
requireSignIn?: boolean;
customState?: string;
preferPrivateSession?: boolean;
options?: SignInWithRedirectInput['options'];
Expand All @@ -102,6 +105,7 @@ const oauthSignIn = async ({
oAuthStore.storePKCE(value);

const queryString = Object.entries({
prompt: requireSignIn ? 'login' : 'none',
redirect_uri: redirectUri,
response_type: responseType,
client_id: clientId,
Expand Down
6 changes: 6 additions & 0 deletions packages/auth/src/types/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ export interface AuthSignInWithRedirectInput {
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/authorization-endpoint.html
*/
nonce?: string;

/**
* A flag to use if you want to force the user to sign in again or view the account selector screen.
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/authorization-endpoint.html
*/
requireSignIn?: boolean;
};
}

Expand Down
Loading