@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.
- package/README.md +87 -114
- package/bin/copy-service-worker-files.mjs +24 -17
- package/dist/FetchToken.d.ts.map +1 -1
- package/dist/OidcProvider.d.ts +1 -1
- package/dist/OidcProvider.d.ts.map +1 -1
- package/dist/OidcSecure.d.ts.map +1 -1
- package/dist/OidcTrustedDomains.js +13 -10
- package/dist/ReactOidc.d.ts.map +1 -1
- package/dist/User.d.ts.map +1 -1
- package/dist/core/default-component/Authenticating.component.d.ts.map +1 -1
- package/dist/core/default-component/Callback.component.d.ts.map +1 -1
- package/dist/core/default-component/Loading.component.d.ts.map +1 -1
- package/dist/core/default-component/ServiceWorkerNotSupported.component.d.ts.map +1 -1
- package/dist/core/default-component/SilentCallback.component.d.ts +5 -2
- package/dist/core/default-component/SilentCallback.component.d.ts.map +1 -1
- package/dist/core/routes/OidcRoutes.d.ts.map +1 -1
- package/dist/core/routes/withRouter.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +165 -72
- package/dist/index.umd.cjs +1 -1
- package/package.json +3 -3
- package/src/FetchToken.tsx +36 -13
- package/src/OidcProvider.tsx +222 -176
- package/src/OidcSecure.tsx +34 -23
- package/src/ReactOidc.tsx +181 -142
- package/src/User.ts +60 -50
- package/src/core/default-component/Authenticating.component.tsx +1 -1
- package/src/core/default-component/Callback.component.tsx +18 -11
- package/src/core/default-component/Loading.component.tsx +1 -5
- package/src/core/default-component/ServiceWorkerNotSupported.component.tsx +5 -2
- package/src/core/default-component/SessionLost.component.tsx +1 -1
- package/src/core/default-component/SilentCallback.component.tsx +17 -11
- package/src/core/default-component/SilentLogin.component.tsx +18 -18
- package/src/core/routes/OidcRoutes.spec.tsx +2 -2
- package/src/core/routes/OidcRoutes.tsx +12 -5
- package/src/core/routes/withRouter.spec.tsx +5 -6
- package/src/core/routes/withRouter.tsx +21 -19
- package/src/index.ts +8 -3
package/src/OidcProvider.tsx
CHANGED
|
@@ -1,210 +1,256 @@
|
|
|
1
|
-
import {
|
|
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 {
|
|
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
|
-
|
|
23
|
+
(name?: string): OidcClient;
|
|
12
24
|
};
|
|
13
25
|
|
|
14
26
|
const defaultEventState = { name: '', data: null };
|
|
15
27
|
|
|
16
28
|
export type OidcProviderProps = {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
37
|
-
|
|
48
|
+
configurationName: string;
|
|
49
|
+
loadingComponent: PropsWithChildren<any>;
|
|
38
50
|
};
|
|
39
51
|
|
|
40
|
-
const OidcSession: FC<PropsWithChildren<OidcSessionProps>> = ({
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
176
|
+
const SessionLostComponent = sessionLostComponent;
|
|
177
|
+
const AuthenticatingComponent = authenticatingComponent;
|
|
178
|
+
const LoadingComponent = loadingComponent;
|
|
179
|
+
const ServiceWorkerNotSupportedComponent = serviceWorkerNotSupportedComponent;
|
|
180
|
+
const AuthenticatingErrorComponent = authenticatingErrorComponent;
|
|
165
181
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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;
|
package/src/OidcSecure.tsx
CHANGED
|
@@ -1,32 +1,43 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
callbackPath?: string;
|
|
6
|
+
extras?: StringMap;
|
|
7
|
+
configurationName?: string;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
-
export const OidcSecure: FC<PropsWithChildren<OidcSecureProps>> = ({
|
|
11
|
-
|
|
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
|
-
) =>
|
|
31
|
-
|
|
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
|
+
};
|