@aws-amplify/auth 5.6.1 → 5.6.2-unstable.bee9c8e.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/OAuth/OAuth.d.ts +1 -1
- package/lib/OAuth/OAuth.js +5 -9
- package/lib/OAuth/OAuth.js.map +1 -1
- package/lib/internals/InternalAuth.d.ts +13 -2
- package/lib/internals/InternalAuth.js +274 -197
- package/lib/internals/InternalAuth.js.map +1 -1
- package/lib/utils.d.ts +3 -0
- package/lib/utils.js +15 -0
- package/lib/utils.js.map +1 -0
- package/lib-esm/OAuth/OAuth.d.ts +1 -1
- package/lib-esm/OAuth/OAuth.js +6 -10
- package/lib-esm/OAuth/OAuth.js.map +1 -1
- package/lib-esm/internals/InternalAuth.d.ts +13 -2
- package/lib-esm/internals/InternalAuth.js +277 -200
- package/lib-esm/internals/InternalAuth.js.map +1 -1
- package/lib-esm/utils.d.ts +3 -0
- package/lib-esm/utils.js +11 -0
- package/lib-esm/utils.js.map +1 -0
- package/package.json +5 -5
- package/src/OAuth/OAuth.ts +10 -9
- package/src/internals/InternalAuth.ts +681 -312
- package/src/utils.ts +27 -0
|
@@ -25,9 +25,11 @@ import {
|
|
|
25
25
|
|
|
26
26
|
import {
|
|
27
27
|
Amplify,
|
|
28
|
+
AuthAction,
|
|
28
29
|
ConsoleLogger as Logger,
|
|
29
30
|
Credentials,
|
|
30
31
|
CustomUserAgentDetails,
|
|
32
|
+
getAmplifyUserAgent,
|
|
31
33
|
Hub,
|
|
32
34
|
StorageHelper,
|
|
33
35
|
ICredentials,
|
|
@@ -40,7 +42,6 @@ import {
|
|
|
40
42
|
} from '@aws-amplify/core';
|
|
41
43
|
import {
|
|
42
44
|
CookieStorage,
|
|
43
|
-
CognitoUserPool,
|
|
44
45
|
AuthenticationDetails,
|
|
45
46
|
ICognitoUserPoolData,
|
|
46
47
|
ICognitoUserData,
|
|
@@ -60,6 +61,8 @@ import {
|
|
|
60
61
|
import {
|
|
61
62
|
addAuthCategoryToCognitoUserAgent,
|
|
62
63
|
addFrameworkToCognitoUserAgent,
|
|
64
|
+
InternalCognitoUser,
|
|
65
|
+
InternalCognitoUserPool,
|
|
63
66
|
} from 'amazon-cognito-identity-js/internals';
|
|
64
67
|
|
|
65
68
|
import { parse } from 'url';
|
|
@@ -72,6 +75,7 @@ import {
|
|
|
72
75
|
CognitoHostedUIIdentityProvider,
|
|
73
76
|
IAuthDevice,
|
|
74
77
|
} from '../types/Auth';
|
|
78
|
+
import { getAuthUserAgentDetails, getAuthUserAgentValue } from '../utils';
|
|
75
79
|
|
|
76
80
|
const logger = new Logger('AuthClass');
|
|
77
81
|
const USER_ADMIN_SCOPE = 'aws.cognito.signin.user.admin';
|
|
@@ -101,7 +105,7 @@ const MAX_AUTOSIGNIN_POLLING_MS = 3 * 60 * 1000;
|
|
|
101
105
|
*/
|
|
102
106
|
export class InternalAuthClass {
|
|
103
107
|
private _config: AuthOptions;
|
|
104
|
-
private userPool:
|
|
108
|
+
private userPool: InternalCognitoUserPool = null;
|
|
105
109
|
private user: any = null;
|
|
106
110
|
private _oAuthHandler: OAuth;
|
|
107
111
|
private _storage;
|
|
@@ -205,7 +209,7 @@ export class InternalAuthClass {
|
|
|
205
209
|
};
|
|
206
210
|
userPoolData.Storage = this._storage;
|
|
207
211
|
|
|
208
|
-
this.userPool = new
|
|
212
|
+
this.userPool = new InternalCognitoUserPool(
|
|
209
213
|
userPoolData,
|
|
210
214
|
this.wrapRefreshSessionCallback
|
|
211
215
|
);
|
|
@@ -256,14 +260,17 @@ export class InternalAuthClass {
|
|
|
256
260
|
// Prevents _handleAuthResponse from being called multiple times in Expo
|
|
257
261
|
// See https://github.com/aws-amplify/amplify-js/issues/4388
|
|
258
262
|
const usedResponseUrls = {};
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
+
// Only register urlListener once
|
|
264
|
+
if (this.getModuleName() === 'InternalAuth') {
|
|
265
|
+
urlListener(({ url }) => {
|
|
266
|
+
if (usedResponseUrls[url]) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
263
269
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
270
|
+
usedResponseUrls[url] = true;
|
|
271
|
+
this._handleAuthResponse(url);
|
|
272
|
+
});
|
|
273
|
+
}
|
|
267
274
|
}
|
|
268
275
|
|
|
269
276
|
dispatchAuthEvent(
|
|
@@ -408,6 +415,10 @@ export class InternalAuthClass {
|
|
|
408
415
|
logger.debug('signUp validation data:', validationData);
|
|
409
416
|
|
|
410
417
|
return new Promise((resolve, reject) => {
|
|
418
|
+
const userAgentDetails = getAuthUserAgentDetails(
|
|
419
|
+
AuthAction.SignUp,
|
|
420
|
+
customUserAgentDetails
|
|
421
|
+
);
|
|
411
422
|
this.userPool.signUp(
|
|
412
423
|
username,
|
|
413
424
|
password,
|
|
@@ -433,13 +444,15 @@ export class InternalAuthClass {
|
|
|
433
444
|
password,
|
|
434
445
|
autoSignInValidationData,
|
|
435
446
|
autoSignInClientMetaData,
|
|
436
|
-
data
|
|
447
|
+
data,
|
|
448
|
+
userAgentDetails
|
|
437
449
|
);
|
|
438
450
|
}
|
|
439
451
|
resolve(data);
|
|
440
452
|
}
|
|
441
453
|
},
|
|
442
|
-
clientMetadata
|
|
454
|
+
clientMetadata,
|
|
455
|
+
getAmplifyUserAgent(userAgentDetails)
|
|
443
456
|
);
|
|
444
457
|
});
|
|
445
458
|
}
|
|
@@ -449,7 +462,8 @@ export class InternalAuthClass {
|
|
|
449
462
|
password: string,
|
|
450
463
|
validationData: {},
|
|
451
464
|
clientMetadata: any,
|
|
452
|
-
data: any
|
|
465
|
+
data: any,
|
|
466
|
+
customUserAgentDetails: CustomUserAgentDetails
|
|
453
467
|
) {
|
|
454
468
|
this.autoSignInInitiated = true;
|
|
455
469
|
const authDetails = new AuthenticationDetails({
|
|
@@ -459,24 +473,34 @@ export class InternalAuthClass {
|
|
|
459
473
|
ClientMetadata: clientMetadata,
|
|
460
474
|
});
|
|
461
475
|
if (data.userConfirmed) {
|
|
462
|
-
this.signInAfterUserConfirmed(authDetails);
|
|
476
|
+
this.signInAfterUserConfirmed(authDetails, customUserAgentDetails);
|
|
463
477
|
} else if (this._config.signUpVerificationMethod === 'link') {
|
|
464
|
-
this.handleLinkAutoSignIn(authDetails);
|
|
478
|
+
this.handleLinkAutoSignIn(authDetails, customUserAgentDetails);
|
|
465
479
|
} else {
|
|
466
|
-
this.handleCodeAutoSignIn(authDetails);
|
|
480
|
+
this.handleCodeAutoSignIn(authDetails, customUserAgentDetails);
|
|
467
481
|
}
|
|
468
482
|
}
|
|
469
483
|
|
|
470
|
-
private handleCodeAutoSignIn(
|
|
484
|
+
private handleCodeAutoSignIn(
|
|
485
|
+
authDetails: AuthenticationDetails,
|
|
486
|
+
customUserAgentDetails: CustomUserAgentDetails
|
|
487
|
+
) {
|
|
471
488
|
const listenEvent = ({ payload }) => {
|
|
472
489
|
if (payload.event === 'confirmSignUp') {
|
|
473
|
-
this.signInAfterUserConfirmed(
|
|
490
|
+
this.signInAfterUserConfirmed(
|
|
491
|
+
authDetails,
|
|
492
|
+
customUserAgentDetails,
|
|
493
|
+
listenEvent
|
|
494
|
+
);
|
|
474
495
|
}
|
|
475
496
|
};
|
|
476
497
|
Hub.listen('auth', listenEvent);
|
|
477
498
|
}
|
|
478
499
|
|
|
479
|
-
private handleLinkAutoSignIn(
|
|
500
|
+
private handleLinkAutoSignIn(
|
|
501
|
+
authDetails: AuthenticationDetails,
|
|
502
|
+
customUserAgentDetails: CustomUserAgentDetails
|
|
503
|
+
) {
|
|
480
504
|
this._storage.setItem('amplify-polling-started', 'true');
|
|
481
505
|
const start = Date.now();
|
|
482
506
|
const autoSignInPollingIntervalId = setInterval(() => {
|
|
@@ -491,7 +515,8 @@ export class InternalAuthClass {
|
|
|
491
515
|
} else {
|
|
492
516
|
this.signInAfterUserConfirmed(
|
|
493
517
|
authDetails,
|
|
494
|
-
|
|
518
|
+
customUserAgentDetails,
|
|
519
|
+
undefined,
|
|
495
520
|
autoSignInPollingIntervalId
|
|
496
521
|
);
|
|
497
522
|
}
|
|
@@ -500,6 +525,7 @@ export class InternalAuthClass {
|
|
|
500
525
|
|
|
501
526
|
private async signInAfterUserConfirmed(
|
|
502
527
|
authDetails: AuthenticationDetails,
|
|
528
|
+
customUserAgentDetails: CustomUserAgentDetails,
|
|
503
529
|
listenEvent?: HubCallback,
|
|
504
530
|
autoSignInPollingIntervalId?: ReturnType<typeof setInterval>
|
|
505
531
|
) {
|
|
@@ -527,8 +553,10 @@ export class InternalAuthClass {
|
|
|
527
553
|
error => {
|
|
528
554
|
logger.error(error);
|
|
529
555
|
this._storage.removeItem('amplify-auto-sign-in');
|
|
530
|
-
}
|
|
531
|
-
|
|
556
|
+
},
|
|
557
|
+
customUserAgentDetails
|
|
558
|
+
),
|
|
559
|
+
getAmplifyUserAgent(customUserAgentDetails)
|
|
532
560
|
);
|
|
533
561
|
} catch (error) {
|
|
534
562
|
logger.error(error);
|
|
@@ -596,7 +624,8 @@ export class InternalAuthClass {
|
|
|
596
624
|
resolve(data);
|
|
597
625
|
}
|
|
598
626
|
},
|
|
599
|
-
clientMetadata
|
|
627
|
+
clientMetadata,
|
|
628
|
+
getAuthUserAgentValue(AuthAction.ConfirmSignUp, customUserAgentDetails)
|
|
600
629
|
);
|
|
601
630
|
});
|
|
602
631
|
}
|
|
@@ -627,13 +656,17 @@ export class InternalAuthClass {
|
|
|
627
656
|
|
|
628
657
|
const user = this.createCognitoUser(username);
|
|
629
658
|
return new Promise((resolve, reject) => {
|
|
630
|
-
user.resendConfirmationCode(
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
659
|
+
user.resendConfirmationCode(
|
|
660
|
+
(err, data) => {
|
|
661
|
+
if (err) {
|
|
662
|
+
reject(err);
|
|
663
|
+
} else {
|
|
664
|
+
resolve(data);
|
|
665
|
+
}
|
|
666
|
+
},
|
|
667
|
+
clientMetadata,
|
|
668
|
+
getAuthUserAgentValue(AuthAction.ResendSignUp, customUserAgentDetails)
|
|
669
|
+
);
|
|
637
670
|
});
|
|
638
671
|
}
|
|
639
672
|
|
|
@@ -683,24 +716,29 @@ export class InternalAuthClass {
|
|
|
683
716
|
ValidationData: validationData,
|
|
684
717
|
ClientMetadata: clientMetadata,
|
|
685
718
|
});
|
|
719
|
+
const userAgentDetails = getAuthUserAgentDetails(
|
|
720
|
+
AuthAction.SignIn,
|
|
721
|
+
customUserAgentDetails
|
|
722
|
+
);
|
|
686
723
|
if (password) {
|
|
687
|
-
return this.signInWithPassword(authDetails);
|
|
724
|
+
return this.signInWithPassword(authDetails, userAgentDetails);
|
|
688
725
|
} else {
|
|
689
|
-
return this.signInWithoutPassword(authDetails);
|
|
726
|
+
return this.signInWithoutPassword(authDetails, userAgentDetails);
|
|
690
727
|
}
|
|
691
728
|
}
|
|
692
729
|
|
|
693
730
|
/**
|
|
694
731
|
* Return an object with the authentication callbacks
|
|
695
|
-
* @param {
|
|
732
|
+
* @param {InternalCognitoUser} user - the cognito user object
|
|
696
733
|
* @param {} resolve - function called when resolving the current step
|
|
697
734
|
* @param {} reject - function called when rejecting the current step
|
|
698
735
|
* @return - an object with the callback methods for user authentication
|
|
699
736
|
*/
|
|
700
737
|
private authCallbacks(
|
|
701
|
-
user:
|
|
702
|
-
resolve: (value?:
|
|
703
|
-
reject: (value?: any) => void
|
|
738
|
+
user: InternalCognitoUser,
|
|
739
|
+
resolve: (value?: InternalCognitoUser | any) => void,
|
|
740
|
+
reject: (value?: any) => void,
|
|
741
|
+
customUserAgentDetails: CustomUserAgentDetails
|
|
704
742
|
): IAuthenticationCallback {
|
|
705
743
|
const that = this;
|
|
706
744
|
return {
|
|
@@ -718,7 +756,10 @@ export class InternalAuthClass {
|
|
|
718
756
|
try {
|
|
719
757
|
// In order to get user attributes and MFA methods
|
|
720
758
|
// We need to trigger currentUserPoolUser again
|
|
721
|
-
const currentUser = await this.
|
|
759
|
+
const currentUser = await this._currentUserPoolUser(
|
|
760
|
+
undefined,
|
|
761
|
+
customUserAgentDetails
|
|
762
|
+
);
|
|
722
763
|
that.user = currentUser;
|
|
723
764
|
dispatchAuthEvent(
|
|
724
765
|
'signIn',
|
|
@@ -787,11 +828,13 @@ export class InternalAuthClass {
|
|
|
787
828
|
* Sign in with a password
|
|
788
829
|
* @private
|
|
789
830
|
* @param {AuthenticationDetails} authDetails - the user sign in data
|
|
831
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
790
832
|
* @return - A promise resolves the CognitoUser object if success or mfa required
|
|
791
833
|
*/
|
|
792
834
|
private signInWithPassword(
|
|
793
|
-
authDetails: AuthenticationDetails
|
|
794
|
-
|
|
835
|
+
authDetails: AuthenticationDetails,
|
|
836
|
+
customUserAgentDetails: CustomUserAgentDetails
|
|
837
|
+
): Promise<InternalCognitoUser | any> {
|
|
795
838
|
if (this.pendingSignIn) {
|
|
796
839
|
throw new Error('Pending sign-in attempt already in progress');
|
|
797
840
|
}
|
|
@@ -810,8 +853,10 @@ export class InternalAuthClass {
|
|
|
810
853
|
error => {
|
|
811
854
|
this.pendingSignIn = null;
|
|
812
855
|
reject(error);
|
|
813
|
-
}
|
|
814
|
-
|
|
856
|
+
},
|
|
857
|
+
customUserAgentDetails
|
|
858
|
+
),
|
|
859
|
+
getAmplifyUserAgent(customUserAgentDetails)
|
|
815
860
|
);
|
|
816
861
|
});
|
|
817
862
|
|
|
@@ -822,16 +867,22 @@ export class InternalAuthClass {
|
|
|
822
867
|
* Sign in without a password
|
|
823
868
|
* @private
|
|
824
869
|
* @param {AuthenticationDetails} authDetails - the user sign in data
|
|
825
|
-
* @
|
|
870
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
871
|
+
* @return - A promise resolves the InternalCognitoUser object if success or mfa required
|
|
826
872
|
*/
|
|
827
873
|
private signInWithoutPassword(
|
|
828
|
-
authDetails: AuthenticationDetails
|
|
829
|
-
|
|
874
|
+
authDetails: AuthenticationDetails,
|
|
875
|
+
customUserAgentDetails: CustomUserAgentDetails
|
|
876
|
+
): Promise<InternalCognitoUser | any> {
|
|
830
877
|
const user = this.createCognitoUser(authDetails.getUsername());
|
|
831
878
|
user.setAuthenticationFlowType('CUSTOM_AUTH');
|
|
832
879
|
|
|
833
880
|
return new Promise((resolve, reject) => {
|
|
834
|
-
user.initiateAuth(
|
|
881
|
+
user.initiateAuth(
|
|
882
|
+
authDetails,
|
|
883
|
+
this.authCallbacks(user, resolve, reject, customUserAgentDetails),
|
|
884
|
+
getAmplifyUserAgent(customUserAgentDetails)
|
|
885
|
+
);
|
|
835
886
|
});
|
|
836
887
|
}
|
|
837
888
|
|
|
@@ -848,8 +899,10 @@ export class InternalAuthClass {
|
|
|
848
899
|
user: CognitoUser | any,
|
|
849
900
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
850
901
|
): Promise<MFAOption[]> {
|
|
902
|
+
const internalUser: InternalCognitoUser | any = user;
|
|
903
|
+
|
|
851
904
|
return new Promise((res, rej) => {
|
|
852
|
-
|
|
905
|
+
internalUser.getMFAOptions((err, mfaOptions) => {
|
|
853
906
|
if (err) {
|
|
854
907
|
logger.debug('get MFA Options failed', err);
|
|
855
908
|
rej(err);
|
|
@@ -858,7 +911,7 @@ export class InternalAuthClass {
|
|
|
858
911
|
logger.debug('get MFA options success', mfaOptions);
|
|
859
912
|
res(mfaOptions);
|
|
860
913
|
return;
|
|
861
|
-
});
|
|
914
|
+
}, getAuthUserAgentValue(AuthAction.GetMFAOptions, customUserAgentDetails));
|
|
862
915
|
});
|
|
863
916
|
}
|
|
864
917
|
|
|
@@ -873,18 +926,23 @@ export class InternalAuthClass {
|
|
|
873
926
|
params?: GetPreferredMFAOpts,
|
|
874
927
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
875
928
|
): Promise<string> {
|
|
929
|
+
const internalUser: InternalCognitoUser | any = user;
|
|
876
930
|
const that = this;
|
|
877
931
|
return new Promise((res, rej) => {
|
|
878
932
|
const clientMetadata = this._config.clientMetadata; // TODO: verify behavior if this is override during signIn
|
|
879
933
|
|
|
880
934
|
const bypassCache = params ? params.bypassCache : false;
|
|
881
|
-
|
|
935
|
+
const userAgentValue = getAuthUserAgentValue(
|
|
936
|
+
AuthAction.GetPreferredMFA,
|
|
937
|
+
customUserAgentDetails
|
|
938
|
+
);
|
|
939
|
+
internalUser.getUserData(
|
|
882
940
|
async (err, data) => {
|
|
883
941
|
if (err) {
|
|
884
942
|
logger.debug('getting preferred mfa failed', err);
|
|
885
943
|
if (this.isSessionInvalid(err)) {
|
|
886
944
|
try {
|
|
887
|
-
await this.cleanUpInvalidSession(user);
|
|
945
|
+
await this.cleanUpInvalidSession(user, userAgentValue);
|
|
888
946
|
} catch (cleanUpError) {
|
|
889
947
|
rej(
|
|
890
948
|
new Error(
|
|
@@ -907,7 +965,8 @@ export class InternalAuthClass {
|
|
|
907
965
|
return;
|
|
908
966
|
}
|
|
909
967
|
},
|
|
910
|
-
{ bypassCache, clientMetadata }
|
|
968
|
+
{ bypassCache, clientMetadata },
|
|
969
|
+
userAgentValue
|
|
911
970
|
);
|
|
912
971
|
});
|
|
913
972
|
}
|
|
@@ -942,29 +1001,37 @@ export class InternalAuthClass {
|
|
|
942
1001
|
return ret;
|
|
943
1002
|
}
|
|
944
1003
|
|
|
945
|
-
private _getUserData(
|
|
1004
|
+
private _getUserData(
|
|
1005
|
+
user: InternalCognitoUser,
|
|
1006
|
+
params,
|
|
1007
|
+
userAgentValue: string
|
|
1008
|
+
) {
|
|
946
1009
|
return new Promise((res, rej) => {
|
|
947
|
-
user.getUserData(
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
1010
|
+
user.getUserData(
|
|
1011
|
+
async (err, data) => {
|
|
1012
|
+
if (err) {
|
|
1013
|
+
logger.debug('getting user data failed', err);
|
|
1014
|
+
if (this.isSessionInvalid(err)) {
|
|
1015
|
+
try {
|
|
1016
|
+
await this.cleanUpInvalidSession(user, userAgentValue);
|
|
1017
|
+
} catch (cleanUpError) {
|
|
1018
|
+
rej(
|
|
1019
|
+
new Error(
|
|
1020
|
+
`Session is invalid due to: ${err.message} and failed to clean up invalid session: ${cleanUpError.message}`
|
|
1021
|
+
)
|
|
1022
|
+
);
|
|
1023
|
+
return;
|
|
1024
|
+
}
|
|
960
1025
|
}
|
|
1026
|
+
rej(err);
|
|
1027
|
+
return;
|
|
1028
|
+
} else {
|
|
1029
|
+
res(data);
|
|
961
1030
|
}
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
}
|
|
967
|
-
}, params);
|
|
1031
|
+
},
|
|
1032
|
+
params,
|
|
1033
|
+
userAgentValue
|
|
1034
|
+
);
|
|
968
1035
|
});
|
|
969
1036
|
}
|
|
970
1037
|
|
|
@@ -980,12 +1047,21 @@ export class InternalAuthClass {
|
|
|
980
1047
|
mfaMethod: 'TOTP' | 'SMS' | 'NOMFA' | 'SMS_MFA' | 'SOFTWARE_TOKEN_MFA',
|
|
981
1048
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
982
1049
|
): Promise<string> {
|
|
1050
|
+
const internalUser: InternalCognitoUser | any = user;
|
|
1051
|
+
const userAgentValue = getAuthUserAgentValue(
|
|
1052
|
+
AuthAction.SetPreferredMFA,
|
|
1053
|
+
customUserAgentDetails
|
|
1054
|
+
);
|
|
983
1055
|
const clientMetadata = this._config.clientMetadata; // TODO: verify behavior if this is override during signIn
|
|
984
1056
|
|
|
985
|
-
const userData = await this._getUserData(
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
1057
|
+
const userData = await this._getUserData(
|
|
1058
|
+
user,
|
|
1059
|
+
{
|
|
1060
|
+
bypassCache: true,
|
|
1061
|
+
clientMetadata,
|
|
1062
|
+
},
|
|
1063
|
+
userAgentValue
|
|
1064
|
+
);
|
|
989
1065
|
let smsMfaSettings = null;
|
|
990
1066
|
let totpMfaSettings = null;
|
|
991
1067
|
|
|
@@ -1048,7 +1124,7 @@ export class InternalAuthClass {
|
|
|
1048
1124
|
|
|
1049
1125
|
const that = this;
|
|
1050
1126
|
return new Promise<string>((res, rej) => {
|
|
1051
|
-
|
|
1127
|
+
internalUser.setUserMfaPreference(
|
|
1052
1128
|
smsMfaSettings,
|
|
1053
1129
|
totpMfaSettings,
|
|
1054
1130
|
(err, result) => {
|
|
@@ -1059,13 +1135,13 @@ export class InternalAuthClass {
|
|
|
1059
1135
|
logger.debug('Set user mfa success', result);
|
|
1060
1136
|
logger.debug('Caching the latest user data into local');
|
|
1061
1137
|
// cache the latest result into user data
|
|
1062
|
-
|
|
1138
|
+
internalUser.getUserData(
|
|
1063
1139
|
async (err, data) => {
|
|
1064
1140
|
if (err) {
|
|
1065
1141
|
logger.debug('getting user data failed', err);
|
|
1066
1142
|
if (this.isSessionInvalid(err)) {
|
|
1067
1143
|
try {
|
|
1068
|
-
await this.cleanUpInvalidSession(user);
|
|
1144
|
+
await this.cleanUpInvalidSession(user, userAgentValue);
|
|
1069
1145
|
} catch (cleanUpError) {
|
|
1070
1146
|
rej(
|
|
1071
1147
|
new Error(
|
|
@@ -1083,9 +1159,11 @@ export class InternalAuthClass {
|
|
|
1083
1159
|
{
|
|
1084
1160
|
bypassCache: true,
|
|
1085
1161
|
clientMetadata,
|
|
1086
|
-
}
|
|
1162
|
+
},
|
|
1163
|
+
userAgentValue
|
|
1087
1164
|
);
|
|
1088
|
-
}
|
|
1165
|
+
},
|
|
1166
|
+
userAgentValue
|
|
1089
1167
|
);
|
|
1090
1168
|
});
|
|
1091
1169
|
}
|
|
@@ -1101,8 +1179,10 @@ export class InternalAuthClass {
|
|
|
1101
1179
|
user: CognitoUser,
|
|
1102
1180
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
1103
1181
|
): Promise<string> {
|
|
1182
|
+
const internalUser = user as InternalCognitoUser;
|
|
1183
|
+
|
|
1104
1184
|
return new Promise((res, rej) => {
|
|
1105
|
-
|
|
1185
|
+
internalUser.disableMFA((err, data) => {
|
|
1106
1186
|
if (err) {
|
|
1107
1187
|
logger.debug('disable mfa failed', err);
|
|
1108
1188
|
rej(err);
|
|
@@ -1111,7 +1191,7 @@ export class InternalAuthClass {
|
|
|
1111
1191
|
logger.debug('disable mfa succeed', data);
|
|
1112
1192
|
res(data);
|
|
1113
1193
|
return;
|
|
1114
|
-
});
|
|
1194
|
+
}, getAuthUserAgentValue(AuthAction.DisableSMS, customUserAgentDetails));
|
|
1115
1195
|
});
|
|
1116
1196
|
}
|
|
1117
1197
|
|
|
@@ -1126,8 +1206,10 @@ export class InternalAuthClass {
|
|
|
1126
1206
|
user: CognitoUser,
|
|
1127
1207
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
1128
1208
|
): Promise<string> {
|
|
1209
|
+
const internalUser = user as InternalCognitoUser;
|
|
1210
|
+
|
|
1129
1211
|
return new Promise((res, rej) => {
|
|
1130
|
-
|
|
1212
|
+
internalUser.enableMFA((err, data) => {
|
|
1131
1213
|
if (err) {
|
|
1132
1214
|
logger.debug('enable mfa failed', err);
|
|
1133
1215
|
rej(err);
|
|
@@ -1136,7 +1218,7 @@ export class InternalAuthClass {
|
|
|
1136
1218
|
logger.debug('enable mfa succeed', data);
|
|
1137
1219
|
res(data);
|
|
1138
1220
|
return;
|
|
1139
|
-
});
|
|
1221
|
+
}, getAuthUserAgentValue(AuthAction.EnableSMS, customUserAgentDetails));
|
|
1140
1222
|
});
|
|
1141
1223
|
}
|
|
1142
1224
|
|
|
@@ -1150,19 +1232,24 @@ export class InternalAuthClass {
|
|
|
1150
1232
|
user: CognitoUser | any,
|
|
1151
1233
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
1152
1234
|
): Promise<string> {
|
|
1235
|
+
const internalUser: InternalCognitoUser | any = user;
|
|
1236
|
+
|
|
1153
1237
|
return new Promise((res, rej) => {
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1238
|
+
internalUser.associateSoftwareToken(
|
|
1239
|
+
{
|
|
1240
|
+
onFailure: err => {
|
|
1241
|
+
logger.debug('associateSoftwareToken failed', err);
|
|
1242
|
+
rej(err);
|
|
1243
|
+
return;
|
|
1244
|
+
},
|
|
1245
|
+
associateSecretCode: secretCode => {
|
|
1246
|
+
logger.debug('associateSoftwareToken success', secretCode);
|
|
1247
|
+
res(secretCode);
|
|
1248
|
+
return;
|
|
1249
|
+
},
|
|
1164
1250
|
},
|
|
1165
|
-
|
|
1251
|
+
getAuthUserAgentValue(AuthAction.SetupTOTP, customUserAgentDetails)
|
|
1252
|
+
);
|
|
1166
1253
|
});
|
|
1167
1254
|
}
|
|
1168
1255
|
|
|
@@ -1179,38 +1266,50 @@ export class InternalAuthClass {
|
|
|
1179
1266
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
1180
1267
|
): Promise<CognitoUserSession> {
|
|
1181
1268
|
logger.debug('verification totp token', user, challengeAnswer);
|
|
1269
|
+
const internalUser: InternalCognitoUser | any = user;
|
|
1182
1270
|
|
|
1183
1271
|
let signInUserSession;
|
|
1184
|
-
if (
|
|
1185
|
-
|
|
1272
|
+
if (
|
|
1273
|
+
internalUser &&
|
|
1274
|
+
typeof internalUser.getSignInUserSession === 'function'
|
|
1275
|
+
) {
|
|
1276
|
+
signInUserSession = (user as InternalCognitoUser).getSignInUserSession();
|
|
1186
1277
|
}
|
|
1187
1278
|
const isLoggedIn = signInUserSession?.isValid();
|
|
1188
1279
|
|
|
1189
1280
|
return new Promise((res, rej) => {
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1281
|
+
internalUser.verifySoftwareToken(
|
|
1282
|
+
challengeAnswer,
|
|
1283
|
+
'My TOTP device',
|
|
1284
|
+
{
|
|
1285
|
+
onFailure: err => {
|
|
1286
|
+
logger.debug('verifyTotpToken failed', err);
|
|
1287
|
+
rej(err);
|
|
1288
|
+
return;
|
|
1289
|
+
},
|
|
1290
|
+
onSuccess: data => {
|
|
1291
|
+
if (!isLoggedIn) {
|
|
1292
|
+
dispatchAuthEvent(
|
|
1293
|
+
'signIn',
|
|
1294
|
+
internalUser,
|
|
1295
|
+
`A user ${internalUser.getUsername()} has been signed in`
|
|
1296
|
+
);
|
|
1297
|
+
}
|
|
1198
1298
|
dispatchAuthEvent(
|
|
1199
|
-
'
|
|
1200
|
-
|
|
1201
|
-
`A user ${
|
|
1299
|
+
'verify',
|
|
1300
|
+
internalUser,
|
|
1301
|
+
`A user ${internalUser.getUsername()} has been verified`
|
|
1202
1302
|
);
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
`A user ${user.getUsername()} has been verified`
|
|
1208
|
-
);
|
|
1209
|
-
logger.debug('verifyTotpToken success', data);
|
|
1210
|
-
res(data);
|
|
1211
|
-
return;
|
|
1303
|
+
logger.debug('verifyTotpToken success', data);
|
|
1304
|
+
res(data);
|
|
1305
|
+
return;
|
|
1306
|
+
},
|
|
1212
1307
|
},
|
|
1213
|
-
|
|
1308
|
+
getAuthUserAgentValue(
|
|
1309
|
+
AuthAction.VerifyTotpToken,
|
|
1310
|
+
customUserAgentDetails
|
|
1311
|
+
)
|
|
1312
|
+
);
|
|
1214
1313
|
});
|
|
1215
1314
|
}
|
|
1216
1315
|
|
|
@@ -1229,13 +1328,19 @@ export class InternalAuthClass {
|
|
|
1229
1328
|
clientMetadata: ClientMetaData = this._config.clientMetadata,
|
|
1230
1329
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
1231
1330
|
): Promise<CognitoUser | any> {
|
|
1331
|
+
const internalUser: InternalCognitoUser | any = user;
|
|
1332
|
+
|
|
1232
1333
|
if (!code) {
|
|
1233
1334
|
return this.rejectAuthError(AuthErrorTypes.EmptyCode);
|
|
1234
1335
|
}
|
|
1235
1336
|
|
|
1236
1337
|
const that = this;
|
|
1338
|
+
const userAgentDetails = getAuthUserAgentDetails(
|
|
1339
|
+
AuthAction.ConfirmSignIn,
|
|
1340
|
+
customUserAgentDetails
|
|
1341
|
+
);
|
|
1237
1342
|
return new Promise((resolve, reject) => {
|
|
1238
|
-
|
|
1343
|
+
internalUser.sendMFACode(
|
|
1239
1344
|
code,
|
|
1240
1345
|
{
|
|
1241
1346
|
onSuccess: async session => {
|
|
@@ -1247,19 +1352,24 @@ export class InternalAuthClass {
|
|
|
1247
1352
|
} catch (e) {
|
|
1248
1353
|
logger.debug('cannot get cognito credentials', e);
|
|
1249
1354
|
} finally {
|
|
1250
|
-
that.user =
|
|
1355
|
+
that.user = internalUser;
|
|
1251
1356
|
try {
|
|
1252
|
-
const currentUser = await this.
|
|
1253
|
-
|
|
1357
|
+
const currentUser = await this._currentUserPoolUser(
|
|
1358
|
+
undefined,
|
|
1359
|
+
userAgentDetails
|
|
1360
|
+
);
|
|
1361
|
+
Object.assign(internalUser, {
|
|
1362
|
+
attributes: currentUser.attributes,
|
|
1363
|
+
});
|
|
1254
1364
|
} catch (e) {
|
|
1255
1365
|
logger.debug('cannot get updated Cognito User', e);
|
|
1256
1366
|
}
|
|
1257
1367
|
dispatchAuthEvent(
|
|
1258
1368
|
'signIn',
|
|
1259
|
-
|
|
1260
|
-
`A user ${
|
|
1369
|
+
internalUser,
|
|
1370
|
+
`A user ${internalUser.getUsername()} has been signed in`
|
|
1261
1371
|
);
|
|
1262
|
-
resolve(
|
|
1372
|
+
resolve(internalUser);
|
|
1263
1373
|
}
|
|
1264
1374
|
},
|
|
1265
1375
|
onFailure: err => {
|
|
@@ -1268,7 +1378,8 @@ export class InternalAuthClass {
|
|
|
1268
1378
|
},
|
|
1269
1379
|
},
|
|
1270
1380
|
mfaType,
|
|
1271
|
-
clientMetadata
|
|
1381
|
+
clientMetadata,
|
|
1382
|
+
getAmplifyUserAgent(userAgentDetails)
|
|
1272
1383
|
);
|
|
1273
1384
|
});
|
|
1274
1385
|
}
|
|
@@ -1280,13 +1391,15 @@ export class InternalAuthClass {
|
|
|
1280
1391
|
clientMetadata: ClientMetaData = this._config.clientMetadata,
|
|
1281
1392
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
1282
1393
|
): Promise<CognitoUser | any> {
|
|
1394
|
+
const internalUser: InternalCognitoUser | any = user;
|
|
1395
|
+
|
|
1283
1396
|
if (!password) {
|
|
1284
1397
|
return this.rejectAuthError(AuthErrorTypes.EmptyPassword);
|
|
1285
1398
|
}
|
|
1286
1399
|
|
|
1287
1400
|
const that = this;
|
|
1288
1401
|
return new Promise((resolve, reject) => {
|
|
1289
|
-
|
|
1402
|
+
internalUser.completeNewPasswordChallenge(
|
|
1290
1403
|
password,
|
|
1291
1404
|
requiredAttributes,
|
|
1292
1405
|
{
|
|
@@ -1299,13 +1412,13 @@ export class InternalAuthClass {
|
|
|
1299
1412
|
} catch (e) {
|
|
1300
1413
|
logger.debug('cannot get cognito credentials', e);
|
|
1301
1414
|
} finally {
|
|
1302
|
-
that.user =
|
|
1415
|
+
that.user = internalUser;
|
|
1303
1416
|
dispatchAuthEvent(
|
|
1304
1417
|
'signIn',
|
|
1305
|
-
|
|
1306
|
-
`A user ${
|
|
1418
|
+
internalUser,
|
|
1419
|
+
`A user ${internalUser.getUsername()} has been signed in`
|
|
1307
1420
|
);
|
|
1308
|
-
resolve(
|
|
1421
|
+
resolve(internalUser);
|
|
1309
1422
|
}
|
|
1310
1423
|
},
|
|
1311
1424
|
onFailure: err => {
|
|
@@ -1319,24 +1432,28 @@ export class InternalAuthClass {
|
|
|
1319
1432
|
},
|
|
1320
1433
|
mfaRequired: (challengeName, challengeParam) => {
|
|
1321
1434
|
logger.debug('signIn MFA required');
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
resolve(
|
|
1435
|
+
internalUser['challengeName'] = challengeName;
|
|
1436
|
+
internalUser['challengeParam'] = challengeParam;
|
|
1437
|
+
resolve(internalUser);
|
|
1325
1438
|
},
|
|
1326
1439
|
mfaSetup: (challengeName, challengeParam) => {
|
|
1327
1440
|
logger.debug('signIn mfa setup', challengeName);
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
resolve(
|
|
1441
|
+
internalUser['challengeName'] = challengeName;
|
|
1442
|
+
internalUser['challengeParam'] = challengeParam;
|
|
1443
|
+
resolve(internalUser);
|
|
1331
1444
|
},
|
|
1332
1445
|
totpRequired: (challengeName, challengeParam) => {
|
|
1333
1446
|
logger.debug('signIn mfa setup', challengeName);
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
resolve(
|
|
1447
|
+
internalUser['challengeName'] = challengeName;
|
|
1448
|
+
internalUser['challengeParam'] = challengeParam;
|
|
1449
|
+
resolve(internalUser);
|
|
1337
1450
|
},
|
|
1338
1451
|
},
|
|
1339
|
-
clientMetadata
|
|
1452
|
+
clientMetadata,
|
|
1453
|
+
getAuthUserAgentValue(
|
|
1454
|
+
AuthAction.CompleteNewPassword,
|
|
1455
|
+
customUserAgentDetails
|
|
1456
|
+
)
|
|
1340
1457
|
);
|
|
1341
1458
|
});
|
|
1342
1459
|
}
|
|
@@ -1347,6 +1464,7 @@ export class InternalAuthClass {
|
|
|
1347
1464
|
* @param {String} challengeResponses - The confirmation code
|
|
1348
1465
|
* @param {ClientMetaData} clientMetadata - optional client metadata defaults to config
|
|
1349
1466
|
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
1467
|
+
*
|
|
1350
1468
|
*/
|
|
1351
1469
|
public sendCustomChallengeAnswer(
|
|
1352
1470
|
user: CognitoUser | any,
|
|
@@ -1354,6 +1472,8 @@ export class InternalAuthClass {
|
|
|
1354
1472
|
clientMetadata: ClientMetaData = this._config.clientMetadata,
|
|
1355
1473
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
1356
1474
|
): Promise<CognitoUser | any> {
|
|
1475
|
+
const internalUser: InternalCognitoUser | any = user;
|
|
1476
|
+
|
|
1357
1477
|
if (!this.userPool) {
|
|
1358
1478
|
return this.rejectNoUserPool();
|
|
1359
1479
|
}
|
|
@@ -1362,11 +1482,16 @@ export class InternalAuthClass {
|
|
|
1362
1482
|
}
|
|
1363
1483
|
|
|
1364
1484
|
const that = this;
|
|
1485
|
+
const userAgentDetails = getAuthUserAgentDetails(
|
|
1486
|
+
AuthAction.SendCustomChallengeAnswer,
|
|
1487
|
+
customUserAgentDetails
|
|
1488
|
+
);
|
|
1365
1489
|
return new Promise((resolve, reject) => {
|
|
1366
|
-
|
|
1490
|
+
internalUser.sendCustomChallengeAnswer(
|
|
1367
1491
|
challengeResponses,
|
|
1368
|
-
this.authCallbacks(
|
|
1369
|
-
clientMetadata
|
|
1492
|
+
this.authCallbacks(internalUser, resolve, reject, userAgentDetails),
|
|
1493
|
+
clientMetadata,
|
|
1494
|
+
getAmplifyUserAgent(userAgentDetails)
|
|
1370
1495
|
);
|
|
1371
1496
|
});
|
|
1372
1497
|
}
|
|
@@ -1383,16 +1508,25 @@ export class InternalAuthClass {
|
|
|
1383
1508
|
attributeNames: string[],
|
|
1384
1509
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
1385
1510
|
) {
|
|
1511
|
+
const internalUser: InternalCognitoUser | any = user;
|
|
1386
1512
|
const that = this;
|
|
1513
|
+
const userAgentValue = getAuthUserAgentValue(
|
|
1514
|
+
AuthAction.DeleteUserAttributes,
|
|
1515
|
+
customUserAgentDetails
|
|
1516
|
+
);
|
|
1387
1517
|
return new Promise((resolve, reject) => {
|
|
1388
|
-
that.
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1518
|
+
that._userSession(userAgentValue, internalUser).then(session => {
|
|
1519
|
+
internalUser.deleteAttributes(
|
|
1520
|
+
attributeNames,
|
|
1521
|
+
(err, result) => {
|
|
1522
|
+
if (err) {
|
|
1523
|
+
return reject(err);
|
|
1524
|
+
} else {
|
|
1525
|
+
return resolve(result);
|
|
1526
|
+
}
|
|
1527
|
+
},
|
|
1528
|
+
userAgentValue
|
|
1529
|
+
);
|
|
1396
1530
|
});
|
|
1397
1531
|
});
|
|
1398
1532
|
}
|
|
@@ -1419,18 +1553,26 @@ export class InternalAuthClass {
|
|
|
1419
1553
|
|
|
1420
1554
|
return new Promise(async (res, rej) => {
|
|
1421
1555
|
if (this.userPool) {
|
|
1422
|
-
const
|
|
1556
|
+
const internalUser =
|
|
1557
|
+
this.userPool.getCurrentUser() as InternalCognitoUser;
|
|
1423
1558
|
|
|
1424
|
-
if (!
|
|
1559
|
+
if (!internalUser) {
|
|
1425
1560
|
logger.debug('Failed to get user from user pool');
|
|
1426
1561
|
return rej(new Error('No current user.'));
|
|
1427
1562
|
} else {
|
|
1428
|
-
|
|
1563
|
+
const userAgentValue = getAuthUserAgentValue(
|
|
1564
|
+
AuthAction.DeleteUser,
|
|
1565
|
+
customUserAgentDetails
|
|
1566
|
+
);
|
|
1567
|
+
internalUser.getSession(async (err, session) => {
|
|
1429
1568
|
if (err) {
|
|
1430
1569
|
logger.debug('Failed to get the user session', err);
|
|
1431
1570
|
if (this.isSessionInvalid(err)) {
|
|
1432
1571
|
try {
|
|
1433
|
-
await this.cleanUpInvalidSession(
|
|
1572
|
+
await this.cleanUpInvalidSession(
|
|
1573
|
+
internalUser,
|
|
1574
|
+
userAgentValue
|
|
1575
|
+
);
|
|
1434
1576
|
} catch (cleanUpError) {
|
|
1435
1577
|
rej(
|
|
1436
1578
|
new Error(
|
|
@@ -1442,36 +1584,40 @@ export class InternalAuthClass {
|
|
|
1442
1584
|
}
|
|
1443
1585
|
return rej(err);
|
|
1444
1586
|
} else {
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
dispatchAuthEvent(
|
|
1450
|
-
'userDeleted',
|
|
1451
|
-
result,
|
|
1452
|
-
'The authenticated user has been deleted.'
|
|
1453
|
-
);
|
|
1454
|
-
user.signOut();
|
|
1455
|
-
this.user = null;
|
|
1456
|
-
try {
|
|
1457
|
-
this.cleanCachedItems(); // clean aws credentials
|
|
1458
|
-
} catch (e) {
|
|
1459
|
-
// TODO: change to rejects in refactor
|
|
1460
|
-
logger.debug('failed to clear cached items');
|
|
1461
|
-
}
|
|
1462
|
-
|
|
1463
|
-
if (isSignedInHostedUI) {
|
|
1464
|
-
this.oAuthSignOutRedirect(res, rej);
|
|
1587
|
+
internalUser.deleteUser(
|
|
1588
|
+
(err, result: string) => {
|
|
1589
|
+
if (err) {
|
|
1590
|
+
rej(err);
|
|
1465
1591
|
} else {
|
|
1466
1592
|
dispatchAuthEvent(
|
|
1467
|
-
'
|
|
1468
|
-
|
|
1469
|
-
|
|
1593
|
+
'userDeleted',
|
|
1594
|
+
result,
|
|
1595
|
+
'The authenticated user has been deleted.'
|
|
1470
1596
|
);
|
|
1471
|
-
|
|
1597
|
+
internalUser.signOut(undefined, userAgentValue);
|
|
1598
|
+
this.user = null;
|
|
1599
|
+
try {
|
|
1600
|
+
this.cleanCachedItems(); // clean aws credentials
|
|
1601
|
+
} catch (e) {
|
|
1602
|
+
// TODO: change to rejects in refactor
|
|
1603
|
+
logger.debug('failed to clear cached items');
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
if (isSignedInHostedUI) {
|
|
1607
|
+
this.oAuthSignOutRedirect(res, rej);
|
|
1608
|
+
} else {
|
|
1609
|
+
dispatchAuthEvent(
|
|
1610
|
+
'signOut',
|
|
1611
|
+
this.user,
|
|
1612
|
+
`A user has been signed out`
|
|
1613
|
+
);
|
|
1614
|
+
res(result);
|
|
1615
|
+
}
|
|
1472
1616
|
}
|
|
1473
|
-
}
|
|
1474
|
-
|
|
1617
|
+
},
|
|
1618
|
+
undefined,
|
|
1619
|
+
userAgentValue
|
|
1620
|
+
);
|
|
1475
1621
|
}
|
|
1476
1622
|
});
|
|
1477
1623
|
}
|
|
@@ -1496,10 +1642,15 @@ export class InternalAuthClass {
|
|
|
1496
1642
|
clientMetadata: ClientMetaData = this._config.clientMetadata,
|
|
1497
1643
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
1498
1644
|
): Promise<string> {
|
|
1645
|
+
const internalUser: InternalCognitoUser | any = user;
|
|
1499
1646
|
const attributeList: ICognitoUserAttributeData[] = [];
|
|
1500
1647
|
const that = this;
|
|
1648
|
+
const userAgentValue = getAuthUserAgentValue(
|
|
1649
|
+
AuthAction.UpdateUserAttributes,
|
|
1650
|
+
customUserAgentDetails
|
|
1651
|
+
);
|
|
1501
1652
|
return new Promise((resolve, reject) => {
|
|
1502
|
-
that.
|
|
1653
|
+
that._userSession(userAgentValue, internalUser).then(session => {
|
|
1503
1654
|
for (const key in attributes) {
|
|
1504
1655
|
if (key !== 'sub' && key.indexOf('_verified') < 0) {
|
|
1505
1656
|
const attr: ICognitoUserAttributeData = {
|
|
@@ -1509,7 +1660,7 @@ export class InternalAuthClass {
|
|
|
1509
1660
|
attributeList.push(attr);
|
|
1510
1661
|
}
|
|
1511
1662
|
}
|
|
1512
|
-
|
|
1663
|
+
internalUser.updateAttributes(
|
|
1513
1664
|
attributeList,
|
|
1514
1665
|
(err, result, details) => {
|
|
1515
1666
|
if (err) {
|
|
@@ -1532,7 +1683,8 @@ export class InternalAuthClass {
|
|
|
1532
1683
|
return resolve(result);
|
|
1533
1684
|
}
|
|
1534
1685
|
},
|
|
1535
|
-
clientMetadata
|
|
1686
|
+
clientMetadata,
|
|
1687
|
+
userAgentValue
|
|
1536
1688
|
);
|
|
1537
1689
|
});
|
|
1538
1690
|
});
|
|
@@ -1568,15 +1720,27 @@ export class InternalAuthClass {
|
|
|
1568
1720
|
user: CognitoUser | any,
|
|
1569
1721
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
1570
1722
|
): Promise<CognitoUserAttribute[]> {
|
|
1723
|
+
return this._userAttributes(user, customUserAgentDetails);
|
|
1724
|
+
}
|
|
1725
|
+
|
|
1726
|
+
private _userAttributes(
|
|
1727
|
+
user: CognitoUser | any,
|
|
1728
|
+
customUserAgentDetails?: CustomUserAgentDetails
|
|
1729
|
+
): Promise<CognitoUserAttribute[]> {
|
|
1730
|
+
const internalUser: InternalCognitoUser | any = user;
|
|
1731
|
+
const userAgentValue = getAuthUserAgentValue(
|
|
1732
|
+
AuthAction.UserAttributes,
|
|
1733
|
+
customUserAgentDetails
|
|
1734
|
+
);
|
|
1571
1735
|
return new Promise((resolve, reject) => {
|
|
1572
|
-
this.
|
|
1573
|
-
|
|
1736
|
+
this._userSession(userAgentValue, internalUser).then(session => {
|
|
1737
|
+
internalUser.getUserAttributes((err, attributes) => {
|
|
1574
1738
|
if (err) {
|
|
1575
1739
|
reject(err);
|
|
1576
1740
|
} else {
|
|
1577
1741
|
resolve(attributes);
|
|
1578
1742
|
}
|
|
1579
|
-
});
|
|
1743
|
+
}, userAgentValue);
|
|
1580
1744
|
});
|
|
1581
1745
|
});
|
|
1582
1746
|
}
|
|
@@ -1586,7 +1750,13 @@ export class InternalAuthClass {
|
|
|
1586
1750
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
1587
1751
|
) {
|
|
1588
1752
|
const that = this;
|
|
1589
|
-
return this.
|
|
1753
|
+
return this._userAttributes(
|
|
1754
|
+
user,
|
|
1755
|
+
getAuthUserAgentDetails(
|
|
1756
|
+
AuthAction.VerifiedContact,
|
|
1757
|
+
customUserAgentDetails
|
|
1758
|
+
)
|
|
1759
|
+
).then(attributes => {
|
|
1590
1760
|
const attrs = that.attributesToObject(attributes);
|
|
1591
1761
|
const unverified = {};
|
|
1592
1762
|
const verified = {};
|
|
@@ -1687,8 +1857,11 @@ export class InternalAuthClass {
|
|
|
1687
1857
|
);
|
|
1688
1858
|
}
|
|
1689
1859
|
|
|
1690
|
-
private async cleanUpInvalidSession(
|
|
1691
|
-
|
|
1860
|
+
private async cleanUpInvalidSession(
|
|
1861
|
+
internalUser: InternalCognitoUser,
|
|
1862
|
+
userAgentValue: string
|
|
1863
|
+
) {
|
|
1864
|
+
internalUser.signOut(undefined, userAgentValue);
|
|
1692
1865
|
this.user = null;
|
|
1693
1866
|
try {
|
|
1694
1867
|
await this.cleanCachedItems(); // clean aws credentials
|
|
@@ -1713,6 +1886,13 @@ export class InternalAuthClass {
|
|
|
1713
1886
|
public currentUserPoolUser(
|
|
1714
1887
|
params?: CurrentUserOpts,
|
|
1715
1888
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
1889
|
+
): Promise<CognitoUser | any> {
|
|
1890
|
+
return this._currentUserPoolUser(params, customUserAgentDetails);
|
|
1891
|
+
}
|
|
1892
|
+
|
|
1893
|
+
private _currentUserPoolUser(
|
|
1894
|
+
params?: CurrentUserOpts,
|
|
1895
|
+
customUserAgentDetails?: CustomUserAgentDetails
|
|
1716
1896
|
): Promise<CognitoUser | any> {
|
|
1717
1897
|
if (!this.userPool) {
|
|
1718
1898
|
return this.rejectNoUserPool();
|
|
@@ -1753,9 +1933,10 @@ export class InternalAuthClass {
|
|
|
1753
1933
|
});
|
|
1754
1934
|
}
|
|
1755
1935
|
|
|
1756
|
-
const
|
|
1936
|
+
const internalUser =
|
|
1937
|
+
this.userPool.getCurrentUser() as InternalCognitoUser;
|
|
1757
1938
|
|
|
1758
|
-
if (!
|
|
1939
|
+
if (!internalUser) {
|
|
1759
1940
|
logger.debug('Failed to get user from user pool');
|
|
1760
1941
|
rej('No current user');
|
|
1761
1942
|
return;
|
|
@@ -1763,7 +1944,14 @@ export class InternalAuthClass {
|
|
|
1763
1944
|
|
|
1764
1945
|
// refresh the session if the session expired.
|
|
1765
1946
|
try {
|
|
1766
|
-
const
|
|
1947
|
+
const userAgentValue = getAuthUserAgentValue(
|
|
1948
|
+
AuthAction.CurrentUserPoolUser,
|
|
1949
|
+
customUserAgentDetails
|
|
1950
|
+
);
|
|
1951
|
+
const session = await this._userSession(
|
|
1952
|
+
userAgentValue,
|
|
1953
|
+
internalUser
|
|
1954
|
+
);
|
|
1767
1955
|
|
|
1768
1956
|
// get user data from Cognito
|
|
1769
1957
|
const bypassCache = params ? params.bypassCache : false;
|
|
@@ -1777,13 +1965,16 @@ export class InternalAuthClass {
|
|
|
1777
1965
|
// validate the token's scope first before calling this function
|
|
1778
1966
|
const { scope = '' } = session.getAccessToken().decodePayload();
|
|
1779
1967
|
if (scope.split(' ').includes(USER_ADMIN_SCOPE)) {
|
|
1780
|
-
|
|
1968
|
+
internalUser.getUserData(
|
|
1781
1969
|
async (err, data) => {
|
|
1782
1970
|
if (err) {
|
|
1783
1971
|
logger.debug('getting user data failed', err);
|
|
1784
1972
|
if (this.isSessionInvalid(err)) {
|
|
1785
1973
|
try {
|
|
1786
|
-
await this.cleanUpInvalidSession(
|
|
1974
|
+
await this.cleanUpInvalidSession(
|
|
1975
|
+
internalUser,
|
|
1976
|
+
userAgentValue
|
|
1977
|
+
);
|
|
1787
1978
|
} catch (cleanUpError) {
|
|
1788
1979
|
rej(
|
|
1789
1980
|
new Error(
|
|
@@ -1794,12 +1985,12 @@ export class InternalAuthClass {
|
|
|
1794
1985
|
}
|
|
1795
1986
|
rej(err);
|
|
1796
1987
|
} else {
|
|
1797
|
-
res(
|
|
1988
|
+
res(internalUser);
|
|
1798
1989
|
}
|
|
1799
1990
|
return;
|
|
1800
1991
|
}
|
|
1801
1992
|
const preferredMFA = data.PreferredMfaSetting || 'NOMFA';
|
|
1802
|
-
const attributeList = [];
|
|
1993
|
+
const attributeList: CognitoUserAttribute[] = [];
|
|
1803
1994
|
|
|
1804
1995
|
for (let i = 0; i < data.UserAttributes.length; i++) {
|
|
1805
1996
|
const attribute = {
|
|
@@ -1811,17 +2002,18 @@ export class InternalAuthClass {
|
|
|
1811
2002
|
}
|
|
1812
2003
|
|
|
1813
2004
|
const attributes = this.attributesToObject(attributeList);
|
|
1814
|
-
Object.assign(
|
|
1815
|
-
return res(
|
|
2005
|
+
Object.assign(internalUser, { attributes, preferredMFA });
|
|
2006
|
+
return res(internalUser);
|
|
1816
2007
|
},
|
|
1817
|
-
{ bypassCache, clientMetadata }
|
|
2008
|
+
{ bypassCache, clientMetadata },
|
|
2009
|
+
userAgentValue
|
|
1818
2010
|
);
|
|
1819
2011
|
} else {
|
|
1820
2012
|
logger.debug(
|
|
1821
2013
|
`Unable to get the user data because the ${USER_ADMIN_SCOPE} ` +
|
|
1822
2014
|
`is not in the scopes of the access token`
|
|
1823
2015
|
);
|
|
1824
|
-
return res(
|
|
2016
|
+
return res(internalUser);
|
|
1825
2017
|
}
|
|
1826
2018
|
} catch (err) {
|
|
1827
2019
|
rej(err);
|
|
@@ -1844,7 +2036,14 @@ export class InternalAuthClass {
|
|
|
1844
2036
|
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
1845
2037
|
* @return - A promise resolves to current authenticated CognitoUser if success
|
|
1846
2038
|
*/
|
|
1847
|
-
public
|
|
2039
|
+
public currentAuthenticatedUser(
|
|
2040
|
+
params?: CurrentUserOpts,
|
|
2041
|
+
customUserAgentDetails?: CustomUserAgentDetails
|
|
2042
|
+
): Promise<CognitoUser | any> {
|
|
2043
|
+
return this._currentAuthenticatedUser(params, customUserAgentDetails);
|
|
2044
|
+
}
|
|
2045
|
+
|
|
2046
|
+
private async _currentAuthenticatedUser(
|
|
1848
2047
|
params?: CurrentUserOpts,
|
|
1849
2048
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
1850
2049
|
): Promise<CognitoUser | any> {
|
|
@@ -1879,7 +2078,13 @@ export class InternalAuthClass {
|
|
|
1879
2078
|
logger.debug('get current authenticated userpool user');
|
|
1880
2079
|
let user = null;
|
|
1881
2080
|
try {
|
|
1882
|
-
user = await this.
|
|
2081
|
+
user = await this._currentUserPoolUser(
|
|
2082
|
+
params,
|
|
2083
|
+
getAuthUserAgentDetails(
|
|
2084
|
+
AuthAction.CurrentAuthenticatedUser,
|
|
2085
|
+
customUserAgentDetails
|
|
2086
|
+
)
|
|
2087
|
+
);
|
|
1883
2088
|
} catch (e) {
|
|
1884
2089
|
if (e === 'No userPool') {
|
|
1885
2090
|
logger.error(
|
|
@@ -1902,8 +2107,18 @@ export class InternalAuthClass {
|
|
|
1902
2107
|
*/
|
|
1903
2108
|
public currentSession(
|
|
1904
2109
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
2110
|
+
): Promise<CognitoUserSession> {
|
|
2111
|
+
return this._currentSession(customUserAgentDetails);
|
|
2112
|
+
}
|
|
2113
|
+
|
|
2114
|
+
private _currentSession(
|
|
2115
|
+
customUserAgentDetails?: CustomUserAgentDetails
|
|
1905
2116
|
): Promise<CognitoUserSession> {
|
|
1906
2117
|
const that = this;
|
|
2118
|
+
const userAgentDetails = getAuthUserAgentDetails(
|
|
2119
|
+
AuthAction.CurrentSession,
|
|
2120
|
+
customUserAgentDetails
|
|
2121
|
+
);
|
|
1907
2122
|
logger.debug('Getting current session');
|
|
1908
2123
|
// Purposely not calling the reject method here because we don't need a console error
|
|
1909
2124
|
if (!this.userPool) {
|
|
@@ -1912,10 +2127,10 @@ export class InternalAuthClass {
|
|
|
1912
2127
|
|
|
1913
2128
|
return new Promise((res, rej) => {
|
|
1914
2129
|
that
|
|
1915
|
-
.
|
|
2130
|
+
._currentUserPoolUser(undefined, userAgentDetails)
|
|
1916
2131
|
.then(user => {
|
|
1917
2132
|
that
|
|
1918
|
-
.
|
|
2133
|
+
._userSession(getAmplifyUserAgent(userAgentDetails), user)
|
|
1919
2134
|
.then(session => {
|
|
1920
2135
|
res(session);
|
|
1921
2136
|
return;
|
|
@@ -1934,8 +2149,11 @@ export class InternalAuthClass {
|
|
|
1934
2149
|
});
|
|
1935
2150
|
}
|
|
1936
2151
|
|
|
1937
|
-
private async _userSession(
|
|
1938
|
-
|
|
2152
|
+
private async _userSession(
|
|
2153
|
+
userAgentValue: string,
|
|
2154
|
+
internalUser?: InternalCognitoUser
|
|
2155
|
+
): Promise<CognitoUserSession> {
|
|
2156
|
+
if (!internalUser) {
|
|
1939
2157
|
logger.debug('the user is null');
|
|
1940
2158
|
return this.rejectAuthError(AuthErrorTypes.NoUserSession);
|
|
1941
2159
|
}
|
|
@@ -1945,13 +2163,19 @@ export class InternalAuthClass {
|
|
|
1945
2163
|
if (this.inflightSessionPromiseCounter === 0) {
|
|
1946
2164
|
this.inflightSessionPromise = new Promise<CognitoUserSession>(
|
|
1947
2165
|
(res, rej) => {
|
|
1948
|
-
|
|
2166
|
+
internalUser.getSession(
|
|
1949
2167
|
async (err, session) => {
|
|
1950
2168
|
if (err) {
|
|
1951
|
-
logger.debug(
|
|
2169
|
+
logger.debug(
|
|
2170
|
+
'Failed to get the session from user',
|
|
2171
|
+
internalUser
|
|
2172
|
+
);
|
|
1952
2173
|
if (this.isSessionInvalid(err)) {
|
|
1953
2174
|
try {
|
|
1954
|
-
await this.cleanUpInvalidSession(
|
|
2175
|
+
await this.cleanUpInvalidSession(
|
|
2176
|
+
internalUser,
|
|
2177
|
+
userAgentValue
|
|
2178
|
+
);
|
|
1955
2179
|
} catch (cleanUpError) {
|
|
1956
2180
|
rej(
|
|
1957
2181
|
new Error(
|
|
@@ -1969,7 +2193,8 @@ export class InternalAuthClass {
|
|
|
1969
2193
|
return;
|
|
1970
2194
|
}
|
|
1971
2195
|
},
|
|
1972
|
-
{ clientMetadata }
|
|
2196
|
+
{ clientMetadata },
|
|
2197
|
+
userAgentValue
|
|
1973
2198
|
);
|
|
1974
2199
|
}
|
|
1975
2200
|
);
|
|
@@ -1980,7 +2205,7 @@ export class InternalAuthClass {
|
|
|
1980
2205
|
const userSession = await this.inflightSessionPromise;
|
|
1981
2206
|
// Set private member. Avoid user.setSignInUserSession() to prevent excessive localstorage refresh.
|
|
1982
2207
|
// @ts-ignore
|
|
1983
|
-
|
|
2208
|
+
internalUser.signInUserSession = userSession;
|
|
1984
2209
|
return userSession!;
|
|
1985
2210
|
} finally {
|
|
1986
2211
|
this.inflightSessionPromiseCounter--;
|
|
@@ -1997,7 +2222,10 @@ export class InternalAuthClass {
|
|
|
1997
2222
|
user,
|
|
1998
2223
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
1999
2224
|
): Promise<CognitoUserSession> {
|
|
2000
|
-
return this._userSession(
|
|
2225
|
+
return this._userSession(
|
|
2226
|
+
getAuthUserAgentValue(AuthAction.UserSession, customUserAgentDetails),
|
|
2227
|
+
user
|
|
2228
|
+
);
|
|
2001
2229
|
}
|
|
2002
2230
|
|
|
2003
2231
|
/**
|
|
@@ -2031,7 +2259,12 @@ export class InternalAuthClass {
|
|
|
2031
2259
|
// refresh the jwt token here if necessary
|
|
2032
2260
|
return this.Credentials.refreshFederatedToken(federatedInfo);
|
|
2033
2261
|
} else {
|
|
2034
|
-
return this.
|
|
2262
|
+
return this._currentSession(
|
|
2263
|
+
getAuthUserAgentDetails(
|
|
2264
|
+
AuthAction.CurrentUserCredentials,
|
|
2265
|
+
customUserAgentDetails
|
|
2266
|
+
)
|
|
2267
|
+
)
|
|
2035
2268
|
.then(session => {
|
|
2036
2269
|
logger.debug('getting session success', session);
|
|
2037
2270
|
return this.Credentials.set(session, 'session');
|
|
@@ -2054,6 +2287,8 @@ export class InternalAuthClass {
|
|
|
2054
2287
|
* Initiate an attribute confirmation request
|
|
2055
2288
|
* @param {Object} user - The CognitoUser
|
|
2056
2289
|
* @param {Object} attr - The attributes to be verified
|
|
2290
|
+
* @param {ClientMetaData} clientMetadata - optional client metadata, defaults to config
|
|
2291
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
2057
2292
|
* @return - A promise resolves to callback data if success
|
|
2058
2293
|
*/
|
|
2059
2294
|
public verifyUserAttribute(
|
|
@@ -2062,8 +2297,24 @@ export class InternalAuthClass {
|
|
|
2062
2297
|
clientMetadata: ClientMetaData = this._config.clientMetadata,
|
|
2063
2298
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
2064
2299
|
): Promise<void> {
|
|
2300
|
+
return this._verifyUserAttribute(
|
|
2301
|
+
user,
|
|
2302
|
+
attr,
|
|
2303
|
+
clientMetadata,
|
|
2304
|
+
customUserAgentDetails
|
|
2305
|
+
);
|
|
2306
|
+
}
|
|
2307
|
+
|
|
2308
|
+
private _verifyUserAttribute(
|
|
2309
|
+
user: CognitoUser | any,
|
|
2310
|
+
attr: string,
|
|
2311
|
+
clientMetadata: ClientMetaData = this._config.clientMetadata,
|
|
2312
|
+
customUserAgentDetails?: CustomUserAgentDetails
|
|
2313
|
+
): Promise<void> {
|
|
2314
|
+
const internalUser: InternalCognitoUser | any = user;
|
|
2315
|
+
|
|
2065
2316
|
return new Promise((resolve, reject) => {
|
|
2066
|
-
|
|
2317
|
+
internalUser.getAttributeVerificationCode(
|
|
2067
2318
|
attr,
|
|
2068
2319
|
{
|
|
2069
2320
|
onSuccess(success) {
|
|
@@ -2073,7 +2324,11 @@ export class InternalAuthClass {
|
|
|
2073
2324
|
return reject(err);
|
|
2074
2325
|
},
|
|
2075
2326
|
},
|
|
2076
|
-
clientMetadata
|
|
2327
|
+
clientMetadata,
|
|
2328
|
+
getAuthUserAgentValue(
|
|
2329
|
+
AuthAction.VerifyUserAttribute,
|
|
2330
|
+
customUserAgentDetails
|
|
2331
|
+
)
|
|
2077
2332
|
);
|
|
2078
2333
|
});
|
|
2079
2334
|
}
|
|
@@ -2091,22 +2346,45 @@ export class InternalAuthClass {
|
|
|
2091
2346
|
attr: string,
|
|
2092
2347
|
code: string,
|
|
2093
2348
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
2349
|
+
): Promise<string> {
|
|
2350
|
+
return this._verifyUserAttributeSubmit(
|
|
2351
|
+
user,
|
|
2352
|
+
attr,
|
|
2353
|
+
code,
|
|
2354
|
+
customUserAgentDetails
|
|
2355
|
+
);
|
|
2356
|
+
}
|
|
2357
|
+
|
|
2358
|
+
private _verifyUserAttributeSubmit(
|
|
2359
|
+
user: CognitoUser | any,
|
|
2360
|
+
attr: string,
|
|
2361
|
+
code: string,
|
|
2362
|
+
customUserAgentDetails?: CustomUserAgentDetails
|
|
2094
2363
|
): Promise<string> {
|
|
2095
2364
|
if (!code) {
|
|
2096
2365
|
return this.rejectAuthError(AuthErrorTypes.EmptyCode);
|
|
2097
2366
|
}
|
|
2367
|
+
const internalUser: InternalCognitoUser | any = user;
|
|
2098
2368
|
|
|
2099
2369
|
return new Promise((resolve, reject) => {
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2370
|
+
internalUser.verifyAttribute(
|
|
2371
|
+
attr,
|
|
2372
|
+
code,
|
|
2373
|
+
{
|
|
2374
|
+
onSuccess(data) {
|
|
2375
|
+
resolve(data);
|
|
2376
|
+
return;
|
|
2377
|
+
},
|
|
2378
|
+
onFailure(err) {
|
|
2379
|
+
reject(err);
|
|
2380
|
+
return;
|
|
2381
|
+
},
|
|
2108
2382
|
},
|
|
2109
|
-
|
|
2383
|
+
getAuthUserAgentValue(
|
|
2384
|
+
AuthAction.VerifyUserAttributeSubmit,
|
|
2385
|
+
customUserAgentDetails
|
|
2386
|
+
)
|
|
2387
|
+
);
|
|
2110
2388
|
});
|
|
2111
2389
|
}
|
|
2112
2390
|
|
|
@@ -2114,10 +2392,16 @@ export class InternalAuthClass {
|
|
|
2114
2392
|
attr: string,
|
|
2115
2393
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
2116
2394
|
): Promise<void> {
|
|
2395
|
+
const userAgentDetails = getAuthUserAgentDetails(
|
|
2396
|
+
AuthAction.VerifyCurrentUserAttribute,
|
|
2397
|
+
customUserAgentDetails
|
|
2398
|
+
);
|
|
2117
2399
|
const that = this;
|
|
2118
2400
|
return that
|
|
2119
|
-
.
|
|
2120
|
-
.then(user =>
|
|
2401
|
+
._currentUserPoolUser(undefined, userAgentDetails)
|
|
2402
|
+
.then(user =>
|
|
2403
|
+
that._verifyUserAttribute(user, attr, undefined, userAgentDetails)
|
|
2404
|
+
);
|
|
2121
2405
|
}
|
|
2122
2406
|
|
|
2123
2407
|
/**
|
|
@@ -2132,15 +2416,22 @@ export class InternalAuthClass {
|
|
|
2132
2416
|
code: string,
|
|
2133
2417
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
2134
2418
|
): Promise<string> {
|
|
2419
|
+
const userAgentDetails = getAuthUserAgentDetails(
|
|
2420
|
+
AuthAction.VerifyCurrentUserAttributeSubmit,
|
|
2421
|
+
customUserAgentDetails
|
|
2422
|
+
);
|
|
2135
2423
|
const that = this;
|
|
2136
2424
|
return that
|
|
2137
|
-
.
|
|
2138
|
-
.then(user =>
|
|
2425
|
+
._currentUserPoolUser(undefined, userAgentDetails)
|
|
2426
|
+
.then(user =>
|
|
2427
|
+
that._verifyUserAttributeSubmit(user, attr, code, userAgentDetails)
|
|
2428
|
+
);
|
|
2139
2429
|
}
|
|
2140
2430
|
|
|
2141
2431
|
private async cognitoIdentitySignOut(
|
|
2142
2432
|
opts: SignOutOpts,
|
|
2143
|
-
|
|
2433
|
+
internalUser: InternalCognitoUser | any,
|
|
2434
|
+
userAgentValue: string
|
|
2144
2435
|
) {
|
|
2145
2436
|
try {
|
|
2146
2437
|
await this._storageSync;
|
|
@@ -2155,18 +2446,21 @@ export class InternalAuthClass {
|
|
|
2155
2446
|
|
|
2156
2447
|
return new Promise((res, rej) => {
|
|
2157
2448
|
if (opts && opts.global) {
|
|
2158
|
-
logger.debug('user global sign out',
|
|
2449
|
+
logger.debug('user global sign out', internalUser);
|
|
2159
2450
|
// in order to use global signout
|
|
2160
2451
|
// we must validate the user as an authenticated user by using getSession
|
|
2161
2452
|
const clientMetadata = this._config.clientMetadata; // TODO: verify behavior if this is override during signIn
|
|
2162
2453
|
|
|
2163
|
-
|
|
2454
|
+
internalUser.getSession(
|
|
2164
2455
|
async (err, result) => {
|
|
2165
2456
|
if (err) {
|
|
2166
2457
|
logger.debug('failed to get the user session', err);
|
|
2167
2458
|
if (this.isSessionInvalid(err)) {
|
|
2168
2459
|
try {
|
|
2169
|
-
await this.cleanUpInvalidSession(
|
|
2460
|
+
await this.cleanUpInvalidSession(
|
|
2461
|
+
internalUser,
|
|
2462
|
+
userAgentValue
|
|
2463
|
+
);
|
|
2170
2464
|
} catch (cleanUpError) {
|
|
2171
2465
|
rej(
|
|
2172
2466
|
new Error(
|
|
@@ -2178,32 +2472,36 @@ export class InternalAuthClass {
|
|
|
2178
2472
|
}
|
|
2179
2473
|
return rej(err);
|
|
2180
2474
|
}
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2475
|
+
internalUser.globalSignOut(
|
|
2476
|
+
{
|
|
2477
|
+
onSuccess: data => {
|
|
2478
|
+
logger.debug('global sign out success');
|
|
2479
|
+
if (isSignedInHostedUI) {
|
|
2480
|
+
this.oAuthSignOutRedirect(res, rej);
|
|
2481
|
+
} else {
|
|
2482
|
+
return res();
|
|
2483
|
+
}
|
|
2484
|
+
},
|
|
2485
|
+
onFailure: err => {
|
|
2486
|
+
logger.debug('global sign out failed', err);
|
|
2487
|
+
return rej(err);
|
|
2488
|
+
},
|
|
2193
2489
|
},
|
|
2194
|
-
|
|
2490
|
+
userAgentValue
|
|
2491
|
+
);
|
|
2195
2492
|
},
|
|
2196
|
-
{ clientMetadata }
|
|
2493
|
+
{ clientMetadata },
|
|
2494
|
+
userAgentValue
|
|
2197
2495
|
);
|
|
2198
2496
|
} else {
|
|
2199
|
-
logger.debug('user sign out',
|
|
2200
|
-
|
|
2497
|
+
logger.debug('user sign out', internalUser);
|
|
2498
|
+
internalUser.signOut(() => {
|
|
2201
2499
|
if (isSignedInHostedUI) {
|
|
2202
2500
|
this.oAuthSignOutRedirect(res, rej);
|
|
2203
2501
|
} else {
|
|
2204
2502
|
return res();
|
|
2205
2503
|
}
|
|
2206
|
-
});
|
|
2504
|
+
}, userAgentValue);
|
|
2207
2505
|
}
|
|
2208
2506
|
});
|
|
2209
2507
|
}
|
|
@@ -2250,9 +2548,14 @@ export class InternalAuthClass {
|
|
|
2250
2548
|
}
|
|
2251
2549
|
|
|
2252
2550
|
if (this.userPool) {
|
|
2253
|
-
const
|
|
2254
|
-
|
|
2255
|
-
|
|
2551
|
+
const internalUser =
|
|
2552
|
+
this.userPool.getCurrentUser() as InternalCognitoUser;
|
|
2553
|
+
if (internalUser) {
|
|
2554
|
+
await this.cognitoIdentitySignOut(
|
|
2555
|
+
opts,
|
|
2556
|
+
internalUser,
|
|
2557
|
+
getAuthUserAgentValue(AuthAction.SignOut, customUserAgentDetails)
|
|
2558
|
+
);
|
|
2256
2559
|
} else {
|
|
2257
2560
|
logger.debug('no current Cognito user');
|
|
2258
2561
|
}
|
|
@@ -2291,9 +2594,15 @@ export class InternalAuthClass {
|
|
|
2291
2594
|
clientMetadata: ClientMetaData = this._config.clientMetadata,
|
|
2292
2595
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
2293
2596
|
): Promise<'SUCCESS'> {
|
|
2597
|
+
const internalUser: InternalCognitoUser | any = user;
|
|
2598
|
+
const userAgentValue = getAuthUserAgentValue(
|
|
2599
|
+
AuthAction.ChangePassword,
|
|
2600
|
+
customUserAgentDetails
|
|
2601
|
+
);
|
|
2602
|
+
|
|
2294
2603
|
return new Promise((resolve, reject) => {
|
|
2295
|
-
this.
|
|
2296
|
-
|
|
2604
|
+
this._userSession(userAgentValue, internalUser).then(session => {
|
|
2605
|
+
internalUser.changePassword(
|
|
2297
2606
|
oldPassword,
|
|
2298
2607
|
newPassword,
|
|
2299
2608
|
(err, data) => {
|
|
@@ -2304,7 +2613,8 @@ export class InternalAuthClass {
|
|
|
2304
2613
|
return resolve(data);
|
|
2305
2614
|
}
|
|
2306
2615
|
},
|
|
2307
|
-
clientMetadata
|
|
2616
|
+
clientMetadata,
|
|
2617
|
+
userAgentValue
|
|
2308
2618
|
);
|
|
2309
2619
|
});
|
|
2310
2620
|
});
|
|
@@ -2329,9 +2639,9 @@ export class InternalAuthClass {
|
|
|
2329
2639
|
return this.rejectAuthError(AuthErrorTypes.EmptyUsername);
|
|
2330
2640
|
}
|
|
2331
2641
|
|
|
2332
|
-
const
|
|
2642
|
+
const internalUser = this.createCognitoUser(username);
|
|
2333
2643
|
return new Promise((resolve, reject) => {
|
|
2334
|
-
|
|
2644
|
+
internalUser.forgotPassword(
|
|
2335
2645
|
{
|
|
2336
2646
|
onSuccess: () => {
|
|
2337
2647
|
resolve();
|
|
@@ -2350,14 +2660,15 @@ export class InternalAuthClass {
|
|
|
2350
2660
|
inputVerificationCode: data => {
|
|
2351
2661
|
dispatchAuthEvent(
|
|
2352
2662
|
'forgotPassword',
|
|
2353
|
-
|
|
2663
|
+
internalUser,
|
|
2354
2664
|
`${username} has initiated forgot password flow`
|
|
2355
2665
|
);
|
|
2356
2666
|
resolve(data);
|
|
2357
2667
|
return;
|
|
2358
2668
|
},
|
|
2359
2669
|
},
|
|
2360
|
-
clientMetadata
|
|
2670
|
+
clientMetadata,
|
|
2671
|
+
getAuthUserAgentValue(AuthAction.ForgotPassword, customUserAgentDetails)
|
|
2361
2672
|
);
|
|
2362
2673
|
});
|
|
2363
2674
|
}
|
|
@@ -2391,16 +2702,16 @@ export class InternalAuthClass {
|
|
|
2391
2702
|
return this.rejectAuthError(AuthErrorTypes.EmptyPassword);
|
|
2392
2703
|
}
|
|
2393
2704
|
|
|
2394
|
-
const
|
|
2705
|
+
const internalUser = this.createCognitoUser(username);
|
|
2395
2706
|
return new Promise((resolve, reject) => {
|
|
2396
|
-
|
|
2707
|
+
internalUser.confirmPassword(
|
|
2397
2708
|
code,
|
|
2398
2709
|
password,
|
|
2399
2710
|
{
|
|
2400
2711
|
onSuccess: success => {
|
|
2401
2712
|
dispatchAuthEvent(
|
|
2402
2713
|
'forgotPasswordSubmit',
|
|
2403
|
-
|
|
2714
|
+
internalUser,
|
|
2404
2715
|
`${username} forgotPasswordSubmit successful`
|
|
2405
2716
|
);
|
|
2406
2717
|
resolve(success);
|
|
@@ -2416,7 +2727,11 @@ export class InternalAuthClass {
|
|
|
2416
2727
|
return;
|
|
2417
2728
|
},
|
|
2418
2729
|
},
|
|
2419
|
-
clientMetadata
|
|
2730
|
+
clientMetadata,
|
|
2731
|
+
getAuthUserAgentValue(
|
|
2732
|
+
AuthAction.ForgotPasswordSubmit,
|
|
2733
|
+
customUserAgentDetails
|
|
2734
|
+
)
|
|
2420
2735
|
);
|
|
2421
2736
|
});
|
|
2422
2737
|
}
|
|
@@ -2431,17 +2746,25 @@ export class InternalAuthClass {
|
|
|
2431
2746
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
2432
2747
|
) {
|
|
2433
2748
|
const source = this.Credentials.getCredSource();
|
|
2749
|
+
const userAgentDetails = getAuthUserAgentDetails(
|
|
2750
|
+
AuthAction.CurrentUserInfo,
|
|
2751
|
+
customUserAgentDetails
|
|
2752
|
+
);
|
|
2434
2753
|
|
|
2435
2754
|
if (!source || source === 'aws' || source === 'userPool') {
|
|
2436
|
-
const
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2755
|
+
const internalUser: InternalCognitoUser = await this._currentUserPoolUser(
|
|
2756
|
+
undefined,
|
|
2757
|
+
userAgentDetails
|
|
2758
|
+
).catch(err => logger.error(err));
|
|
2759
|
+
if (!internalUser) {
|
|
2440
2760
|
return null;
|
|
2441
2761
|
}
|
|
2442
2762
|
|
|
2443
2763
|
try {
|
|
2444
|
-
const attributes = await this.
|
|
2764
|
+
const attributes = await this._userAttributes(
|
|
2765
|
+
internalUser,
|
|
2766
|
+
userAgentDetails
|
|
2767
|
+
);
|
|
2445
2768
|
const userAttrs: object = this.attributesToObject(attributes);
|
|
2446
2769
|
let credentials = null;
|
|
2447
2770
|
try {
|
|
@@ -2455,7 +2778,7 @@ export class InternalAuthClass {
|
|
|
2455
2778
|
|
|
2456
2779
|
const info = {
|
|
2457
2780
|
id: credentials ? credentials.identityId : undefined,
|
|
2458
|
-
username:
|
|
2781
|
+
username: internalUser.getUsername(),
|
|
2459
2782
|
attributes: userAttrs,
|
|
2460
2783
|
};
|
|
2461
2784
|
return info;
|
|
@@ -2521,6 +2844,14 @@ export class InternalAuthClass {
|
|
|
2521
2844
|
? this._config.oauth.redirectSignIn
|
|
2522
2845
|
: this._config.oauth.redirectUri;
|
|
2523
2846
|
|
|
2847
|
+
this._storage.setItem(
|
|
2848
|
+
'aws-amplify-federatedUserAgent',
|
|
2849
|
+
getAuthUserAgentValue(
|
|
2850
|
+
AuthAction.FederatedSignIn,
|
|
2851
|
+
customUserAgentDetails
|
|
2852
|
+
)
|
|
2853
|
+
);
|
|
2854
|
+
|
|
2524
2855
|
this._oAuthHandler.oauthSignIn(
|
|
2525
2856
|
this._config.oauth.responseType,
|
|
2526
2857
|
this._config.oauth.domain,
|
|
@@ -2550,7 +2881,7 @@ export class InternalAuthClass {
|
|
|
2550
2881
|
{ provider, token, identity_id, user, expires_at },
|
|
2551
2882
|
'federation'
|
|
2552
2883
|
);
|
|
2553
|
-
const currentUser = await this.
|
|
2884
|
+
const currentUser = await this._currentAuthenticatedUser();
|
|
2554
2885
|
dispatchAuthEvent(
|
|
2555
2886
|
'signIn',
|
|
2556
2887
|
currentUser,
|
|
@@ -2601,9 +2932,15 @@ export class InternalAuthClass {
|
|
|
2601
2932
|
|
|
2602
2933
|
if (hasCodeOrError || hasTokenOrError) {
|
|
2603
2934
|
this._storage.setItem('amplify-redirected-from-hosted-ui', 'true');
|
|
2935
|
+
const userAgentValue =
|
|
2936
|
+
this._storage.getItem('aws-amplify-federatedUserAgent') || undefined;
|
|
2937
|
+
this._storage.removeItem('aws-amplify-federatedUserAgent');
|
|
2604
2938
|
try {
|
|
2605
2939
|
const { accessToken, idToken, refreshToken, state } =
|
|
2606
|
-
await this._oAuthHandler.handleAuthResponse(
|
|
2940
|
+
await this._oAuthHandler.handleAuthResponse(
|
|
2941
|
+
currentUrl,
|
|
2942
|
+
userAgentValue
|
|
2943
|
+
);
|
|
2607
2944
|
const session = new CognitoUserSession({
|
|
2608
2945
|
IdToken: new CognitoIdToken({ IdToken: idToken }),
|
|
2609
2946
|
RefreshToken: new CognitoRefreshToken({
|
|
@@ -2745,7 +3082,7 @@ export class InternalAuthClass {
|
|
|
2745
3082
|
);
|
|
2746
3083
|
}
|
|
2747
3084
|
|
|
2748
|
-
private createCognitoUser(username: string):
|
|
3085
|
+
private createCognitoUser(username: string): InternalCognitoUser {
|
|
2749
3086
|
const userData: ICognitoUserData = {
|
|
2750
3087
|
Username: username,
|
|
2751
3088
|
Pool: this.userPool,
|
|
@@ -2754,11 +3091,11 @@ export class InternalAuthClass {
|
|
|
2754
3091
|
|
|
2755
3092
|
const { authenticationFlowType } = this._config;
|
|
2756
3093
|
|
|
2757
|
-
const
|
|
3094
|
+
const internalUser = new InternalCognitoUser(userData);
|
|
2758
3095
|
if (authenticationFlowType) {
|
|
2759
|
-
|
|
3096
|
+
internalUser.setAuthenticationFlowType(authenticationFlowType);
|
|
2760
3097
|
}
|
|
2761
|
-
return
|
|
3098
|
+
return internalUser;
|
|
2762
3099
|
}
|
|
2763
3100
|
|
|
2764
3101
|
private _isValidAuthStorage(obj) {
|
|
@@ -2793,78 +3130,105 @@ export class InternalAuthClass {
|
|
|
2793
3130
|
public async rememberDevice(
|
|
2794
3131
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
2795
3132
|
): Promise<string | AuthError> {
|
|
2796
|
-
let
|
|
3133
|
+
let internalUser: InternalCognitoUser | any;
|
|
3134
|
+
const userAgentDetails = getAuthUserAgentDetails(
|
|
3135
|
+
AuthAction.RememberDevice,
|
|
3136
|
+
customUserAgentDetails
|
|
3137
|
+
);
|
|
2797
3138
|
|
|
2798
3139
|
try {
|
|
2799
|
-
|
|
3140
|
+
internalUser = await this._currentUserPoolUser(
|
|
3141
|
+
undefined,
|
|
3142
|
+
userAgentDetails
|
|
3143
|
+
);
|
|
2800
3144
|
} catch (error) {
|
|
2801
3145
|
logger.debug('The user is not authenticated by the error', error);
|
|
2802
3146
|
return Promise.reject('The user is not authenticated');
|
|
2803
3147
|
}
|
|
2804
3148
|
|
|
2805
|
-
|
|
3149
|
+
internalUser.getCachedDeviceKeyAndPassword();
|
|
2806
3150
|
return new Promise((res, rej) => {
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
3151
|
+
internalUser.setDeviceStatusRemembered(
|
|
3152
|
+
{
|
|
3153
|
+
onSuccess: data => {
|
|
3154
|
+
res(data);
|
|
3155
|
+
},
|
|
3156
|
+
onFailure: err => {
|
|
3157
|
+
if (err.code === 'InvalidParameterException') {
|
|
3158
|
+
rej(new AuthError(AuthErrorTypes.DeviceConfig));
|
|
3159
|
+
} else if (err.code === 'NetworkError') {
|
|
3160
|
+
rej(new AuthError(AuthErrorTypes.NetworkError));
|
|
3161
|
+
} else {
|
|
3162
|
+
rej(err);
|
|
3163
|
+
}
|
|
3164
|
+
},
|
|
2819
3165
|
},
|
|
2820
|
-
|
|
3166
|
+
getAmplifyUserAgent(userAgentDetails)
|
|
3167
|
+
);
|
|
2821
3168
|
});
|
|
2822
3169
|
}
|
|
2823
3170
|
|
|
2824
3171
|
public async forgetDevice(
|
|
2825
3172
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
2826
3173
|
): Promise<void> {
|
|
2827
|
-
let
|
|
3174
|
+
let internalUser: InternalCognitoUser | any;
|
|
3175
|
+
const userAgentDetails = getAuthUserAgentDetails(
|
|
3176
|
+
AuthAction.ForgetDevice,
|
|
3177
|
+
customUserAgentDetails
|
|
3178
|
+
);
|
|
2828
3179
|
|
|
2829
3180
|
try {
|
|
2830
|
-
|
|
3181
|
+
internalUser = await this._currentUserPoolUser(
|
|
3182
|
+
undefined,
|
|
3183
|
+
userAgentDetails
|
|
3184
|
+
);
|
|
2831
3185
|
} catch (error) {
|
|
2832
3186
|
logger.debug('The user is not authenticated by the error', error);
|
|
2833
3187
|
return Promise.reject('The user is not authenticated');
|
|
2834
3188
|
}
|
|
2835
3189
|
|
|
2836
|
-
|
|
3190
|
+
internalUser.getCachedDeviceKeyAndPassword();
|
|
2837
3191
|
return new Promise((res, rej) => {
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
3192
|
+
internalUser.forgetDevice(
|
|
3193
|
+
{
|
|
3194
|
+
onSuccess: data => {
|
|
3195
|
+
res(data);
|
|
3196
|
+
},
|
|
3197
|
+
onFailure: err => {
|
|
3198
|
+
if (err.code === 'InvalidParameterException') {
|
|
3199
|
+
rej(new AuthError(AuthErrorTypes.DeviceConfig));
|
|
3200
|
+
} else if (err.code === 'NetworkError') {
|
|
3201
|
+
rej(new AuthError(AuthErrorTypes.NetworkError));
|
|
3202
|
+
} else {
|
|
3203
|
+
rej(err);
|
|
3204
|
+
}
|
|
3205
|
+
},
|
|
2850
3206
|
},
|
|
2851
|
-
|
|
3207
|
+
getAmplifyUserAgent(userAgentDetails)
|
|
3208
|
+
);
|
|
2852
3209
|
});
|
|
2853
3210
|
}
|
|
2854
3211
|
|
|
2855
3212
|
public async fetchDevices(
|
|
2856
3213
|
customUserAgentDetails?: CustomUserAgentDetails
|
|
2857
3214
|
): Promise<IAuthDevice[]> {
|
|
2858
|
-
let
|
|
3215
|
+
let internalUser: InternalCognitoUser | any;
|
|
3216
|
+
const userAgentDetails = getAuthUserAgentDetails(
|
|
3217
|
+
AuthAction.FetchDevices,
|
|
3218
|
+
customUserAgentDetails
|
|
3219
|
+
);
|
|
2859
3220
|
|
|
2860
3221
|
try {
|
|
2861
|
-
|
|
3222
|
+
internalUser = await this._currentUserPoolUser(
|
|
3223
|
+
undefined,
|
|
3224
|
+
userAgentDetails
|
|
3225
|
+
);
|
|
2862
3226
|
} catch (error) {
|
|
2863
3227
|
logger.debug('The user is not authenticated by the error', error);
|
|
2864
3228
|
throw new Error('The user is not authenticated');
|
|
2865
3229
|
}
|
|
2866
3230
|
|
|
2867
|
-
|
|
3231
|
+
internalUser.getCachedDeviceKeyAndPassword();
|
|
2868
3232
|
return new Promise((res, rej) => {
|
|
2869
3233
|
const cb = {
|
|
2870
3234
|
onSuccess(data) {
|
|
@@ -2892,7 +3256,12 @@ export class InternalAuthClass {
|
|
|
2892
3256
|
}
|
|
2893
3257
|
},
|
|
2894
3258
|
};
|
|
2895
|
-
|
|
3259
|
+
internalUser.listDevices(
|
|
3260
|
+
MAX_DEVICES,
|
|
3261
|
+
null,
|
|
3262
|
+
cb,
|
|
3263
|
+
getAmplifyUserAgent(userAgentDetails)
|
|
3264
|
+
);
|
|
2896
3265
|
});
|
|
2897
3266
|
}
|
|
2898
3267
|
}
|