@axa-fr/react-oidc 7.22.18 → 7.22.19

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 (39) hide show
  1. package/README.md +87 -114
  2. package/bin/copy-service-worker-files.mjs +24 -17
  3. package/dist/FetchToken.d.ts.map +1 -1
  4. package/dist/OidcProvider.d.ts +1 -1
  5. package/dist/OidcProvider.d.ts.map +1 -1
  6. package/dist/OidcSecure.d.ts.map +1 -1
  7. package/dist/OidcTrustedDomains.js +13 -10
  8. package/dist/ReactOidc.d.ts.map +1 -1
  9. package/dist/User.d.ts.map +1 -1
  10. package/dist/core/default-component/Authenticating.component.d.ts.map +1 -1
  11. package/dist/core/default-component/Callback.component.d.ts.map +1 -1
  12. package/dist/core/default-component/Loading.component.d.ts.map +1 -1
  13. package/dist/core/default-component/ServiceWorkerNotSupported.component.d.ts.map +1 -1
  14. package/dist/core/default-component/SilentCallback.component.d.ts +5 -2
  15. package/dist/core/default-component/SilentCallback.component.d.ts.map +1 -1
  16. package/dist/core/routes/OidcRoutes.d.ts.map +1 -1
  17. package/dist/core/routes/withRouter.d.ts.map +1 -1
  18. package/dist/index.d.ts +2 -2
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +165 -72
  21. package/dist/index.umd.cjs +1 -1
  22. package/package.json +3 -3
  23. package/src/FetchToken.tsx +36 -13
  24. package/src/OidcProvider.tsx +222 -176
  25. package/src/OidcSecure.tsx +34 -23
  26. package/src/ReactOidc.tsx +181 -142
  27. package/src/User.ts +60 -50
  28. package/src/core/default-component/Authenticating.component.tsx +1 -1
  29. package/src/core/default-component/Callback.component.tsx +18 -11
  30. package/src/core/default-component/Loading.component.tsx +1 -5
  31. package/src/core/default-component/ServiceWorkerNotSupported.component.tsx +5 -2
  32. package/src/core/default-component/SessionLost.component.tsx +1 -1
  33. package/src/core/default-component/SilentCallback.component.tsx +17 -11
  34. package/src/core/default-component/SilentLogin.component.tsx +18 -18
  35. package/src/core/routes/OidcRoutes.spec.tsx +2 -2
  36. package/src/core/routes/OidcRoutes.tsx +12 -5
  37. package/src/core/routes/withRouter.spec.tsx +5 -6
  38. package/src/core/routes/withRouter.tsx +21 -19
  39. package/src/index.ts +8 -3
@@ -1,210 +1,256 @@
1
- import {Fetch, getFetchDefault, OidcConfiguration, OidcClient, ILOidcLocation, OidcLocation} from '@axa-fr/oidc-client';
1
+ import {
2
+ Fetch,
3
+ getFetchDefault,
4
+ ILOidcLocation,
5
+ OidcClient,
6
+ OidcConfiguration,
7
+ OidcLocation,
8
+ } from '@axa-fr/oidc-client';
2
9
  import { ComponentType, FC, PropsWithChildren, useEffect, useState } from 'react';
3
10
 
4
11
  import AuthenticatingError from './core/default-component/AuthenticateError.component.js';
5
- import { Authenticating, CallBackSuccess, Loading, SessionLost } from './core/default-component/index.js';
12
+ import {
13
+ Authenticating,
14
+ CallBackSuccess,
15
+ Loading,
16
+ SessionLost,
17
+ } from './core/default-component/index.js';
6
18
  import ServiceWorkerNotSupported from './core/default-component/ServiceWorkerNotSupported.component.js';
7
19
  import OidcRoutes from './core/routes/OidcRoutes.js';
8
20
  import { CustomHistory } from './core/routes/withRouter.js';
9
21
 
10
22
  export type oidcContext = {
11
- (name?: string): OidcClient;
23
+ (name?: string): OidcClient;
12
24
  };
13
25
 
14
26
  const defaultEventState = { name: '', data: null };
15
27
 
16
28
  export type OidcProviderProps = {
17
- callbackSuccessComponent?: ComponentType<any>;
18
- sessionLostComponent?: ComponentType<any>;
19
- authenticatingComponent?: ComponentType<any>;
20
- authenticatingErrorComponent?: ComponentType<any>;
21
- loadingComponent?: ComponentType<any>;
22
- serviceWorkerNotSupportedComponent?: ComponentType<any>;
23
- configurationName?: string;
24
- configuration?: OidcConfiguration;
25
- children: any;
26
- onSessionLost?: () => void;
27
- onLogoutFromAnotherTab?: () => void;
28
- onLogoutFromSameTab?: () => void;
29
- withCustomHistory?: () => CustomHistory;
30
- onEvent?: (configuration: string, name: string, data: any) => void;
31
- getFetch?: () => Fetch;
32
- location?: ILOidcLocation
29
+ callbackSuccessComponent?: ComponentType<any>;
30
+ sessionLostComponent?: ComponentType<any>;
31
+ authenticatingComponent?: ComponentType<any>;
32
+ authenticatingErrorComponent?: ComponentType<any>;
33
+ loadingComponent?: ComponentType<any>;
34
+ serviceWorkerNotSupportedComponent?: ComponentType<any>;
35
+ configurationName?: string;
36
+ configuration?: OidcConfiguration;
37
+ children: any;
38
+ onSessionLost?: () => void;
39
+ onLogoutFromAnotherTab?: () => void;
40
+ onLogoutFromSameTab?: () => void;
41
+ withCustomHistory?: () => CustomHistory;
42
+ onEvent?: (configuration: string, name: string, data: any) => void;
43
+ getFetch?: () => Fetch;
44
+ location?: ILOidcLocation;
33
45
  };
34
46
 
35
47
  export type OidcSessionProps = {
36
- configurationName: string;
37
- loadingComponent: PropsWithChildren<any>;
48
+ configurationName: string;
49
+ loadingComponent: PropsWithChildren<any>;
38
50
  };
39
51
 
40
- const OidcSession: FC<PropsWithChildren<OidcSessionProps>> = ({ loadingComponent, children, configurationName }) => {
41
- const [isLoading, setIsLoading] = useState(true);
42
- const getOidc = OidcClient.get;
43
- const oidc = getOidc(configurationName);
44
- useEffect(() => {
45
- let isMounted = true;
46
- if (oidc) {
47
- oidc.tryKeepExistingSessionAsync().then(() => {
48
- if (isMounted) {
49
- setIsLoading(false);
50
- }
51
- });
52
+ const OidcSession: FC<PropsWithChildren<OidcSessionProps>> = ({
53
+ loadingComponent,
54
+ children,
55
+ configurationName,
56
+ }) => {
57
+ const [isLoading, setIsLoading] = useState(true);
58
+ const getOidc = OidcClient.get;
59
+ const oidc = getOidc(configurationName);
60
+ useEffect(() => {
61
+ let isMounted = true;
62
+ if (oidc) {
63
+ oidc.tryKeepExistingSessionAsync().then(() => {
64
+ if (isMounted) {
65
+ setIsLoading(false);
52
66
  }
53
- return () => {
54
- isMounted = false;
55
- };
56
- // eslint-disable-next-line react-hooks/exhaustive-deps
57
- }, [configurationName]);
58
- const LoadingComponent = loadingComponent;
59
- return (
60
- <>
61
- {isLoading
62
- ? (
63
- <LoadingComponent configurationName={configurationName} />
64
- )
65
- : (
66
- <>{children}</>
67
- )}
68
- </>
69
- );
67
+ });
68
+ }
69
+ return () => {
70
+ isMounted = false;
71
+ };
72
+ }, [configurationName]);
73
+ const LoadingComponent = loadingComponent;
74
+ return (
75
+ <>{isLoading ? <LoadingComponent configurationName={configurationName} /> : <>{children}</>}</>
76
+ );
70
77
  };
71
78
 
72
79
  const Switch = ({ isLoading, loadingComponent, children, configurationName }) => {
73
- const LoadingComponent = loadingComponent;
74
- if (isLoading) {
75
- return <LoadingComponent configurationName={configurationName}>{children}</LoadingComponent>;
76
- }
77
- return <>{children}</>;
80
+ const LoadingComponent = loadingComponent;
81
+ if (isLoading) {
82
+ return <LoadingComponent configurationName={configurationName}>{children}</LoadingComponent>;
83
+ }
84
+ return <>{children}</>;
78
85
  };
79
86
 
80
87
  export const OidcProvider: FC<PropsWithChildren<OidcProviderProps>> = ({
81
- children,
82
- configuration,
83
- configurationName = 'default',
84
- callbackSuccessComponent = CallBackSuccess,
85
- authenticatingComponent = Authenticating,
86
- loadingComponent = Loading,
87
- serviceWorkerNotSupportedComponent = ServiceWorkerNotSupported,
88
- authenticatingErrorComponent = AuthenticatingError,
89
- sessionLostComponent = SessionLost,
90
- onSessionLost = null,
91
- onLogoutFromAnotherTab = null,
92
- onLogoutFromSameTab = null,
93
- withCustomHistory = null,
94
- onEvent = null,
95
- getFetch = null,
96
- location= null,
88
+ children,
89
+ configuration,
90
+ configurationName = 'default',
91
+ callbackSuccessComponent = CallBackSuccess,
92
+ authenticatingComponent = Authenticating,
93
+ loadingComponent = Loading,
94
+ serviceWorkerNotSupportedComponent = ServiceWorkerNotSupported,
95
+ authenticatingErrorComponent = AuthenticatingError,
96
+ sessionLostComponent = SessionLost,
97
+ onSessionLost = null,
98
+ onLogoutFromAnotherTab = null,
99
+ onLogoutFromSameTab = null,
100
+ withCustomHistory = null,
101
+ onEvent = null,
102
+ getFetch = null,
103
+ location = null,
97
104
  }) => {
98
- const getOidc = (configurationName = 'default') => {
99
- return OidcClient.getOrCreate(getFetch ?? getFetchDefault, location ?? new OidcLocation())(configuration, configurationName);
100
- };
101
- // eslint-disable-next-line @typescript-eslint/naming-convention
102
- const [loading, setLoading] = useState(true);
103
- const [event, setEvent] = useState(defaultEventState);
104
- const [currentConfigurationName, setConfigurationName] = useState('default');
105
+ const getOidc = (configurationName = 'default') => {
106
+ return OidcClient.getOrCreate(getFetch ?? getFetchDefault, location ?? new OidcLocation())(
107
+ configuration,
108
+ configurationName,
109
+ );
110
+ };
111
+
112
+ const [loading, setLoading] = useState(true);
113
+ const [event, setEvent] = useState(defaultEventState);
114
+ const [currentConfigurationName, setConfigurationName] = useState('default');
105
115
 
106
- useEffect(() => {
107
- const oidc = getOidc(configurationName);
108
- const newSubscriptionId = oidc.subscribeEvents((name, data) => {
109
- if (onEvent) {
110
- onEvent(configurationName, name, data);
111
- }
112
- });
113
- return () => {
114
- const previousOidc = getOidc(configurationName);
115
- previousOidc.removeEventSubscription(newSubscriptionId);
116
- };
117
- }, [configurationName, onEvent]);
116
+ useEffect(() => {
117
+ const oidc = getOidc(configurationName);
118
+ const newSubscriptionId = oidc.subscribeEvents((name, data) => {
119
+ if (onEvent) {
120
+ onEvent(configurationName, name, data);
121
+ }
122
+ });
123
+ return () => {
124
+ const previousOidc = getOidc(configurationName);
125
+ previousOidc.removeEventSubscription(newSubscriptionId);
126
+ };
127
+ }, [configurationName, onEvent]);
118
128
 
119
- useEffect(() => {
120
- const oidc = getOidc(configurationName);
121
- const newSubscriptionId = oidc.subscribeEvents((name, data) => {
122
- if (name === OidcClient.eventNames.refreshTokensAsync_error || name === OidcClient.eventNames.syncTokensAsync_error) {
123
- if (onSessionLost != null) {
124
- onSessionLost();
125
- return;
126
- }
127
- setEvent({ name, data });
128
- } else if (name === OidcClient.eventNames.logout_from_another_tab) {
129
- if (onLogoutFromAnotherTab != null) {
130
- onLogoutFromAnotherTab();
131
- return;
132
- }
133
- setEvent({ name, data });
134
- } else if (name === OidcClient.eventNames.logout_from_same_tab) {
135
- if (onLogoutFromSameTab != null) {
136
- onLogoutFromSameTab();
137
- }
138
- // setEvent({name, data});
139
- } else if (name === OidcClient.eventNames.loginAsync_begin ||
140
- name === OidcClient.eventNames.loginCallbackAsync_end ||
141
- name === OidcClient.eventNames.loginAsync_error ||
142
- name === OidcClient.eventNames.loginCallbackAsync_error
143
- ) {
144
- setEvent({ name, data });
145
- } else if (name === OidcClient.eventNames.service_worker_not_supported_by_browser && configuration.service_worker_only === true) {
146
- setEvent({ name, data });
147
- }
148
- });
129
+ useEffect(() => {
130
+ const oidc = getOidc(configurationName);
131
+ const newSubscriptionId = oidc.subscribeEvents((name, data) => {
132
+ if (
133
+ name === OidcClient.eventNames.refreshTokensAsync_error ||
134
+ name === OidcClient.eventNames.syncTokensAsync_error
135
+ ) {
136
+ if (onSessionLost != null) {
137
+ onSessionLost();
138
+ return;
139
+ }
140
+ setEvent({ name, data });
141
+ } else if (name === OidcClient.eventNames.logout_from_another_tab) {
142
+ if (onLogoutFromAnotherTab != null) {
143
+ onLogoutFromAnotherTab();
144
+ return;
145
+ }
146
+ setEvent({ name, data });
147
+ } else if (name === OidcClient.eventNames.logout_from_same_tab) {
148
+ if (onLogoutFromSameTab != null) {
149
+ onLogoutFromSameTab();
150
+ }
151
+ // setEvent({name, data});
152
+ } else if (
153
+ name === OidcClient.eventNames.loginAsync_begin ||
154
+ name === OidcClient.eventNames.loginCallbackAsync_end ||
155
+ name === OidcClient.eventNames.loginAsync_error ||
156
+ name === OidcClient.eventNames.loginCallbackAsync_error
157
+ ) {
158
+ setEvent({ name, data });
159
+ } else if (
160
+ name === OidcClient.eventNames.service_worker_not_supported_by_browser &&
161
+ configuration.service_worker_only === true
162
+ ) {
163
+ setEvent({ name, data });
164
+ }
165
+ });
149
166
 
150
- setConfigurationName(configurationName);
151
- setLoading(false);
152
- return () => {
153
- const previousOidc = getOidc(configurationName);
154
- previousOidc.removeEventSubscription(newSubscriptionId);
155
- setEvent(defaultEventState);
156
- };
157
- // eslint-disable-next-line react-hooks/exhaustive-deps
158
- }, [configuration, configurationName]);
167
+ setConfigurationName(configurationName);
168
+ setLoading(false);
169
+ return () => {
170
+ const previousOidc = getOidc(configurationName);
171
+ previousOidc.removeEventSubscription(newSubscriptionId);
172
+ setEvent(defaultEventState);
173
+ };
174
+ }, [configuration, configurationName]);
159
175
 
160
- const SessionLostComponent = sessionLostComponent;
161
- const AuthenticatingComponent = authenticatingComponent;
162
- const LoadingComponent = loadingComponent;
163
- const ServiceWorkerNotSupportedComponent = serviceWorkerNotSupportedComponent;
164
- const AuthenticatingErrorComponent = authenticatingErrorComponent;
176
+ const SessionLostComponent = sessionLostComponent;
177
+ const AuthenticatingComponent = authenticatingComponent;
178
+ const LoadingComponent = loadingComponent;
179
+ const ServiceWorkerNotSupportedComponent = serviceWorkerNotSupportedComponent;
180
+ const AuthenticatingErrorComponent = authenticatingErrorComponent;
165
181
 
166
- const isLoading = (loading || (currentConfigurationName !== configurationName));
167
- const oidc = getOidc(configurationName);
168
- const eventName = event.name;
169
- switch (eventName) {
170
- case OidcClient.eventNames.service_worker_not_supported_by_browser:
171
- return (<Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
172
- <ServiceWorkerNotSupportedComponent configurationName={configurationName} />
173
- </Switch>);
174
- case OidcClient.eventNames.loginAsync_begin:
175
- return (<Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
176
- <AuthenticatingComponent configurationName={configurationName} />
177
- </Switch>);
178
- case OidcClient.eventNames.loginAsync_error:
179
- case OidcClient.eventNames.loginCallbackAsync_error:
180
- return (<Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
181
- <AuthenticatingErrorComponent configurationName={configurationName} />
182
- </Switch>);
183
- case OidcClient.eventNames.refreshTokensAsync_error:
184
- case OidcClient.eventNames.syncTokensAsync_error:
185
- case OidcClient.eventNames.logout_from_another_tab:
186
- return (<Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
187
- <SessionLostComponent configurationName={configurationName} />
188
- </Switch>);
189
- default:
190
- return (
191
- <Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
192
- <OidcRoutes redirect_uri={oidc.configuration.redirect_uri}
193
- silent_redirect_uri={oidc.configuration.silent_redirect_uri}
194
- silent_login_uri={oidc.configuration.silent_login_uri}
195
- callbackSuccessComponent={callbackSuccessComponent}
196
- callbackErrorComponent={authenticatingErrorComponent}
197
- authenticatingComponent={authenticatingComponent}
198
- configurationName={configurationName}
199
- withCustomHistory={withCustomHistory}
200
- location={location ?? new OidcLocation()}>
201
- <OidcSession loadingComponent={LoadingComponent} configurationName={configurationName}>
202
- {children}
203
- </OidcSession>
204
- </OidcRoutes>
205
- </Switch>
206
- );
207
- }
182
+ const isLoading = loading || currentConfigurationName !== configurationName;
183
+ const oidc = getOidc(configurationName);
184
+ const eventName = event.name;
185
+ switch (eventName) {
186
+ case OidcClient.eventNames.service_worker_not_supported_by_browser:
187
+ return (
188
+ <Switch
189
+ loadingComponent={LoadingComponent}
190
+ isLoading={isLoading}
191
+ configurationName={configurationName}
192
+ >
193
+ <ServiceWorkerNotSupportedComponent configurationName={configurationName} />
194
+ </Switch>
195
+ );
196
+ case OidcClient.eventNames.loginAsync_begin:
197
+ return (
198
+ <Switch
199
+ loadingComponent={LoadingComponent}
200
+ isLoading={isLoading}
201
+ configurationName={configurationName}
202
+ >
203
+ <AuthenticatingComponent configurationName={configurationName} />
204
+ </Switch>
205
+ );
206
+ case OidcClient.eventNames.loginAsync_error:
207
+ case OidcClient.eventNames.loginCallbackAsync_error:
208
+ return (
209
+ <Switch
210
+ loadingComponent={LoadingComponent}
211
+ isLoading={isLoading}
212
+ configurationName={configurationName}
213
+ >
214
+ <AuthenticatingErrorComponent configurationName={configurationName} />
215
+ </Switch>
216
+ );
217
+ case OidcClient.eventNames.refreshTokensAsync_error:
218
+ case OidcClient.eventNames.syncTokensAsync_error:
219
+ case OidcClient.eventNames.logout_from_another_tab:
220
+ return (
221
+ <Switch
222
+ loadingComponent={LoadingComponent}
223
+ isLoading={isLoading}
224
+ configurationName={configurationName}
225
+ >
226
+ <SessionLostComponent configurationName={configurationName} />
227
+ </Switch>
228
+ );
229
+ default:
230
+ return (
231
+ <Switch
232
+ loadingComponent={LoadingComponent}
233
+ isLoading={isLoading}
234
+ configurationName={configurationName}
235
+ >
236
+ <OidcRoutes
237
+ redirect_uri={oidc.configuration.redirect_uri}
238
+ silent_redirect_uri={oidc.configuration.silent_redirect_uri}
239
+ silent_login_uri={oidc.configuration.silent_login_uri}
240
+ callbackSuccessComponent={callbackSuccessComponent}
241
+ callbackErrorComponent={authenticatingErrorComponent}
242
+ authenticatingComponent={authenticatingComponent}
243
+ configurationName={configurationName}
244
+ withCustomHistory={withCustomHistory}
245
+ location={location ?? new OidcLocation()}
246
+ >
247
+ <OidcSession loadingComponent={LoadingComponent} configurationName={configurationName}>
248
+ {children}
249
+ </OidcSession>
250
+ </OidcRoutes>
251
+ </Switch>
252
+ );
253
+ }
208
254
  };
209
255
 
210
256
  export default OidcProvider;
@@ -1,32 +1,43 @@
1
- import { StringMap, OidcClient } from '@axa-fr/oidc-client';
1
+ import { OidcClient, StringMap } from '@axa-fr/oidc-client';
2
2
  import { FC, PropsWithChildren, useEffect } from 'react';
3
3
 
4
4
  export type OidcSecureProps = {
5
- callbackPath?:string;
6
- extras?:StringMap;
7
- configurationName?: string;
5
+ callbackPath?: string;
6
+ extras?: StringMap;
7
+ configurationName?: string;
8
8
  };
9
9
 
10
- export const OidcSecure: FC<PropsWithChildren<OidcSecureProps>> = ({ children, callbackPath = null, extras = null, configurationName = 'default' }) => {
11
- const getOidc = OidcClient.get;
12
- const oidc = getOidc(configurationName);
13
- useEffect(() => {
14
- if (!oidc.tokens) {
15
- oidc.loginAsync(callbackPath, extras);
16
- }
17
- }, [configurationName, callbackPath, extras]);
18
-
19
- if (!oidc.tokens) {
20
- return null;
21
- }
22
- return <>{children}</>;
23
- };
24
-
25
- export const withOidcSecure = (
26
- WrappedComponent: FC<PropsWithChildren<OidcSecureProps>>,
10
+ export const OidcSecure: FC<PropsWithChildren<OidcSecureProps>> = ({
11
+ children,
27
12
  callbackPath = null,
28
13
  extras = null,
29
14
  configurationName = 'default',
30
- ) => (props) => {
31
- return <OidcSecure callbackPath={callbackPath} extras={extras} configurationName={configurationName}><WrappedComponent {...props} /></OidcSecure>;
15
+ }) => {
16
+ const getOidc = OidcClient.get;
17
+ const oidc = getOidc(configurationName);
18
+ useEffect(() => {
19
+ if (!oidc.tokens) {
20
+ oidc.loginAsync(callbackPath, extras);
21
+ }
22
+ }, [configurationName, callbackPath, extras]);
23
+
24
+ if (!oidc.tokens) {
25
+ return null;
26
+ }
27
+ return <>{children}</>;
32
28
  };
29
+
30
+ export const withOidcSecure =
31
+ (
32
+ WrappedComponent: FC<PropsWithChildren<OidcSecureProps>>,
33
+ callbackPath = null,
34
+ extras = null,
35
+ configurationName = 'default',
36
+ ) =>
37
+ props => {
38
+ return (
39
+ <OidcSecure callbackPath={callbackPath} extras={extras} configurationName={configurationName}>
40
+ <WrappedComponent {...props} />
41
+ </OidcSecure>
42
+ );
43
+ };