Skip to content

Commit

Permalink
fix: override default CognitoIdentityClient config to remove assumpti…
Browse files Browse the repository at this point in the history
…on of fetch body support
  • Loading branch information
russell-dot-js committed May 6, 2020
1 parent 1a3cd7c commit 21fde63
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
"@aws-crypto/sha256-js": "1.0.0-alpha.0",
"@aws-sdk/client-cognito-identity": "1.0.0-beta.5",
"@aws-sdk/credential-provider-cognito-identity": "1.0.0-beta.5",
"@aws-sdk/fetch-http-handler": "1.0.0-beta.4",
"@aws-sdk/node-http-handler": "1.0.0-beta.4",
"@aws-sdk/stream-collector-native": "^1.0.0-beta.4",
"@aws-sdk/types": "1.0.0-beta.4",
"@aws-sdk/util-hex-encoding": "1.0.0-beta.2",
"@aws-sdk/util-user-agent-browser": "1.0.0-beta.4",
Expand Down
59 changes: 30 additions & 29 deletions packages/core/src/Credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import {
CognitoIdentityClient,
GetIdCommand,
} from '@aws-sdk/client-cognito-identity';

// TODO: can remove CognitoIdentityClient config overrides once
// https://github.com/aws/aws-sdk-js-v3/pull/1121 or
// https://github.com/aws/aws-sdk-js-v3/pull/1123 is merged
import { FetchHttpHandler } from '@aws-sdk/fetch-http-handler';
import { streamCollector } from '@aws-sdk/stream-collector-native';

import { CredentialProvider } from '@aws-sdk/types';

const logger = new Logger('Credentials');
Expand Down Expand Up @@ -181,9 +188,25 @@ export class CredentialsClass {
return true;
}

private _getCognitoIdentityClient(): CognitoIdentityClient {
const { region } = this._config;

if (!region) {
logger.debug('region is not configured for getting the credentials');
throw new Error('region is not configured for getting the credentials');
}

return new CognitoIdentityClient({
customUserAgent: getAmplifyUserAgent(),
region,
requestHandler: new FetchHttpHandler({ bufferBody: true }),
streamCollector,
});
}

private async _setCredentialsForGuest() {
logger.debug('setting credentials for guest');
const { identityPoolId, region, mandatorySignIn } = this._config;
const { identityPoolId, mandatorySignIn } = this._config;
if (mandatorySignIn) {
return Promise.reject(
'cannot get guest credentials when mandatory signin enabled'
Expand All @@ -199,13 +222,6 @@ export class CredentialsClass {
);
}

if (!region) {
logger.debug('region is not configured for getting the credentials');
return Promise.reject(
'region is not configured for getting the credentials'
);
}

let identityId = undefined;
try {
await this._storageSync;
Expand All @@ -215,10 +231,7 @@ export class CredentialsClass {
logger.debug('Failed to get the cached identityId', e);
}

const cognitoClient = new CognitoIdentityClient({
region,
customUserAgent: getAmplifyUserAgent(),
});
const cognitoClient = this._getCognitoIdentityClient();

let credentials = undefined;
if (identityId) {
Expand Down Expand Up @@ -267,7 +280,7 @@ export class CredentialsClass {
});
}

private _setCredentialsFromFederation(params) {
private async _setCredentialsFromFederation(params): Promise<ICredentials> {
const { provider, token, identity_id } = params;
const domains = {
google: 'accounts.google.com',
Expand All @@ -285,22 +298,13 @@ export class CredentialsClass {
const logins = {};
logins[domain] = token;

const { identityPoolId, region } = this._config;
const { identityPoolId } = this._config;
if (!identityPoolId) {
logger.debug('No Cognito Federated Identity pool provided');
return Promise.reject('No Cognito Federated Identity pool provided');
}
if (!region) {
logger.debug('region is not configured for getting the credentials');
return Promise.reject(
'region is not configured for getting the credentials'
);
}

const cognitoClient = new CognitoIdentityClient({
region,
customUserAgent: getAmplifyUserAgent(),
});
const cognitoClient = this._getCognitoIdentityClient();

let credentials = undefined;
if (identity_id) {
Expand All @@ -321,7 +325,7 @@ export class CredentialsClass {
return this._loadCredentials(credentials, 'federated', true, params);
}

private _setCredentialsFromSession(session): Promise<ICredentials> {
private async _setCredentialsFromSession(session): Promise<ICredentials> {
logger.debug('set credentials from session');
const idToken = session.getIdToken().getJwtToken();
const { region, userPoolId, identityPoolId } = this._config;
Expand All @@ -339,10 +343,7 @@ export class CredentialsClass {
const logins = {};
logins[key] = idToken;

const cognitoClient = new CognitoIdentityClient({
region,
customUserAgent: getAmplifyUserAgent(),
});
const cognitoClient = this._getCognitoIdentityClient();

/*
Retreiving identityId with GetIdCommand to mimic the behavior in the following code in aws-sdk-v3:
Expand Down

0 comments on commit 21fde63

Please sign in to comment.