@aws-amplify/auth 6.4.2 → 6.4.3-unstable.555defc.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.
Files changed (32) hide show
  1. package/dist/cjs/providers/cognito/apis/confirmSignIn.js +1 -1
  2. package/dist/cjs/providers/cognito/apis/confirmSignIn.js.map +1 -1
  3. package/dist/cjs/providers/cognito/apis/signInWithCustomAuth.js +2 -2
  4. package/dist/cjs/providers/cognito/apis/signInWithCustomAuth.js.map +1 -1
  5. package/dist/cjs/providers/cognito/apis/signInWithCustomSRPAuth.js +2 -2
  6. package/dist/cjs/providers/cognito/apis/signInWithCustomSRPAuth.js.map +1 -1
  7. package/dist/cjs/providers/cognito/apis/signInWithSRP.js +2 -2
  8. package/dist/cjs/providers/cognito/apis/signInWithSRP.js.map +1 -1
  9. package/dist/cjs/providers/cognito/apis/signInWithUserPassword.js +2 -2
  10. package/dist/cjs/providers/cognito/apis/signInWithUserPassword.js.map +1 -1
  11. package/dist/cjs/providers/cognito/utils/signInStore.js +66 -14
  12. package/dist/cjs/providers/cognito/utils/signInStore.js.map +1 -1
  13. package/dist/esm/providers/cognito/apis/confirmSignIn.mjs +2 -2
  14. package/dist/esm/providers/cognito/apis/confirmSignIn.mjs.map +1 -1
  15. package/dist/esm/providers/cognito/apis/signInWithCustomAuth.mjs +3 -3
  16. package/dist/esm/providers/cognito/apis/signInWithCustomAuth.mjs.map +1 -1
  17. package/dist/esm/providers/cognito/apis/signInWithCustomSRPAuth.mjs +3 -3
  18. package/dist/esm/providers/cognito/apis/signInWithCustomSRPAuth.mjs.map +1 -1
  19. package/dist/esm/providers/cognito/apis/signInWithSRP.mjs +3 -3
  20. package/dist/esm/providers/cognito/apis/signInWithSRP.mjs.map +1 -1
  21. package/dist/esm/providers/cognito/apis/signInWithUserPassword.mjs +3 -3
  22. package/dist/esm/providers/cognito/apis/signInWithUserPassword.mjs.map +1 -1
  23. package/dist/esm/providers/cognito/utils/signInStore.d.ts +3 -1
  24. package/dist/esm/providers/cognito/utils/signInStore.mjs +66 -13
  25. package/dist/esm/providers/cognito/utils/signInStore.mjs.map +1 -1
  26. package/package.json +104 -104
  27. package/src/providers/cognito/apis/confirmSignIn.ts +3 -6
  28. package/src/providers/cognito/apis/signInWithCustomAuth.ts +3 -6
  29. package/src/providers/cognito/apis/signInWithCustomSRPAuth.ts +3 -6
  30. package/src/providers/cognito/apis/signInWithSRP.ts +3 -6
  31. package/src/providers/cognito/apis/signInWithUserPassword.ts +3 -6
  32. package/src/providers/cognito/utils/signInStore.ts +99 -13
@@ -27,10 +27,7 @@ import {
27
27
  SignInWithSRPInput,
28
28
  SignInWithSRPOutput,
29
29
  } from '../types';
30
- import {
31
- cleanActiveSignInState,
32
- setActiveSignInState,
33
- } from '../utils/signInStore';
30
+ import { setActiveSignInState, signInStore } from '../utils/signInStore';
34
31
  import { cacheCognitoTokens } from '../tokenProvider/cacheTokens';
35
32
  import { tokenOrchestrator } from '../tokenProvider';
36
33
  import { dispatchSignedInHubEvent } from '../utils/dispatchSignedInHubEvent';
@@ -89,7 +86,7 @@ export async function signInWithSRP(
89
86
  signInDetails,
90
87
  });
91
88
  if (AuthenticationResult) {
92
- cleanActiveSignInState();
89
+ signInStore.dispatch({ type: 'RESET_STATE' });
93
90
  await cacheCognitoTokens({
94
91
  username: activeUsername,
95
92
  ...AuthenticationResult,
@@ -115,7 +112,7 @@ export async function signInWithSRP(
115
112
  challengeParameters: handledChallengeParameters as ChallengeParameters,
116
113
  });
117
114
  } catch (error) {
118
- cleanActiveSignInState();
115
+ signInStore.dispatch({ type: 'RESET_STATE' });
119
116
  assertServiceError(error);
120
117
  const result = getSignInResultFromError(error.name);
121
118
  if (result) return result;
@@ -25,10 +25,7 @@ import {
25
25
  SignInWithUserPasswordInput,
26
26
  SignInWithUserPasswordOutput,
27
27
  } from '../types';
28
- import {
29
- cleanActiveSignInState,
30
- setActiveSignInState,
31
- } from '../utils/signInStore';
28
+ import { setActiveSignInState, signInStore } from '../utils/signInStore';
32
29
  import { cacheCognitoTokens } from '../tokenProvider/cacheTokens';
33
30
  import { tokenOrchestrator } from '../tokenProvider';
34
31
  import { dispatchSignedInHubEvent } from '../utils/dispatchSignedInHubEvent';
@@ -84,6 +81,7 @@ export async function signInWithUserPassword(
84
81
  signInDetails,
85
82
  });
86
83
  if (AuthenticationResult) {
84
+ signInStore.dispatch({ type: 'RESET_STATE' });
87
85
  await cacheCognitoTokens({
88
86
  ...AuthenticationResult,
89
87
  username: activeUsername,
@@ -95,7 +93,6 @@ export async function signInWithUserPassword(
95
93
  }),
96
94
  signInDetails,
97
95
  });
98
- cleanActiveSignInState();
99
96
 
100
97
  await dispatchSignedInHubEvent();
101
98
 
@@ -110,7 +107,7 @@ export async function signInWithUserPassword(
110
107
  challengeParameters: retriedChallengeParameters as ChallengeParameters,
111
108
  });
112
109
  } catch (error) {
113
- cleanActiveSignInState();
110
+ signInStore.dispatch({ type: 'RESET_STATE' });
114
111
  assertServiceError(error);
115
112
  const result = getSignInResultFromError(error.name);
116
113
  if (result) return result;
@@ -1,6 +1,8 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ import { syncSessionStorage } from '@aws-amplify/core';
5
+
4
6
  import { CognitoAuthSignInDetails } from '../types';
5
7
  import { ChallengeName } from '../../../foundation/factories/serviceClients/cognitoIdentityProvider/types';
6
8
 
@@ -17,7 +19,8 @@ type SignInAction =
17
19
  | { type: 'SET_SIGN_IN_STATE'; value: SignInState }
18
20
  | { type: 'SET_USERNAME'; value?: string }
19
21
  | { type: 'SET_CHALLENGE_NAME'; value?: ChallengeName }
20
- | { type: 'SET_SIGN_IN_SESSION'; value?: string };
22
+ | { type: 'SET_SIGN_IN_SESSION'; value?: string }
23
+ | { type: 'RESET_STATE' };
21
24
 
22
25
  type Store<State, Action> = (reducer: Reducer<State, Action>) => {
23
26
  getState(): ReturnType<Reducer<State, Action>>;
@@ -26,44 +29,110 @@ type Store<State, Action> = (reducer: Reducer<State, Action>) => {
26
29
 
27
30
  type Reducer<State, Action> = (state: State, action: Action) => State;
28
31
 
32
+ // Minutes until stored session invalidates
33
+ const MS_TO_EXPIRY = 3 * 60 * 1000; // 3 mins
34
+ const TGT_STATE = 'CognitoSignInState';
35
+ const SIGN_IN_STATE_KEYS = {
36
+ username: `${TGT_STATE}.username`,
37
+ challengeName: `${TGT_STATE}.challengeName`,
38
+ signInSession: `${TGT_STATE}.signInSession`,
39
+ expiry: `${TGT_STATE}.expiry`,
40
+ };
41
+
29
42
  const signInReducer: Reducer<SignInState, SignInAction> = (state, action) => {
30
43
  switch (action.type) {
31
44
  case 'SET_SIGN_IN_SESSION':
45
+ persistSignInState({ signInSession: action.value });
46
+
32
47
  return {
33
48
  ...state,
34
49
  signInSession: action.value,
35
50
  };
51
+
36
52
  case 'SET_SIGN_IN_STATE':
53
+ persistSignInState(action.value);
54
+
37
55
  return {
38
56
  ...action.value,
39
57
  };
58
+
40
59
  case 'SET_CHALLENGE_NAME':
60
+ persistSignInState({ challengeName: action.value });
61
+
41
62
  return {
42
63
  ...state,
43
64
  challengeName: action.value,
44
65
  };
66
+
45
67
  case 'SET_USERNAME':
68
+ persistSignInState({ username: action.value });
69
+
46
70
  return {
47
71
  ...state,
48
72
  username: action.value,
49
73
  };
74
+
50
75
  case 'SET_INITIAL_STATE':
51
- return defaultState();
76
+ return initializeState();
77
+
78
+ case 'RESET_STATE':
79
+ clearPersistedSignInState();
80
+
81
+ return getDefaultState();
82
+
83
+ // this state is never reachable
52
84
  default:
53
85
  return state;
54
86
  }
55
87
  };
56
88
 
57
- function defaultState(): SignInState {
58
- return {
59
- username: undefined,
60
- challengeName: undefined,
61
- signInSession: undefined,
62
- };
63
- }
89
+ const isExpired = (expiryDate: string | null): boolean => {
90
+ const expiryTimestamp = Number(expiryDate);
91
+ const currentTimestamp = Date.now();
92
+
93
+ return expiryTimestamp <= currentTimestamp;
94
+ };
95
+
96
+ const clearPersistedSignInState = () => {
97
+ for (const storedKey of Object.values(SIGN_IN_STATE_KEYS)) {
98
+ syncSessionStorage.removeItem(storedKey);
99
+ }
100
+ };
101
+
102
+ const getDefaultState = (): SignInState => ({
103
+ username: undefined,
104
+ challengeName: undefined,
105
+ signInSession: undefined,
106
+ });
107
+
108
+ // Hydrate signInStore from Synced Session Storage
109
+ const initializeState = (): SignInState => {
110
+ const expiry = syncSessionStorage.getItem(SIGN_IN_STATE_KEYS.expiry);
111
+
112
+ if (!expiry || (expiry && isExpired(expiry))) {
113
+ clearPersistedSignInState();
114
+
115
+ return getDefaultState();
116
+ } else {
117
+ const username =
118
+ syncSessionStorage.getItem(SIGN_IN_STATE_KEYS.username) ?? undefined;
119
+
120
+ const challengeName = (syncSessionStorage.getItem(
121
+ SIGN_IN_STATE_KEYS.challengeName,
122
+ ) ?? undefined) as ChallengeName;
123
+ const signInSession =
124
+ syncSessionStorage.getItem(SIGN_IN_STATE_KEYS.signInSession) ?? undefined;
125
+
126
+ return {
127
+ username,
128
+ challengeName,
129
+ signInSession,
130
+ };
131
+ }
132
+ };
64
133
 
65
134
  const createStore: Store<SignInState, SignInAction> = reducer => {
66
- let currentState = reducer(defaultState(), { type: 'SET_INITIAL_STATE' });
135
+ let currentState = reducer(getDefaultState(), { type: 'SET_INITIAL_STATE' });
67
136
 
68
137
  return {
69
138
  getState: () => currentState,
@@ -82,6 +151,23 @@ export function setActiveSignInState(state: SignInState): void {
82
151
  });
83
152
  }
84
153
 
85
- export function cleanActiveSignInState(): void {
86
- signInStore.dispatch({ type: 'SET_INITIAL_STATE' });
87
- }
154
+ // Save local state into Session Storage
155
+ export const persistSignInState = ({
156
+ challengeName,
157
+ signInSession,
158
+ username,
159
+ }: SignInState) => {
160
+ username && syncSessionStorage.setItem(SIGN_IN_STATE_KEYS.username, username);
161
+ challengeName &&
162
+ syncSessionStorage.setItem(SIGN_IN_STATE_KEYS.challengeName, challengeName);
163
+
164
+ if (signInSession) {
165
+ syncSessionStorage.setItem(SIGN_IN_STATE_KEYS.signInSession, signInSession);
166
+
167
+ // Updates expiry when session is passed
168
+ syncSessionStorage.setItem(
169
+ SIGN_IN_STATE_KEYS.expiry,
170
+ String(Date.now() + MS_TO_EXPIRY),
171
+ );
172
+ }
173
+ };