@aws-amplify/core 6.0.1-console-preview.2f5ba46.0 → 6.0.1-console-preview.9e7dd78.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Hub/types/AuthTypes.d.ts +13 -7
- package/lib/Platform/index.d.ts +2 -2
- package/lib/Platform/index.js +2 -5
- package/lib/Platform/types.d.ts +4 -55
- package/lib/Platform/types.js +36 -48
- package/lib/Platform/version.d.ts +1 -1
- package/lib/Platform/version.js +1 -1
- package/lib/index.d.ts +0 -6
- package/lib/index.js +2 -17
- package/lib/parseAWSExports.d.ts +5 -4
- package/lib/parseAWSExports.js +37 -43
- package/lib/singleton/Auth/types.d.ts +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/core.d.ts +0 -8
- package/lib-esm/Hub/types/AuthTypes.d.ts +13 -7
- package/lib-esm/Platform/index.d.ts +2 -2
- package/lib-esm/Platform/index.js +2 -5
- package/lib-esm/Platform/types.d.ts +4 -55
- package/lib-esm/Platform/types.js +36 -48
- package/lib-esm/Platform/version.d.ts +1 -1
- package/lib-esm/Platform/version.js +1 -1
- package/lib-esm/index.d.ts +0 -6
- package/lib-esm/index.js +1 -6
- package/lib-esm/parseAWSExports.d.ts +5 -4
- package/lib-esm/parseAWSExports.js +37 -43
- package/lib-esm/singleton/Auth/types.d.ts +1 -1
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types/core.d.ts +0 -8
- package/package.json +2 -26
- package/src/Cache/CHANGELOG.md +12 -0
- package/src/Hub/types/AuthTypes.ts +8 -6
- package/src/Platform/index.ts +3 -8
- package/src/Platform/types.ts +34 -56
- package/src/Platform/version.ts +1 -1
- package/src/index.ts +1 -7
- package/src/parseAWSExports.ts +69 -63
- package/src/singleton/Auth/types.ts +1 -1
- package/src/types/core.ts +0 -10
- package/lib/Credentials.d.ts +0 -47
- package/lib/Credentials.js +0 -676
- package/lib-esm/Credentials.d.ts +0 -47
- package/lib-esm/Credentials.js +0 -673
- package/src/Credentials.ts +0 -630
package/lib-esm/Credentials.js
DELETED
|
@@ -1,673 +0,0 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
23
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
-
function step(op) {
|
|
26
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
28
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
-
switch (op[0]) {
|
|
31
|
-
case 0: case 1: t = op; break;
|
|
32
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
-
default:
|
|
36
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
-
if (t[2]) _.ops.pop();
|
|
41
|
-
_.trys.pop(); continue;
|
|
42
|
-
}
|
|
43
|
-
op = body.call(thisArg, _);
|
|
44
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
// @ts-nocheck
|
|
49
|
-
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
50
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
51
|
-
import { ConsoleLogger as Logger } from './Logger';
|
|
52
|
-
import { StorageHelper } from './StorageHelper';
|
|
53
|
-
import { makeQuerablePromise } from './Util/JS';
|
|
54
|
-
import { FacebookOAuth, GoogleOAuth } from './OAuthHelper';
|
|
55
|
-
import { jitteredExponentialRetry } from './Util';
|
|
56
|
-
import { Amplify } from './Amplify';
|
|
57
|
-
import { getId, getCredentialsForIdentity } from './AwsClients/CognitoIdentity';
|
|
58
|
-
import { parseAWSExports } from './parseAWSExports';
|
|
59
|
-
import { Hub, AMPLIFY_SYMBOL } from './Hub';
|
|
60
|
-
var logger = new Logger('Credentials');
|
|
61
|
-
var CREDENTIALS_TTL = 50 * 60 * 1000; // 50 min, can be modified on config if required in the future
|
|
62
|
-
var COGNITO_IDENTITY_KEY_PREFIX = 'CognitoIdentityId-';
|
|
63
|
-
var dispatchCredentialsEvent = function (event, data, message) {
|
|
64
|
-
Hub.dispatch('core', { event: event, data: data, message: message }, 'Credentials', AMPLIFY_SYMBOL);
|
|
65
|
-
};
|
|
66
|
-
var CredentialsClass = /** @class */ (function () {
|
|
67
|
-
function CredentialsClass(config) {
|
|
68
|
-
this._gettingCredPromise = null;
|
|
69
|
-
this._refreshHandlers = {};
|
|
70
|
-
// Allow `Auth` to be injected for SSR, but Auth isn't a required dependency for Credentials
|
|
71
|
-
this.Auth = undefined;
|
|
72
|
-
this.configure(config);
|
|
73
|
-
this._refreshHandlers['google'] = GoogleOAuth.refreshGoogleToken;
|
|
74
|
-
this._refreshHandlers['facebook'] = FacebookOAuth.refreshFacebookToken;
|
|
75
|
-
}
|
|
76
|
-
CredentialsClass.prototype.getModuleName = function () {
|
|
77
|
-
return 'Credentials';
|
|
78
|
-
};
|
|
79
|
-
CredentialsClass.prototype.getCredSource = function () {
|
|
80
|
-
return this._credentials_source;
|
|
81
|
-
};
|
|
82
|
-
CredentialsClass.prototype.configure = function (config) {
|
|
83
|
-
if (!config)
|
|
84
|
-
return this._config || {};
|
|
85
|
-
this._config = Object.assign({}, this._config, config);
|
|
86
|
-
var refreshHandlers = this._config.refreshHandlers;
|
|
87
|
-
// If the developer has provided an object of refresh handlers,
|
|
88
|
-
// then we can merge the provided handlers with the current handlers.
|
|
89
|
-
if (refreshHandlers) {
|
|
90
|
-
this._refreshHandlers = __assign(__assign({}, this._refreshHandlers), refreshHandlers);
|
|
91
|
-
}
|
|
92
|
-
this._storage = this._config.storage;
|
|
93
|
-
if (!this._storage) {
|
|
94
|
-
this._storage = new StorageHelper().getStorage();
|
|
95
|
-
}
|
|
96
|
-
this._storageSync = Promise.resolve();
|
|
97
|
-
if (typeof this._storage['sync'] === 'function') {
|
|
98
|
-
this._storageSync = this._storage['sync']();
|
|
99
|
-
}
|
|
100
|
-
dispatchCredentialsEvent('credentials_configured', null, "Credentials has been configured successfully");
|
|
101
|
-
return this._config;
|
|
102
|
-
};
|
|
103
|
-
CredentialsClass.prototype.get = function () {
|
|
104
|
-
logger.debug('getting credentials');
|
|
105
|
-
return this._pickupCredentials();
|
|
106
|
-
};
|
|
107
|
-
// currently we only store the guest identity in local storage
|
|
108
|
-
CredentialsClass.prototype._getCognitoIdentityIdStorageKey = function (identityPoolId) {
|
|
109
|
-
return "".concat(COGNITO_IDENTITY_KEY_PREFIX).concat(identityPoolId);
|
|
110
|
-
};
|
|
111
|
-
CredentialsClass.prototype._pickupCredentials = function () {
|
|
112
|
-
logger.debug('picking up credentials');
|
|
113
|
-
if (!this._gettingCredPromise || !this._gettingCredPromise.isPending()) {
|
|
114
|
-
logger.debug('getting new cred promise');
|
|
115
|
-
this._gettingCredPromise = makeQuerablePromise(this._keepAlive());
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
logger.debug('getting old cred promise');
|
|
119
|
-
}
|
|
120
|
-
return this._gettingCredPromise;
|
|
121
|
-
};
|
|
122
|
-
CredentialsClass.prototype._keepAlive = function () {
|
|
123
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
124
|
-
var cred, _a, Auth, user_1, session, refreshToken_1, refreshRequest, err_1;
|
|
125
|
-
return __generator(this, function (_b) {
|
|
126
|
-
switch (_b.label) {
|
|
127
|
-
case 0:
|
|
128
|
-
logger.debug('checking if credentials exists and not expired');
|
|
129
|
-
cred = this._credentials;
|
|
130
|
-
if (cred && !this._isExpired(cred) && !this._isPastTTL()) {
|
|
131
|
-
logger.debug('credentials not changed and not expired, directly return');
|
|
132
|
-
return [2 /*return*/, Promise.resolve(cred)];
|
|
133
|
-
}
|
|
134
|
-
logger.debug('need to get a new credential or refresh the existing one');
|
|
135
|
-
_a = this.Auth, Auth = _a === void 0 ? Amplify.Auth : _a;
|
|
136
|
-
if (!Auth || typeof Auth.currentUserCredentials !== 'function') {
|
|
137
|
-
// If Auth module is not imported, do a best effort to get guest credentials
|
|
138
|
-
return [2 /*return*/, this._setCredentialsForGuest()];
|
|
139
|
-
}
|
|
140
|
-
if (!(!this._isExpired(cred) && this._isPastTTL())) return [3 /*break*/, 6];
|
|
141
|
-
logger.debug('ttl has passed but token is not yet expired');
|
|
142
|
-
_b.label = 1;
|
|
143
|
-
case 1:
|
|
144
|
-
_b.trys.push([1, 5, , 6]);
|
|
145
|
-
return [4 /*yield*/, Auth.currentUserPoolUser()];
|
|
146
|
-
case 2:
|
|
147
|
-
user_1 = _b.sent();
|
|
148
|
-
return [4 /*yield*/, Auth.currentSession()];
|
|
149
|
-
case 3:
|
|
150
|
-
session = _b.sent();
|
|
151
|
-
refreshToken_1 = session.refreshToken;
|
|
152
|
-
refreshRequest = new Promise(function (res, rej) {
|
|
153
|
-
user_1.refreshSession(refreshToken_1, function (err, data) {
|
|
154
|
-
return err ? rej(err) : res(data);
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
return [4 /*yield*/, refreshRequest];
|
|
158
|
-
case 4:
|
|
159
|
-
_b.sent(); // note that rejections will be caught and handled in the catch block.
|
|
160
|
-
return [3 /*break*/, 6];
|
|
161
|
-
case 5:
|
|
162
|
-
err_1 = _b.sent();
|
|
163
|
-
// should not throw because user might just be on guest access or is authenticated through federation
|
|
164
|
-
logger.debug('Error attempting to refreshing the session', err_1);
|
|
165
|
-
return [3 /*break*/, 6];
|
|
166
|
-
case 6: return [2 /*return*/, Auth.currentUserCredentials()];
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
};
|
|
171
|
-
CredentialsClass.prototype.refreshFederatedToken = function (federatedInfo) {
|
|
172
|
-
logger.debug('Getting federated credentials');
|
|
173
|
-
var provider = federatedInfo.provider, user = federatedInfo.user, token = federatedInfo.token, identity_id = federatedInfo.identity_id;
|
|
174
|
-
var expires_at = federatedInfo.expires_at;
|
|
175
|
-
// Make sure expires_at is in millis
|
|
176
|
-
expires_at =
|
|
177
|
-
new Date(expires_at).getFullYear() === 1970
|
|
178
|
-
? expires_at * 1000
|
|
179
|
-
: expires_at;
|
|
180
|
-
var that = this;
|
|
181
|
-
logger.debug('checking if federated jwt token expired');
|
|
182
|
-
if (expires_at > new Date().getTime()) {
|
|
183
|
-
// if not expired
|
|
184
|
-
logger.debug('token not expired');
|
|
185
|
-
return this._setCredentialsFromFederation({
|
|
186
|
-
provider: provider,
|
|
187
|
-
token: token,
|
|
188
|
-
user: user,
|
|
189
|
-
identity_id: identity_id,
|
|
190
|
-
expires_at: expires_at,
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
else {
|
|
194
|
-
// if refresh handler exists
|
|
195
|
-
if (that._refreshHandlers[provider] &&
|
|
196
|
-
typeof that._refreshHandlers[provider] === 'function') {
|
|
197
|
-
logger.debug('getting refreshed jwt token from federation provider');
|
|
198
|
-
return this._providerRefreshWithRetry({
|
|
199
|
-
refreshHandler: that._refreshHandlers[provider],
|
|
200
|
-
provider: provider,
|
|
201
|
-
user: user,
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
else {
|
|
205
|
-
logger.debug('no refresh handler for provider:', provider);
|
|
206
|
-
this.clear();
|
|
207
|
-
return Promise.reject('no refresh handler for provider');
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
|
-
CredentialsClass.prototype._providerRefreshWithRetry = function (_a) {
|
|
212
|
-
var _this = this;
|
|
213
|
-
var refreshHandler = _a.refreshHandler, provider = _a.provider, user = _a.user;
|
|
214
|
-
var MAX_DELAY_MS = 10 * 1000;
|
|
215
|
-
// refreshHandler will retry network errors, otherwise it will
|
|
216
|
-
// return NonRetryableError to break out of jitteredExponentialRetry
|
|
217
|
-
return jitteredExponentialRetry(refreshHandler, [], MAX_DELAY_MS)
|
|
218
|
-
.then(function (data) {
|
|
219
|
-
logger.debug('refresh federated token sucessfully', data);
|
|
220
|
-
return _this._setCredentialsFromFederation({
|
|
221
|
-
provider: provider,
|
|
222
|
-
token: data.token,
|
|
223
|
-
user: user,
|
|
224
|
-
identity_id: data.identity_id,
|
|
225
|
-
expires_at: data.expires_at,
|
|
226
|
-
});
|
|
227
|
-
})
|
|
228
|
-
.catch(function (e) {
|
|
229
|
-
var isNetworkError = typeof e === 'string' &&
|
|
230
|
-
e.toLowerCase().lastIndexOf('network error', e.length) === 0;
|
|
231
|
-
if (!isNetworkError) {
|
|
232
|
-
_this.clear();
|
|
233
|
-
}
|
|
234
|
-
logger.debug('refresh federated token failed', e);
|
|
235
|
-
return Promise.reject('refreshing federation token failed: ' + e);
|
|
236
|
-
});
|
|
237
|
-
};
|
|
238
|
-
CredentialsClass.prototype._isExpired = function (credentials) {
|
|
239
|
-
if (!credentials) {
|
|
240
|
-
logger.debug('no credentials for expiration check');
|
|
241
|
-
return true;
|
|
242
|
-
}
|
|
243
|
-
logger.debug('are these credentials expired?', credentials);
|
|
244
|
-
var ts = Date.now();
|
|
245
|
-
/* returns date object.
|
|
246
|
-
https://github.com/aws/aws-sdk-js-v3/blob/v1.0.0-beta.1/packages/types/src/credentials.ts#L26
|
|
247
|
-
*/
|
|
248
|
-
var expiration = credentials.expiration;
|
|
249
|
-
return expiration.getTime() <= ts;
|
|
250
|
-
};
|
|
251
|
-
CredentialsClass.prototype._isPastTTL = function () {
|
|
252
|
-
return this._nextCredentialsRefresh <= Date.now();
|
|
253
|
-
};
|
|
254
|
-
CredentialsClass.prototype._setCredentialsForGuest = function () {
|
|
255
|
-
var _a;
|
|
256
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
257
|
-
var _b, identityPoolId, region, mandatorySignIn, identityPoolRegion, identityId, _c, cognitoConfig, guestCredentialsProvider, credentials;
|
|
258
|
-
var _this = this;
|
|
259
|
-
return __generator(this, function (_d) {
|
|
260
|
-
switch (_d.label) {
|
|
261
|
-
case 0:
|
|
262
|
-
logger.debug('setting credentials for guest');
|
|
263
|
-
if (!((_a = this._config) === null || _a === void 0 ? void 0 : _a.identityPoolId)) {
|
|
264
|
-
// If Credentials are not configured thru Auth module,
|
|
265
|
-
// doing best effort to check if the library was configured
|
|
266
|
-
this._config = Object.assign({}, this._config, parseAWSExports(this._config || {}).Auth);
|
|
267
|
-
}
|
|
268
|
-
_b = this._config, identityPoolId = _b.identityPoolId, region = _b.region, mandatorySignIn = _b.mandatorySignIn, identityPoolRegion = _b.identityPoolRegion;
|
|
269
|
-
if (mandatorySignIn) {
|
|
270
|
-
return [2 /*return*/, Promise.reject('cannot get guest credentials when mandatory signin enabled')];
|
|
271
|
-
}
|
|
272
|
-
if (!identityPoolId) {
|
|
273
|
-
logger.debug('No Cognito Identity pool provided for unauthenticated access');
|
|
274
|
-
return [2 /*return*/, Promise.reject('No Cognito Identity pool provided for unauthenticated access')];
|
|
275
|
-
}
|
|
276
|
-
if (!identityPoolRegion && !region) {
|
|
277
|
-
logger.debug('region is not configured for getting the credentials');
|
|
278
|
-
return [2 /*return*/, Promise.reject('region is not configured for getting the credentials')];
|
|
279
|
-
}
|
|
280
|
-
_c = this;
|
|
281
|
-
return [4 /*yield*/, this._getGuestIdentityId()];
|
|
282
|
-
case 1:
|
|
283
|
-
identityId = (_c._identityId = _d.sent());
|
|
284
|
-
cognitoConfig = { region: identityPoolRegion !== null && identityPoolRegion !== void 0 ? identityPoolRegion : region };
|
|
285
|
-
guestCredentialsProvider = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
286
|
-
var IdentityId, Credentials;
|
|
287
|
-
return __generator(this, function (_a) {
|
|
288
|
-
switch (_a.label) {
|
|
289
|
-
case 0:
|
|
290
|
-
if (!!identityId) return [3 /*break*/, 2];
|
|
291
|
-
return [4 /*yield*/, getId(cognitoConfig, {
|
|
292
|
-
IdentityPoolId: identityPoolId,
|
|
293
|
-
})];
|
|
294
|
-
case 1:
|
|
295
|
-
IdentityId = (_a.sent()).IdentityId;
|
|
296
|
-
this._identityId = IdentityId;
|
|
297
|
-
_a.label = 2;
|
|
298
|
-
case 2: return [4 /*yield*/, getCredentialsForIdentity(cognitoConfig, {
|
|
299
|
-
IdentityId: this._identityId,
|
|
300
|
-
})];
|
|
301
|
-
case 3:
|
|
302
|
-
Credentials = (_a.sent()).Credentials;
|
|
303
|
-
return [2 /*return*/, {
|
|
304
|
-
identityId: this._identityId,
|
|
305
|
-
accessKeyId: Credentials.AccessKeyId,
|
|
306
|
-
secretAccessKey: Credentials.SecretKey,
|
|
307
|
-
sessionToken: Credentials.SessionToken,
|
|
308
|
-
expiration: Credentials.Expiration,
|
|
309
|
-
}];
|
|
310
|
-
}
|
|
311
|
-
});
|
|
312
|
-
}); };
|
|
313
|
-
credentials = guestCredentialsProvider().catch(function (err) { return __awaiter(_this, void 0, void 0, function () {
|
|
314
|
-
return __generator(this, function (_a) {
|
|
315
|
-
throw err;
|
|
316
|
-
});
|
|
317
|
-
}); });
|
|
318
|
-
return [2 /*return*/, this._loadCredentials(credentials, 'guest', false, null)
|
|
319
|
-
.then(function (res) {
|
|
320
|
-
return res;
|
|
321
|
-
})
|
|
322
|
-
.catch(function (e) { return __awaiter(_this, void 0, void 0, function () {
|
|
323
|
-
var guestCredentialsProvider_1;
|
|
324
|
-
var _this = this;
|
|
325
|
-
return __generator(this, function (_a) {
|
|
326
|
-
switch (_a.label) {
|
|
327
|
-
case 0:
|
|
328
|
-
if (!(e.name === 'ResourceNotFoundException' &&
|
|
329
|
-
e.message === "Identity '".concat(identityId, "' not found."))) return [3 /*break*/, 2];
|
|
330
|
-
logger.debug('Failed to load guest credentials');
|
|
331
|
-
return [4 /*yield*/, this._removeGuestIdentityId()];
|
|
332
|
-
case 1:
|
|
333
|
-
_a.sent();
|
|
334
|
-
guestCredentialsProvider_1 = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
335
|
-
var IdentityId, Credentials;
|
|
336
|
-
return __generator(this, function (_a) {
|
|
337
|
-
switch (_a.label) {
|
|
338
|
-
case 0: return [4 /*yield*/, getId(cognitoConfig, {
|
|
339
|
-
IdentityPoolId: identityPoolId,
|
|
340
|
-
})];
|
|
341
|
-
case 1:
|
|
342
|
-
IdentityId = (_a.sent()).IdentityId;
|
|
343
|
-
this._identityId = IdentityId;
|
|
344
|
-
return [4 /*yield*/, getCredentialsForIdentity(cognitoConfig, {
|
|
345
|
-
IdentityId: IdentityId,
|
|
346
|
-
})];
|
|
347
|
-
case 2:
|
|
348
|
-
Credentials = (_a.sent()).Credentials;
|
|
349
|
-
return [2 /*return*/, {
|
|
350
|
-
identityId: IdentityId,
|
|
351
|
-
accessKeyId: Credentials.AccessKeyId,
|
|
352
|
-
secretAccessKey: Credentials.SecretKey,
|
|
353
|
-
sessionToken: Credentials.SessionToken,
|
|
354
|
-
expiration: Credentials.Expiration,
|
|
355
|
-
}];
|
|
356
|
-
}
|
|
357
|
-
});
|
|
358
|
-
}); };
|
|
359
|
-
credentials = guestCredentialsProvider_1().catch(function (err) { return __awaiter(_this, void 0, void 0, function () {
|
|
360
|
-
return __generator(this, function (_a) {
|
|
361
|
-
throw err;
|
|
362
|
-
});
|
|
363
|
-
}); });
|
|
364
|
-
return [2 /*return*/, this._loadCredentials(credentials, 'guest', false, null)];
|
|
365
|
-
case 2: return [2 /*return*/, e];
|
|
366
|
-
}
|
|
367
|
-
});
|
|
368
|
-
}); })];
|
|
369
|
-
}
|
|
370
|
-
});
|
|
371
|
-
});
|
|
372
|
-
};
|
|
373
|
-
CredentialsClass.prototype._setCredentialsFromFederation = function (params) {
|
|
374
|
-
var _this = this;
|
|
375
|
-
var provider = params.provider, token = params.token;
|
|
376
|
-
var identity_id = params.identity_id;
|
|
377
|
-
var domains = {
|
|
378
|
-
google: 'accounts.google.com',
|
|
379
|
-
facebook: 'graph.facebook.com',
|
|
380
|
-
amazon: 'www.amazon.com',
|
|
381
|
-
developer: 'cognito-identity.amazonaws.com',
|
|
382
|
-
};
|
|
383
|
-
// Use custom provider url instead of the predefined ones
|
|
384
|
-
var domain = domains[provider] || provider;
|
|
385
|
-
if (!domain) {
|
|
386
|
-
return Promise.reject('You must specify a federated provider');
|
|
387
|
-
}
|
|
388
|
-
var logins = {};
|
|
389
|
-
logins[domain] = token;
|
|
390
|
-
var _a = this._config, identityPoolId = _a.identityPoolId, region = _a.region, identityPoolRegion = _a.identityPoolRegion;
|
|
391
|
-
if (!identityPoolId) {
|
|
392
|
-
logger.debug('No Cognito Federated Identity pool provided');
|
|
393
|
-
return Promise.reject('No Cognito Federated Identity pool provided');
|
|
394
|
-
}
|
|
395
|
-
if (!identityPoolRegion && !region) {
|
|
396
|
-
logger.debug('region is not configured for getting the credentials');
|
|
397
|
-
return Promise.reject('region is not configured for getting the credentials');
|
|
398
|
-
}
|
|
399
|
-
var cognitoConfig = { region: identityPoolRegion !== null && identityPoolRegion !== void 0 ? identityPoolRegion : region };
|
|
400
|
-
var authenticatedCredentialsProvider = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
401
|
-
var IdentityId, Credentials;
|
|
402
|
-
return __generator(this, function (_a) {
|
|
403
|
-
switch (_a.label) {
|
|
404
|
-
case 0:
|
|
405
|
-
if (!!identity_id) return [3 /*break*/, 2];
|
|
406
|
-
return [4 /*yield*/, getId(cognitoConfig, {
|
|
407
|
-
IdentityPoolId: identityPoolId,
|
|
408
|
-
Logins: logins,
|
|
409
|
-
})];
|
|
410
|
-
case 1:
|
|
411
|
-
IdentityId = (_a.sent()).IdentityId;
|
|
412
|
-
identity_id = IdentityId;
|
|
413
|
-
_a.label = 2;
|
|
414
|
-
case 2: return [4 /*yield*/, getCredentialsForIdentity(cognitoConfig, {
|
|
415
|
-
IdentityId: identity_id,
|
|
416
|
-
Logins: logins,
|
|
417
|
-
})];
|
|
418
|
-
case 3:
|
|
419
|
-
Credentials = (_a.sent()).Credentials;
|
|
420
|
-
return [2 /*return*/, {
|
|
421
|
-
identityId: identity_id,
|
|
422
|
-
accessKeyId: Credentials.AccessKeyId,
|
|
423
|
-
secretAccessKey: Credentials.SecretKey,
|
|
424
|
-
sessionToken: Credentials.SessionToken,
|
|
425
|
-
expiration: Credentials.Expiration,
|
|
426
|
-
}];
|
|
427
|
-
}
|
|
428
|
-
});
|
|
429
|
-
}); };
|
|
430
|
-
var credentials = authenticatedCredentialsProvider().catch(function (err) { return __awaiter(_this, void 0, void 0, function () {
|
|
431
|
-
return __generator(this, function (_a) {
|
|
432
|
-
throw err;
|
|
433
|
-
});
|
|
434
|
-
}); });
|
|
435
|
-
return this._loadCredentials(credentials, 'federated', true, params);
|
|
436
|
-
};
|
|
437
|
-
CredentialsClass.prototype._setCredentialsFromSession = function (session) {
|
|
438
|
-
var _this = this;
|
|
439
|
-
logger.debug('set credentials from session');
|
|
440
|
-
var idToken = session.getIdToken().getJwtToken();
|
|
441
|
-
var _a = this._config, region = _a.region, userPoolId = _a.userPoolId, identityPoolId = _a.identityPoolId, identityPoolRegion = _a.identityPoolRegion;
|
|
442
|
-
if (!identityPoolId) {
|
|
443
|
-
logger.debug('No Cognito Federated Identity pool provided');
|
|
444
|
-
return Promise.reject('No Cognito Federated Identity pool provided');
|
|
445
|
-
}
|
|
446
|
-
if (!identityPoolRegion && !region) {
|
|
447
|
-
logger.debug('region is not configured for getting the credentials');
|
|
448
|
-
return Promise.reject('region is not configured for getting the credentials');
|
|
449
|
-
}
|
|
450
|
-
var key = 'cognito-idp.' + region + '.amazonaws.com/' + userPoolId;
|
|
451
|
-
var logins = {};
|
|
452
|
-
logins[key] = idToken;
|
|
453
|
-
var cognitoConfig = { region: identityPoolRegion !== null && identityPoolRegion !== void 0 ? identityPoolRegion : region };
|
|
454
|
-
/*
|
|
455
|
-
Retreiving identityId with GetIdCommand to mimic the behavior in the following code in aws-sdk-v3:
|
|
456
|
-
https://git.io/JeDxU
|
|
457
|
-
|
|
458
|
-
Note: Retreive identityId from CredentialsProvider once aws-sdk-js v3 supports this.
|
|
459
|
-
*/
|
|
460
|
-
var credentialsProvider = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
461
|
-
var guestIdentityId, generatedOrRetrievedIdentityId, IdentityId, _a, _b, AccessKeyId, Expiration, SecretKey, SessionToken, primaryIdentityId;
|
|
462
|
-
return __generator(this, function (_c) {
|
|
463
|
-
switch (_c.label) {
|
|
464
|
-
case 0: return [4 /*yield*/, this._getGuestIdentityId()];
|
|
465
|
-
case 1:
|
|
466
|
-
guestIdentityId = _c.sent();
|
|
467
|
-
if (!!guestIdentityId) return [3 /*break*/, 3];
|
|
468
|
-
return [4 /*yield*/, getId(cognitoConfig, {
|
|
469
|
-
IdentityPoolId: identityPoolId,
|
|
470
|
-
Logins: logins,
|
|
471
|
-
})];
|
|
472
|
-
case 2:
|
|
473
|
-
IdentityId = (_c.sent()).IdentityId;
|
|
474
|
-
generatedOrRetrievedIdentityId = IdentityId;
|
|
475
|
-
_c.label = 3;
|
|
476
|
-
case 3: return [4 /*yield*/, getCredentialsForIdentity(cognitoConfig, {
|
|
477
|
-
IdentityId: guestIdentityId || generatedOrRetrievedIdentityId,
|
|
478
|
-
Logins: logins,
|
|
479
|
-
})];
|
|
480
|
-
case 4:
|
|
481
|
-
_a = (_c.sent()), _b = _a.Credentials, AccessKeyId = _b.AccessKeyId, Expiration = _b.Expiration, SecretKey = _b.SecretKey, SessionToken = _b.SessionToken, primaryIdentityId = _a.IdentityId;
|
|
482
|
-
this._identityId = primaryIdentityId;
|
|
483
|
-
if (!guestIdentityId) return [3 /*break*/, 6];
|
|
484
|
-
// if guestIdentity is found and used by GetCredentialsForIdentity
|
|
485
|
-
// it will be linked to the logins provided, and disqualified as an unauth identity
|
|
486
|
-
logger.debug("The guest identity ".concat(guestIdentityId, " has been successfully linked to the logins"));
|
|
487
|
-
if (guestIdentityId === primaryIdentityId) {
|
|
488
|
-
logger.debug("The guest identity ".concat(guestIdentityId, " has become the primary identity"));
|
|
489
|
-
}
|
|
490
|
-
// remove it from local storage to avoid being used as a guest Identity by _setCredentialsForGuest
|
|
491
|
-
return [4 /*yield*/, this._removeGuestIdentityId()];
|
|
492
|
-
case 5:
|
|
493
|
-
// remove it from local storage to avoid being used as a guest Identity by _setCredentialsForGuest
|
|
494
|
-
_c.sent();
|
|
495
|
-
_c.label = 6;
|
|
496
|
-
case 6:
|
|
497
|
-
// https://github.com/aws/aws-sdk-js-v3/blob/main/packages/credential-provider-cognito-identity/src/fromCognitoIdentity.ts#L40
|
|
498
|
-
return [2 /*return*/, {
|
|
499
|
-
accessKeyId: AccessKeyId,
|
|
500
|
-
secretAccessKey: SecretKey,
|
|
501
|
-
sessionToken: SessionToken,
|
|
502
|
-
expiration: Expiration,
|
|
503
|
-
identityId: primaryIdentityId,
|
|
504
|
-
}];
|
|
505
|
-
}
|
|
506
|
-
});
|
|
507
|
-
}); };
|
|
508
|
-
var credentials = credentialsProvider().catch(function (err) { return __awaiter(_this, void 0, void 0, function () {
|
|
509
|
-
return __generator(this, function (_a) {
|
|
510
|
-
throw err;
|
|
511
|
-
});
|
|
512
|
-
}); });
|
|
513
|
-
return this._loadCredentials(credentials, 'userPool', true, null);
|
|
514
|
-
};
|
|
515
|
-
CredentialsClass.prototype._loadCredentials = function (credentials, source, authenticated, info) {
|
|
516
|
-
var _this = this;
|
|
517
|
-
var that = this;
|
|
518
|
-
return new Promise(function (res, rej) {
|
|
519
|
-
credentials
|
|
520
|
-
.then(function (credentials) { return __awaiter(_this, void 0, void 0, function () {
|
|
521
|
-
var user, provider, token, expires_at, identity_id;
|
|
522
|
-
return __generator(this, function (_a) {
|
|
523
|
-
switch (_a.label) {
|
|
524
|
-
case 0:
|
|
525
|
-
logger.debug('Load credentials successfully', credentials);
|
|
526
|
-
if (this._identityId && !credentials.identityId) {
|
|
527
|
-
credentials['identityId'] = this._identityId;
|
|
528
|
-
}
|
|
529
|
-
that._credentials = credentials;
|
|
530
|
-
that._credentials.authenticated = authenticated;
|
|
531
|
-
that._credentials_source = source;
|
|
532
|
-
that._nextCredentialsRefresh = new Date().getTime() + CREDENTIALS_TTL;
|
|
533
|
-
if (source === 'federated') {
|
|
534
|
-
user = Object.assign({ id: this._credentials.identityId }, info.user);
|
|
535
|
-
provider = info.provider, token = info.token, expires_at = info.expires_at, identity_id = info.identity_id;
|
|
536
|
-
try {
|
|
537
|
-
this._storage.setItem('aws-amplify-federatedInfo', JSON.stringify({
|
|
538
|
-
provider: provider,
|
|
539
|
-
token: token,
|
|
540
|
-
user: user,
|
|
541
|
-
expires_at: expires_at,
|
|
542
|
-
identity_id: identity_id,
|
|
543
|
-
}));
|
|
544
|
-
}
|
|
545
|
-
catch (e) {
|
|
546
|
-
logger.debug('Failed to put federated info into auth storage', e);
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
if (!(source === 'guest')) return [3 /*break*/, 2];
|
|
550
|
-
return [4 /*yield*/, this._setGuestIdentityId(credentials.identityId)];
|
|
551
|
-
case 1:
|
|
552
|
-
_a.sent();
|
|
553
|
-
_a.label = 2;
|
|
554
|
-
case 2:
|
|
555
|
-
res(that._credentials);
|
|
556
|
-
return [2 /*return*/];
|
|
557
|
-
}
|
|
558
|
-
});
|
|
559
|
-
}); })
|
|
560
|
-
.catch(function (err) {
|
|
561
|
-
if (err) {
|
|
562
|
-
logger.debug('Failed to load credentials', credentials);
|
|
563
|
-
logger.debug('Error loading credentials', err);
|
|
564
|
-
rej(err);
|
|
565
|
-
return;
|
|
566
|
-
}
|
|
567
|
-
});
|
|
568
|
-
});
|
|
569
|
-
};
|
|
570
|
-
CredentialsClass.prototype.set = function (params, source) {
|
|
571
|
-
if (source === 'session') {
|
|
572
|
-
return this._setCredentialsFromSession(params);
|
|
573
|
-
}
|
|
574
|
-
else if (source === 'federation') {
|
|
575
|
-
return this._setCredentialsFromFederation(params);
|
|
576
|
-
}
|
|
577
|
-
else if (source === 'guest') {
|
|
578
|
-
return this._setCredentialsForGuest();
|
|
579
|
-
}
|
|
580
|
-
else {
|
|
581
|
-
logger.debug('no source specified for setting credentials');
|
|
582
|
-
return Promise.reject('invalid source');
|
|
583
|
-
}
|
|
584
|
-
};
|
|
585
|
-
CredentialsClass.prototype.clear = function () {
|
|
586
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
587
|
-
return __generator(this, function (_a) {
|
|
588
|
-
this._credentials = null;
|
|
589
|
-
this._credentials_source = null;
|
|
590
|
-
logger.debug('removing aws-amplify-federatedInfo from storage');
|
|
591
|
-
this._storage.removeItem('aws-amplify-federatedInfo');
|
|
592
|
-
return [2 /*return*/];
|
|
593
|
-
});
|
|
594
|
-
});
|
|
595
|
-
};
|
|
596
|
-
/* operations on local stored guest identity */
|
|
597
|
-
CredentialsClass.prototype._getGuestIdentityId = function () {
|
|
598
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
599
|
-
var identityPoolId, e_1;
|
|
600
|
-
return __generator(this, function (_a) {
|
|
601
|
-
switch (_a.label) {
|
|
602
|
-
case 0:
|
|
603
|
-
identityPoolId = this._config.identityPoolId;
|
|
604
|
-
_a.label = 1;
|
|
605
|
-
case 1:
|
|
606
|
-
_a.trys.push([1, 3, , 4]);
|
|
607
|
-
return [4 /*yield*/, this._storageSync];
|
|
608
|
-
case 2:
|
|
609
|
-
_a.sent();
|
|
610
|
-
return [2 /*return*/, this._storage.getItem(this._getCognitoIdentityIdStorageKey(identityPoolId))];
|
|
611
|
-
case 3:
|
|
612
|
-
e_1 = _a.sent();
|
|
613
|
-
logger.debug('Failed to get the cached guest identityId', e_1);
|
|
614
|
-
return [3 /*break*/, 4];
|
|
615
|
-
case 4: return [2 /*return*/];
|
|
616
|
-
}
|
|
617
|
-
});
|
|
618
|
-
});
|
|
619
|
-
};
|
|
620
|
-
CredentialsClass.prototype._setGuestIdentityId = function (identityId) {
|
|
621
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
622
|
-
var identityPoolId, e_2;
|
|
623
|
-
return __generator(this, function (_a) {
|
|
624
|
-
switch (_a.label) {
|
|
625
|
-
case 0:
|
|
626
|
-
identityPoolId = this._config.identityPoolId;
|
|
627
|
-
_a.label = 1;
|
|
628
|
-
case 1:
|
|
629
|
-
_a.trys.push([1, 3, , 4]);
|
|
630
|
-
return [4 /*yield*/, this._storageSync];
|
|
631
|
-
case 2:
|
|
632
|
-
_a.sent();
|
|
633
|
-
this._storage.setItem(this._getCognitoIdentityIdStorageKey(identityPoolId), identityId);
|
|
634
|
-
return [3 /*break*/, 4];
|
|
635
|
-
case 3:
|
|
636
|
-
e_2 = _a.sent();
|
|
637
|
-
logger.debug('Failed to cache guest identityId', e_2);
|
|
638
|
-
return [3 /*break*/, 4];
|
|
639
|
-
case 4: return [2 /*return*/];
|
|
640
|
-
}
|
|
641
|
-
});
|
|
642
|
-
});
|
|
643
|
-
};
|
|
644
|
-
CredentialsClass.prototype._removeGuestIdentityId = function () {
|
|
645
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
646
|
-
var identityPoolId;
|
|
647
|
-
return __generator(this, function (_a) {
|
|
648
|
-
identityPoolId = this._config.identityPoolId;
|
|
649
|
-
logger.debug("removing ".concat(this._getCognitoIdentityIdStorageKey(identityPoolId), " from storage"));
|
|
650
|
-
this._storage.removeItem(this._getCognitoIdentityIdStorageKey(identityPoolId));
|
|
651
|
-
return [2 /*return*/];
|
|
652
|
-
});
|
|
653
|
-
});
|
|
654
|
-
};
|
|
655
|
-
/**
|
|
656
|
-
* Compact version of credentials
|
|
657
|
-
* @param {Object} credentials
|
|
658
|
-
* @return {Object} - Credentials
|
|
659
|
-
*/
|
|
660
|
-
CredentialsClass.prototype.shear = function (credentials) {
|
|
661
|
-
return {
|
|
662
|
-
accessKeyId: credentials.accessKeyId,
|
|
663
|
-
sessionToken: credentials.sessionToken,
|
|
664
|
-
secretAccessKey: credentials.secretAccessKey,
|
|
665
|
-
identityId: credentials.identityId,
|
|
666
|
-
authenticated: credentials.authenticated,
|
|
667
|
-
};
|
|
668
|
-
};
|
|
669
|
-
return CredentialsClass;
|
|
670
|
-
}());
|
|
671
|
-
export { CredentialsClass };
|
|
672
|
-
export var Credentials = new CredentialsClass(null);
|
|
673
|
-
Amplify.register(Credentials);
|