Skip to content

Commit

Permalink
Add support for the customRoleArn (#1125)
Browse files Browse the repository at this point in the history
The application can specify the role to assume, which will override/bypass the configured logic in the Identity Pool.

https://docs.aws.amazon.com/cognito/latest/developerguide/role-based-access-control.html#using-tokens-to-assign-roles-to-users
  • Loading branch information
Jeroen Holthof committed Jan 27, 2021
1 parent 4ff9051 commit 6263f15
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/auth/src/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export class AuthClass {
identityPoolRegion,
clientMetadata,
endpoint,
customRoleArn,
} = this._config;

if (!this._config.storage) {
Expand Down Expand Up @@ -195,6 +196,7 @@ export class AuthClass {
identityPoolId,
refreshHandlers,
storage: this._storage,
customRoleArn,
});

// initiailize cognitoauth client if hosted ui options provided
Expand Down
2 changes: 2 additions & 0 deletions packages/auth/src/types/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ICookieStorageData,
ICognitoStorage,
CognitoUserAttribute,
CognitoIdToken,
} from 'amazon-cognito-identity-js';

/**
Expand Down Expand Up @@ -51,6 +52,7 @@ export interface AuthOptions {
identityPoolRegion?: string;
clientMetadata?: any;
endpoint?: string;
customRoleArn?: (token : CognitoIdToken | string) => string,
}

export enum CognitoHostedUIIdentityProvider {
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/Credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ export class CredentialsClass {
const logins = {};
logins[domain] = token;

const { identityPoolId, region } = this._config;
const { identityPoolId, region, customRoleArn } = this._config;
if (!identityPoolId) {
logger.debug('No Cognito Federated Identity pool provided');
return Promise.reject('No Cognito Federated Identity pool provided');
Expand All @@ -385,13 +385,15 @@ export class CredentialsClass {
identityId: identity_id,
logins,
client: cognitoClient,
customRoleArn: customRoleArn && customRoleArn(token),
};
credentials = fromCognitoIdentity(cognitoIdentityParams)();
} else {
const cognitoIdentityParams: FromCognitoIdentityPoolParameters = {
logins,
identityPoolId,
client: cognitoClient,
customRoleArn: customRoleArn && customRoleArn(token),
};
credentials = fromCognitoIdentityPool(cognitoIdentityParams)();
}
Expand All @@ -401,7 +403,7 @@ export class CredentialsClass {
private _setCredentialsFromSession(session): Promise<ICredentials> {
logger.debug('set credentials from session');
const idToken = session.getIdToken().getJwtToken();
const { region, userPoolId, identityPoolId } = this._config;
const { region, userPoolId, identityPoolId, customRoleArn } = this._config;
if (!identityPoolId) {
logger.debug('No Cognito Federated Identity pool provided');
return Promise.reject('No Cognito Federated Identity pool provided');
Expand Down Expand Up @@ -440,6 +442,7 @@ export class CredentialsClass {
client: cognitoClient,
logins,
identityId: IdentityId,
customRoleArn: customRoleArn && customRoleArn(idToken)
};

const credentialsFromCognitoIdentity = fromCognitoIdentity(
Expand Down

0 comments on commit 6263f15

Please sign in to comment.