Skip to content

Commit 98e4694

Browse files
committed
Adds clientConfig to CognitoIdentityCredentials to support passing in region and other config to the underlying CognitoIdentity service client
1 parent 3ea51a6 commit 98e4694

File tree

5 files changed

+113
-28
lines changed

5 files changed

+113
-28
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "feature",
3+
"category": "CognitoIdentityCredentials",
4+
"description": "Adds `clientConfig` as an optional parameter to the `CognitoIdentityCredentials` constructor. This parameter can be used to pass in client configuration to the underlying `CognitoIdentity` service client."
5+
}
Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
11
import {Credentials} from '../credentials';
22
import {AWSError} from '../error';
3+
import {ConfigurationOptions} from '../config';
34
import CognitoIdentity = require('../../clients/cognitoidentity');
45
import STS = require('../../clients/sts');
6+
57
export class CognitoIdentityCredentials extends Credentials {
6-
/**
7-
* Creates a new credentials object.
8-
*/
9-
constructor(options?: CognitoIdentity.Types.GetIdInput|CognitoIdentity.Types.GetCredentialsForIdentityInput|CognitoIdentity.Types.GetOpenIdTokenInput|STS.Types.AssumeRoleWithWebIdentityRequest);
10-
/**
11-
* Refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity(), or AWS.STS.assumeRoleWithWebIdentity().
12-
*/
13-
refresh(callback: (err: AWSError) => void): void;
14-
/**
15-
* Clears the cached Cognito ID associated with the currently configured identity pool ID.
16-
*/
17-
clearCachedId(): void;
18-
/**
19-
* The raw data response from the call to AWS.CognitoIdentity.getCredentialsForIdentity(), or AWS.STS.assumeRoleWithWebIdentity().
20-
*/
21-
data: CognitoIdentity.Types.GetCredentialsForIdentityResponse|STS.Types.AssumeRoleWithWebIdentityResponse;
22-
/**
23-
* The Cognito ID returned by the last call to AWS.CognitoIdentity.getOpenIdToken().
24-
*/
25-
identityId: string;
26-
/**
27-
* The map of params passed to AWS.CognitoIdentity.getId(), AWS.CognitoIdentity.getOpenIdToken(), and AWS.STS.assumeRoleWithWebIdentity().
28-
*/
29-
params: CognitoIdentity.Types.GetIdInput|CognitoIdentity.Types.GetOpenIdTokenInput|STS.Types.AssumeRoleWithWebIdentityRequest;
30-
}
8+
/**
9+
* Creates a new credentials object with optional configuration.
10+
*/
11+
constructor(options: CognitoIdentityCredentials.CognitoIdentityOptions, clientConfig?: ConfigurationOptions);
12+
/**
13+
* Creates a new credentials object.
14+
*/
15+
constructor(options?: CognitoIdentityCredentials.CognitoIdentityOptions);
16+
/**
17+
* Refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity(), or AWS.STS.assumeRoleWithWebIdentity().
18+
*/
19+
refresh(callback: (err: AWSError) => void): void;
20+
/**
21+
* Clears the cached Cognito ID associated with the currently configured identity pool ID.
22+
*/
23+
clearCachedId(): void;
24+
/**
25+
* The raw data response from the call to AWS.CognitoIdentity.getCredentialsForIdentity(), or AWS.STS.assumeRoleWithWebIdentity().
26+
*/
27+
data: CognitoIdentity.Types.GetCredentialsForIdentityResponse|STS.Types.AssumeRoleWithWebIdentityResponse;
28+
/**
29+
* The Cognito ID returned by the last call to AWS.CognitoIdentity.getOpenIdToken().
30+
*/
31+
identityId: string;
32+
/**
33+
* The map of params passed to AWS.CognitoIdentity.getId(), AWS.CognitoIdentity.getOpenIdToken(), and AWS.STS.assumeRoleWithWebIdentity().
34+
*/
35+
params: CognitoIdentity.Types.GetIdInput|CognitoIdentity.Types.GetOpenIdTokenInput|STS.Types.AssumeRoleWithWebIdentityRequest;
36+
}
37+
38+
// Needed to expose interfaces on the class
39+
declare namespace CognitoIdentityCredentials {
40+
export type CognitoIdentityCredentialsInputs = CognitoIdentity.GetIdInput|CognitoIdentity.GetCredentialsForIdentityInput|CognitoIdentity.GetOpenIdTokenInput|STS.AssumeRoleWithWebIdentityRequest;
41+
export type CognitoIdentityOptions = CognitoIdentityCredentialsInputs & {LoginId?: string}
42+
}

lib/credentials/cognito_identity_credentials.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,35 @@ AWS.CognitoIdentityCredentials = AWS.util.inherit(AWS.Credentials, {
102102
* // and multiple users are signed in at once, used for caching
103103
* LoginId: 'example@gmail.com'
104104
*
105+
* }, {
106+
* // optionally provide configuration to apply to the underlying AWS.CognitoIdentity service client
107+
* // if configuration is not provided, then configuration will be pulled from AWS.config
108+
*
109+
* // region should match the region your identity pool is located in
110+
* region: 'us-east-1',
111+
*
112+
* // specify timeout options
113+
* httpOptions: {
114+
* timeout: 100
115+
* }
105116
* });
106117
* @see AWS.CognitoIdentity.getId
107118
* @see AWS.CognitoIdentity.getCredentialsForIdentity
108119
* @see AWS.STS.assumeRoleWithWebIdentity
109120
* @see AWS.CognitoIdentity.getOpenIdToken
121+
* @see AWS.Config
122+
* @note If a region is not provided in the global AWS.config, or
123+
* specified in the `clientConfig` to the CognitoIdentityCredentials
124+
* constructor, you may encounter a 'Missing credentials in config' error
125+
* when calling making a service call.
110126
*/
111-
constructor: function CognitoIdentityCredentials(params) {
127+
constructor: function CognitoIdentityCredentials(params, clientConfig) {
112128
AWS.Credentials.call(this);
113129
this.expired = true;
114130
this.params = params;
115131
this.data = null;
116132
this._identityId = null;
133+
this._clientConfig = AWS.util.copy(clientConfig || {});
117134
this.loadCachedId();
118135
var self = this;
119136
Object.defineProperty(this, 'identityId', {
@@ -296,10 +313,14 @@ AWS.CognitoIdentityCredentials = AWS.util.inherit(AWS.Credentials, {
296313
* @api private
297314
*/
298315
createClients: function() {
316+
var clientConfig = this._clientConfig;
299317
this.webIdentityCredentials = this.webIdentityCredentials ||
300318
new AWS.WebIdentityCredentials(this.params);
301-
this.cognito = this.cognito ||
302-
new CognitoIdentity({params: this.params});
319+
if (!this.cognito) {
320+
var cognitoConfig = AWS.util.merge({}, clientConfig);
321+
cognitoConfig.params = this.params;
322+
this.cognito = new CognitoIdentity(cognitoConfig);
323+
}
303324
this.sts = this.sts || new STS();
304325
},
305326

test/credentials.spec.coffee

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,17 @@ describe 'AWS.CognitoIdentityCredentials', ->
896896
expect(creds.sts).to.eql(sts)
897897
expect(creds.webIdentityCredentials).to.eql(webIdentityCredentials)
898898

899+
it 'uses global config for cognito client if client congif ommitted', ->
900+
creds.createClients();
901+
expect(creds.cognito.config.region).to.equal(AWS.config.region);
902+
expect(creds.cognito.config.httpOptions.timeout).to.equal(AWS.config.httpOptions.timeout);
903+
904+
it 'passes clientConfig to cognito client', ->
905+
creds = new AWS.CognitoIdentityCredentials(initParams, {region: 'us-west-2', httpOptions: {timeout: 50}})
906+
creds.createClients();
907+
expect(creds.cognito.config.region).to.equal('us-west-2');
908+
expect(creds.cognito.config.httpOptions.timeout).to.equal(50);
909+
899910
describe 'refresh', ->
900911
beforeEach -> setupClients()
901912

ts/cognitoidentitycredentials.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {CognitoIdentityCredentials} from '../lib/credentials/cognito_identity_credentials';
2+
3+
const creds1 = new CognitoIdentityCredentials();
4+
const creds2 = new CognitoIdentityCredentials({
5+
IdentityPoolId: 'fake'
6+
});
7+
const creds3: CognitoIdentityCredentials = new CognitoIdentityCredentials({
8+
IdentityId: 'id'
9+
});
10+
11+
const creds4: CognitoIdentityCredentials = new CognitoIdentityCredentials({
12+
IdentityId: 'id',
13+
RoleArn: 'arn'
14+
});
15+
16+
const credOptions: CognitoIdentityCredentials.CognitoIdentityOptions = {
17+
IdentityId: 'id',
18+
Logins: {
19+
'graph.facebook.com': 'FBTOKEN',
20+
'www.amazon.com': 'AMAZONTOKEN',
21+
'accounts.google.com': 'GOOGLETOKEN',
22+
'api.twitter.com': 'TWITTERTOKEN',
23+
'www.digits.com': 'DIGITSTOKEN'
24+
},
25+
LoginId: 'example@gmail.com'
26+
}
27+
28+
const creds5: CognitoIdentityCredentials = new CognitoIdentityCredentials(credOptions);
29+
30+
// test client config
31+
const creds6: CognitoIdentityCredentials = new CognitoIdentityCredentials(credOptions, {
32+
httpOptions: {
33+
timeout: 50
34+
},
35+
region: 'us-west-2'
36+
});

0 commit comments

Comments
 (0)