Skip to content

Added support for idp_identifier query parameter in cognito authorize… #10505

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 1 commit 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
10 changes: 8 additions & 2 deletions packages/auth/src/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import {
AuthErrorTypes,
AutoSignInOptions,
CognitoHostedUIIdentityProvider,
hasIdpIdentifier,
IAuthDevice,
} from './types/Auth';

Expand Down Expand Up @@ -219,7 +220,6 @@ export class AuthClass {
? oauth
: (<any>oauth).awsCognito
: undefined;

if (cognitoHostedUIConfig) {
const cognitoAuthParams = Object.assign(
{
Expand Down Expand Up @@ -2339,6 +2339,7 @@ export class AuthClass {
isFederatedSignInOptions(providerOrOptions) ||
isFederatedSignInOptionsCustom(providerOrOptions) ||
hasCustomState(providerOrOptions) ||
hasIdpIdentifier(providerOrOptions) ||
typeof providerOrOptions === 'undefined'
) {
const options = providerOrOptions || {
Expand All @@ -2352,6 +2353,10 @@ export class AuthClass {
? options.customState
: (options as FederatedSignInOptionsCustom).customState;

const idpIdentifier = isFederatedSignInOptions(options)
? options.idpIdentifier
: (options as FederatedSignInOptionsCustom).idpIdentifier;

if (this._config.userPoolId) {
const client_id = isCognitoHostedOpts(this._config.oauth)
? this._config.userPoolWebClientId
Expand All @@ -2367,7 +2372,8 @@ export class AuthClass {
redirect_uri,
client_id,
provider,
customState
customState,
idpIdentifier
);
}
} else {
Expand Down
87 changes: 49 additions & 38 deletions packages/auth/src/OAuth/OAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import { ConsoleLogger as Logger, Hub, urlSafeEncode } from '@aws-amplify/core';
import sha256 from 'crypto-js/sha256';
import Base64 from 'crypto-js/enc-base64';

const AMPLIFY_SYMBOL = (typeof Symbol !== 'undefined' &&
typeof Symbol.for === 'function'
? Symbol.for('amplify_default')
: '@@amplify_default') as Symbol;
const AMPLIFY_SYMBOL = (
typeof Symbol !== 'undefined' && typeof Symbol.for === 'function'
? Symbol.for('amplify_default')
: '@@amplify_default'
) as Symbol;

const dispatchAuthEvent = (event: string, data: any, message: string) => {
Hub.dispatch('auth', { event, data, message }, 'Auth', AMPLIFY_SYMBOL);
Expand Down Expand Up @@ -72,19 +73,18 @@ export default class OAuth {
domain: string,
redirectSignIn: string,
clientId: string,
provider:
| CognitoHostedUIIdentityProvider
| string = CognitoHostedUIIdentityProvider.Cognito,
customState?: string
provider?: CognitoHostedUIIdentityProvider | string,
customState?: string,
idpIdentifier?: string
) {
const generatedState = this._generateState(32);

/* encodeURIComponent is not URL safe, use urlSafeEncode instead. Cognito
single-encodes/decodes url on first sign in and double-encodes/decodes url
when user already signed in. Using encodeURIComponent, Base32, Base64 add
characters % or = which on further encoding becomes unsafe. '=' create issue
for parsing query params.
Refer: https://github.com/aws-amplify/amplify-js/issues/5218 */
/* encodeURIComponent is not URL safe, use urlSafeEncode instead. Cognito
single-encodes/decodes url on first sign in and double-encodes/decodes url
when user already signed in. Using encodeURIComponent, Base32, Base64 add
characters % or = which on further encoding becomes unsafe. '=' create issue
for parsing query params.
Refer: https://github.com/aws-amplify/amplify-js/issues/5218 */
const state = customState
? `${generatedState}-${urlSafeEncode(customState)}`
: generatedState;
Expand All @@ -99,18 +99,32 @@ export default class OAuth {

const scopesString = this._scopes.join(' ');

const queryString = Object.entries({
redirect_uri: redirectSignIn,
response_type: responseType,
client_id: clientId,
identity_provider: provider,
scope: scopesString,
state,
...(responseType === 'code' ? { code_challenge } : {}),
...(responseType === 'code' ? { code_challenge_method } : {}),
})
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join('&');
const queryString = provider
? Object.entries({
redirect_uri: redirectSignIn,
response_type: responseType,
client_id: clientId,
identity_provider: provider,
scope: scopesString,
state,

...(responseType === 'code' ? { code_challenge } : {}),
...(responseType === 'code' ? { code_challenge_method } : {}),
})
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join('&')
: Object.entries({
redirect_uri: redirectSignIn,
response_type: responseType,
idp_identifier: idpIdentifier,
client_id: clientId,
scope: scopesString,
state,
...(responseType === 'code' ? { code_challenge } : {}),
...(responseType === 'code' ? { code_challenge_method } : {}),
})
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join('&');

const URL = `https://${domain}/oauth2/authorize?${queryString}`;
logger.debug(`Redirecting to ${URL}`);
Expand Down Expand Up @@ -169,18 +183,15 @@ export default class OAuth {
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join('&');

const {
access_token,
refresh_token,
id_token,
error,
} = await ((await fetch(oAuthTokenEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body,
})) as any).json();
const { access_token, refresh_token, id_token, error } = await (
(await fetch(oAuthTokenEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body,
})) as any
).json();

if (error) {
throw new Error(error);
Expand Down
13 changes: 11 additions & 2 deletions packages/auth/src/types/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ export type LegacyProvider =
| string;

export type FederatedSignInOptions = {
provider: CognitoHostedUIIdentityProvider;
provider?: CognitoHostedUIIdentityProvider;
customState?: string;
idpIdentifier?: string;
};

export type FederatedSignInOptionsCustom = {
customProvider: string;
customProvider?: string;
customState?: string;
idpIdentifier?: string;
};

export function isFederatedSignInOptions(
Expand All @@ -100,6 +102,13 @@ export function hasCustomState(obj: any): boolean {
))[] = ['customState'];
return obj && !!keys.find(k => obj.hasOwnProperty(k));
}
export function hasIdpIdentifier(obj: any): boolean {
const keys: (keyof (
| FederatedSignInOptions
| FederatedSignInOptionsCustom
))[] = ['idpIdentifier'];
return obj && !!keys.find(k => obj.hasOwnProperty(k));
}

/**
* Details for multi-factor authentication
Expand Down