@axa-fr/react-oidc 6.18.3 → 6.19.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.
@@ -91,97 +91,104 @@ const isTokensOidcValid = (
91
91
  return { isValid: true, reason: '' };
92
92
  };
93
93
 
94
- function hideTokens(currentDatabaseElement: OidcConfig) {
95
- const configurationName = currentDatabaseElement.configurationName;
96
- return (response: Response) => {
97
- if (response.status !== 200) {
98
- return response;
94
+ function _hideTokens(tokens: Tokens, currentDatabaseElement: OidcConfig, configurationName: string) {
95
+ if (!tokens.issued_at) {
96
+ const currentTimeUnixSecond = new Date().getTime() / 1000;
97
+ tokens.issued_at = currentTimeUnixSecond;
98
+ }
99
+
100
+ const accessTokenPayload = extractTokenPayload(tokens.access_token);
101
+ const secureTokens = {
102
+ ...tokens,
103
+ accessTokenPayload,
104
+ };
105
+ if (currentDatabaseElement.hideAccessToken) {
106
+ secureTokens.access_token = TOKEN.ACCESS_TOKEN + '_' + configurationName;
107
+ }
108
+ tokens.accessTokenPayload = accessTokenPayload;
109
+
110
+ let _idTokenPayload = null;
111
+ if (tokens.id_token) {
112
+ _idTokenPayload = extractTokenPayload(tokens.id_token);
113
+ tokens.idTokenPayload = {..._idTokenPayload};
114
+ if (_idTokenPayload.nonce && currentDatabaseElement.nonce != null) {
115
+ const keyNonce =
116
+ TOKEN.NONCE_TOKEN + '_' + currentDatabaseElement.configurationName;
117
+ _idTokenPayload.nonce = keyNonce;
99
118
  }
100
- return response.json().then<Response>((tokens: Tokens) => {
101
- if (!tokens.issued_at) {
102
- const currentTimeUnixSecond = new Date().getTime() / 1000;
103
- tokens.issued_at = currentTimeUnixSecond;
104
- }
105
-
106
- const accessTokenPayload = extractTokenPayload(tokens.access_token);
107
- const secureTokens = {
108
- ...tokens,
109
- access_token: TOKEN.ACCESS_TOKEN + '_' + configurationName,
110
- accessTokenPayload,
111
- };
112
- tokens.accessTokenPayload = accessTokenPayload;
113
-
114
- let _idTokenPayload = null;
115
- if (tokens.id_token) {
116
- _idTokenPayload = extractTokenPayload(tokens.id_token);
117
- tokens.idTokenPayload = { ..._idTokenPayload };
118
- if (_idTokenPayload.nonce && currentDatabaseElement.nonce != null) {
119
- const keyNonce =
120
- TOKEN.NONCE_TOKEN + '_' + currentDatabaseElement.configurationName;
121
- _idTokenPayload.nonce = keyNonce;
122
- }
123
- secureTokens.idTokenPayload = _idTokenPayload;
124
- }
125
- if (tokens.refresh_token) {
126
- secureTokens.refresh_token =
127
- TOKEN.REFRESH_TOKEN + '_' + configurationName;
128
- }
129
-
130
- const idTokenExpiresAt =
131
- _idTokenPayload && _idTokenPayload.exp
119
+ secureTokens.idTokenPayload = _idTokenPayload;
120
+ }
121
+ if (tokens.refresh_token) {
122
+ secureTokens.refresh_token =
123
+ TOKEN.REFRESH_TOKEN + '_' + configurationName;
124
+ }
125
+
126
+ const idTokenExpiresAt =
127
+ _idTokenPayload && _idTokenPayload.exp
132
128
  ? _idTokenPayload.exp
133
129
  : Number.MAX_VALUE;
134
- const accessTokenExpiresAt =
135
- accessTokenPayload && accessTokenPayload.exp
130
+ const accessTokenExpiresAt =
131
+ accessTokenPayload && accessTokenPayload.exp
136
132
  ? accessTokenPayload.exp
137
133
  : tokens.issued_at + tokens.expires_in;
138
134
 
139
- let expiresAt: number;
140
- const tokenRenewMode = (
141
- currentDatabaseElement.oidcConfiguration as OidcConfiguration
142
- ).token_renew_mode;
143
- if (tokenRenewMode === TokenRenewMode.access_token_invalid) {
144
- expiresAt = accessTokenExpiresAt;
145
- } else if (tokenRenewMode === TokenRenewMode.id_token_invalid) {
146
- expiresAt = idTokenExpiresAt;
147
- } else {
148
- expiresAt =
149
- idTokenExpiresAt < accessTokenExpiresAt
135
+ let expiresAt: number;
136
+ const tokenRenewMode = (
137
+ currentDatabaseElement.oidcConfiguration as OidcConfiguration
138
+ ).token_renew_mode;
139
+ if (tokenRenewMode === TokenRenewMode.access_token_invalid) {
140
+ expiresAt = accessTokenExpiresAt;
141
+ } else if (tokenRenewMode === TokenRenewMode.id_token_invalid) {
142
+ expiresAt = idTokenExpiresAt;
143
+ } else {
144
+ expiresAt =
145
+ idTokenExpiresAt < accessTokenExpiresAt
150
146
  ? idTokenExpiresAt
151
147
  : accessTokenExpiresAt;
152
- }
153
- secureTokens.expiresAt = expiresAt;
154
-
155
- tokens.expiresAt = expiresAt;
156
- const nonce = currentDatabaseElement.nonce
157
- ? currentDatabaseElement.nonce.nonce
158
- : null;
159
- const { isValid, reason } = isTokensOidcValid(
160
- tokens,
161
- nonce,
162
- currentDatabaseElement.oidcServerConfiguration as OidcServerConfiguration
163
- ); //TODO: Type assertion, could be null.
164
- if (!isValid) {
165
- throw Error(`Tokens are not OpenID valid, reason: ${reason}`);
166
- }
167
-
168
- // When refresh_token is not rotated we reuse ald refresh_token
169
- if (
170
- currentDatabaseElement.tokens != null &&
171
- 'refresh_token' in currentDatabaseElement.tokens &&
172
- !('refresh_token' in tokens)
173
- ) {
174
- const refreshToken = currentDatabaseElement.tokens.refresh_token;
175
-
176
- currentDatabaseElement.tokens = {
177
- ...tokens,
178
- refresh_token: refreshToken,
179
- };
180
- } else {
181
- currentDatabaseElement.tokens = tokens;
182
- }
183
-
184
- currentDatabaseElement.status = 'LOGGED_IN';
148
+ }
149
+ secureTokens.expiresAt = expiresAt;
150
+
151
+ tokens.expiresAt = expiresAt;
152
+ const nonce = currentDatabaseElement.nonce
153
+ ? currentDatabaseElement.nonce.nonce
154
+ : null;
155
+ const {isValid, reason} = isTokensOidcValid(
156
+ tokens,
157
+ nonce,
158
+ currentDatabaseElement.oidcServerConfiguration as OidcServerConfiguration
159
+ ); //TODO: Type assertion, could be null.
160
+ if (!isValid) {
161
+ throw Error(`Tokens are not OpenID valid, reason: ${reason}`);
162
+ }
163
+
164
+ // When refresh_token is not rotated we reuse ald refresh_token
165
+ if (
166
+ currentDatabaseElement.tokens != null &&
167
+ 'refresh_token' in currentDatabaseElement.tokens &&
168
+ !('refresh_token' in tokens)
169
+ ) {
170
+ const refreshToken = currentDatabaseElement.tokens.refresh_token;
171
+
172
+ currentDatabaseElement.tokens = {
173
+ ...tokens,
174
+ refresh_token: refreshToken,
175
+ };
176
+ } else {
177
+ currentDatabaseElement.tokens = tokens;
178
+ }
179
+
180
+ currentDatabaseElement.status = 'LOGGED_IN';
181
+ return secureTokens;
182
+ }
183
+
184
+ function hideTokens(currentDatabaseElement: OidcConfig) {
185
+ const configurationName = currentDatabaseElement.configurationName;
186
+ return (response: Response) => {
187
+ if (response.status !== 200) {
188
+ return response;
189
+ }
190
+ return response.json().then<Response>((tokens: Tokens) => {
191
+ const secureTokens = _hideTokens(tokens, currentDatabaseElement, configurationName);
185
192
  const body = JSON.stringify(secureTokens);
186
193
  return new Response(body, response);
187
194
  });
@@ -194,5 +201,6 @@ export {
194
201
  isTokensValid,
195
202
  extractTokenPayload,
196
203
  isTokensOidcValid,
197
- hideTokens
204
+ hideTokens,
205
+ _hideTokens
198
206
  };