@axa-fr/react-oidc 6.9.7 → 6.10.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/README.md +102 -102
- package/dist/FetchToken.d.ts.map +1 -1
- package/dist/FetchToken.js +2 -2
- package/dist/FetchToken.js.map +1 -1
- package/dist/OidcProvider.d.ts +7 -6
- package/dist/OidcProvider.d.ts.map +1 -1
- package/dist/OidcProvider.js +30 -26
- package/dist/OidcProvider.js.map +1 -1
- package/dist/OidcSecure.d.ts +2 -2
- package/dist/OidcSecure.d.ts.map +1 -1
- package/dist/OidcSecure.js +2 -4
- package/dist/OidcSecure.js.map +1 -1
- package/dist/OidcServiceWorker.js +150 -144
- package/dist/OidcTrustedDomains.js +9 -10
- package/dist/ReactOidc.d.ts +1 -1
- package/dist/ReactOidc.d.ts.map +1 -1
- package/dist/ReactOidc.js +22 -16
- package/dist/ReactOidc.js.map +1 -1
- package/dist/User.d.ts.map +1 -1
- package/dist/User.js +1 -1
- package/dist/User.js.map +1 -1
- package/dist/core/default-component/AuthenticateError.component.d.ts +1 -1
- package/dist/core/default-component/AuthenticateError.component.d.ts.map +1 -1
- package/dist/core/default-component/Authenticating.component.d.ts +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/Callback.component.js +5 -5
- package/dist/core/default-component/Callback.component.js.map +1 -1
- package/dist/core/default-component/Loading.component.d.ts +1 -1
- package/dist/core/default-component/Loading.component.d.ts.map +1 -1
- package/dist/core/default-component/ServiceWorkerNotSupported.component.d.ts +1 -1
- package/dist/core/default-component/ServiceWorkerNotSupported.component.d.ts.map +1 -1
- package/dist/core/default-component/SessionLost.component.d.ts.map +1 -1
- package/dist/core/default-component/SilentCallback.component.d.ts.map +1 -1
- package/dist/core/default-component/SilentCallback.component.js +1 -0
- package/dist/core/default-component/SilentCallback.component.js.map +1 -1
- package/dist/core/default-component/SilentLogin.component.d.ts.map +1 -1
- package/dist/core/default-component/SilentLogin.component.js +5 -7
- package/dist/core/default-component/SilentLogin.component.js.map +1 -1
- package/dist/core/default-component/index.d.ts +2 -2
- package/dist/core/default-component/index.d.ts.map +1 -1
- package/dist/core/default-component/index.js +5 -5
- package/dist/core/default-component/index.js.map +1 -1
- package/dist/core/routes/OidcRoutes.d.ts +1 -1
- package/dist/core/routes/OidcRoutes.d.ts.map +1 -1
- package/dist/core/routes/OidcRoutes.js +1 -1
- package/dist/core/routes/OidcRoutes.js.map +1 -1
- package/dist/core/routes/withRouter.d.ts.map +1 -1
- package/dist/core/routes/withRouter.js.map +1 -1
- package/dist/index.d.ts +7 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -10
- package/dist/index.js.map +1 -1
- package/dist/vanilla/checkSessionIFrame.d.ts.map +1 -1
- package/dist/vanilla/checkSessionIFrame.js +15 -15
- package/dist/vanilla/checkSessionIFrame.js.map +1 -1
- package/dist/vanilla/initSession.d.ts.map +1 -1
- package/dist/vanilla/initSession.js +1 -1
- package/dist/vanilla/initSession.js.map +1 -1
- package/dist/vanilla/initWorker.d.ts +1 -1
- package/dist/vanilla/initWorker.d.ts.map +1 -1
- package/dist/vanilla/initWorker.js +22 -20
- package/dist/vanilla/initWorker.js.map +1 -1
- package/dist/vanilla/memoryStorageBackend.d.ts +5 -4
- package/dist/vanilla/memoryStorageBackend.d.ts.map +1 -1
- package/dist/vanilla/memoryStorageBackend.js.map +1 -1
- package/dist/vanilla/noHashQueryStringUtils.d.ts +3 -3
- package/dist/vanilla/noHashQueryStringUtils.d.ts.map +1 -1
- package/dist/vanilla/noHashQueryStringUtils.js +4 -4
- package/dist/vanilla/noHashQueryStringUtils.js.map +1 -1
- package/dist/vanilla/oidc.d.ts +6 -6
- package/dist/vanilla/oidc.d.ts.map +1 -1
- package/dist/vanilla/oidc.js +215 -216
- package/dist/vanilla/oidc.js.map +1 -1
- package/dist/vanilla/parseTokens.d.ts +2 -2
- package/dist/vanilla/parseTokens.d.ts.map +1 -1
- package/dist/vanilla/parseTokens.js +8 -8
- package/dist/vanilla/parseTokens.js.map +1 -1
- package/dist/vanilla/route-utils.d.ts.map +1 -1
- package/dist/vanilla/route-utils.js +10 -7
- package/dist/vanilla/route-utils.js.map +1 -1
- package/dist/vanilla/timer.d.ts.map +1 -1
- package/dist/vanilla/timer.js +8 -8
- package/dist/vanilla/timer.js.map +1 -1
- package/dist/vanilla/vanillaOidc.d.ts +6 -4
- package/dist/vanilla/vanillaOidc.d.ts.map +1 -1
- package/dist/vanilla/vanillaOidc.js +4 -5
- package/dist/vanilla/vanillaOidc.js.map +1 -1
- package/package.json +15 -6
- package/src/oidc/FetchToken.tsx +10 -11
- package/src/oidc/OidcProvider.tsx +82 -83
- package/src/oidc/OidcSecure.tsx +16 -18
- package/src/oidc/ReactOidc.tsx +74 -63
- package/src/oidc/User.ts +14 -13
- package/src/oidc/core/default-component/AuthenticateError.component.tsx +1 -1
- package/src/oidc/core/default-component/Authenticating.component.tsx +1 -1
- package/src/oidc/core/default-component/Callback.component.tsx +18 -18
- package/src/oidc/core/default-component/Loading.component.tsx +1 -1
- package/src/oidc/core/default-component/ServiceWorkerNotSupported.component.tsx +1 -1
- package/src/oidc/core/default-component/SessionLost.component.tsx +1 -1
- package/src/oidc/core/default-component/SilentCallback.component.tsx +7 -6
- package/src/oidc/core/default-component/SilentLogin.component.tsx +16 -18
- package/src/oidc/core/default-component/index.ts +2 -2
- package/src/oidc/core/routes/OidcRoutes.tsx +16 -15
- package/src/oidc/core/routes/withRouter.tsx +2 -4
- package/src/oidc/index.ts +7 -6
- package/src/oidc/vanilla/OidcServiceWorker.js +150 -144
- package/src/oidc/vanilla/OidcTrustedDomains.js +9 -10
- package/src/oidc/vanilla/checkSessionIFrame.ts +24 -23
- package/src/oidc/vanilla/index.ts +2 -1
- package/src/oidc/vanilla/initSession.ts +36 -37
- package/src/oidc/vanilla/initWorker.ts +82 -83
- package/src/oidc/vanilla/memoryStorageBackend.ts +13 -6
- package/src/oidc/vanilla/noHashQueryStringUtils.ts +13 -13
- package/src/oidc/vanilla/oidc.ts +460 -467
- package/src/oidc/vanilla/parseTokens.ts +73 -79
- package/src/oidc/vanilla/route-utils.ts +18 -18
- package/src/oidc/vanilla/timer.ts +14 -16
- package/src/oidc/vanilla/vanillaOidc.ts +35 -19
- package/src/override/AuthenticateError.component.tsx +4 -3
- package/src/override/Authenticating.component.tsx +4 -3
- package/src/override/Callback.component.tsx +4 -3
- package/src/override/Loading.component.tsx +4 -6
- package/src/override/ServiceWorkerNotSupported.component.tsx +5 -5
- package/src/override/SessionLost.component.tsx +8 -7
- package/src/override/style.ts +12 -10
- package/dist/core/routes/index.d.ts +0 -3
- package/dist/core/routes/index.d.ts.map +0 -1
- package/dist/core/routes/index.js +0 -9
- package/dist/core/routes/index.js.map +0 -1
- package/dist/vanilla/index.d.ts +0 -2
- package/dist/vanilla/index.d.ts.map +0 -1
- package/dist/vanilla/index.js +0 -6
- package/dist/vanilla/index.js.map +0 -1
- package/src/App.css +0 -38
- package/src/App.specold.tsx +0 -46
- package/src/App.tsx +0 -103
- package/src/FetchUser.tsx +0 -53
- package/src/Home.tsx +0 -23
- package/src/MultiAuth.tsx +0 -129
- package/src/Profile.tsx +0 -81
- package/src/configurations.ts +0 -73
- package/src/index.css +0 -13
- package/src/index.tsx +0 -9
- package/src/logo.svg +0 -7
- package/src/setupTests.js +0 -5
package/src/oidc/FetchToken.tsx
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {VanillaOidc} from './vanilla/vanillaOidc'
|
|
1
|
+
import { VanillaOidc } from './vanilla/vanillaOidc';
|
|
3
2
|
|
|
4
3
|
export type Fetch = typeof window.fetch;
|
|
5
4
|
|
|
@@ -7,12 +6,12 @@ export interface ComponentWithOidcFetchProps {
|
|
|
7
6
|
fetch?: Fetch;
|
|
8
7
|
}
|
|
9
8
|
|
|
10
|
-
const defaultConfigurationName =
|
|
9
|
+
const defaultConfigurationName = 'default';
|
|
11
10
|
|
|
12
11
|
const fetchWithToken = (fetch: Fetch, getOidcWithConfigurationName: () => VanillaOidc | null) => async (...params: Parameters<Fetch>) => {
|
|
13
12
|
const [url, options, ...rest] = params;
|
|
14
|
-
const optionTmp = options ? { ...options} : { method:
|
|
15
|
-
|
|
13
|
+
const optionTmp = options ? { ...options } : { method: 'GET' };
|
|
14
|
+
|
|
16
15
|
let headers = new Headers();
|
|
17
16
|
if (optionTmp.headers) {
|
|
18
17
|
headers = !(optionTmp.headers instanceof Headers)
|
|
@@ -20,11 +19,11 @@ const fetchWithToken = (fetch: Fetch, getOidcWithConfigurationName: () => Vanill
|
|
|
20
19
|
: optionTmp.headers;
|
|
21
20
|
}
|
|
22
21
|
const oidc = getOidcWithConfigurationName();
|
|
23
|
-
|
|
22
|
+
|
|
24
23
|
// @ts-ignore
|
|
25
24
|
const getValidToken = await oidc.getValidTokenAsync();
|
|
26
25
|
const accessToken = getValidToken.tokens.accessToken;
|
|
27
|
-
|
|
26
|
+
|
|
28
27
|
if (!headers.has('Accept')) {
|
|
29
28
|
headers.set('Accept', 'application/json');
|
|
30
29
|
}
|
|
@@ -39,16 +38,16 @@ const fetchWithToken = (fetch: Fetch, getOidcWithConfigurationName: () => Vanill
|
|
|
39
38
|
};
|
|
40
39
|
|
|
41
40
|
export const withOidcFetch = (fetch: Fetch = null, configurationName = defaultConfigurationName) => (
|
|
42
|
-
WrappedComponent
|
|
41
|
+
WrappedComponent,
|
|
43
42
|
) => (props: ComponentWithOidcFetchProps) => {
|
|
44
|
-
const {fetch:newFetch} = useOidcFetch(fetch || props.fetch, configurationName)
|
|
43
|
+
const { fetch: newFetch } = useOidcFetch(fetch || props.fetch, configurationName);
|
|
45
44
|
return <WrappedComponent {...props} fetch={newFetch} />;
|
|
46
45
|
};
|
|
47
46
|
|
|
48
|
-
export const useOidcFetch =(fetch: Fetch = null, configurationName = defaultConfigurationName) =>{
|
|
47
|
+
export const useOidcFetch = (fetch: Fetch = null, configurationName = defaultConfigurationName) => {
|
|
49
48
|
const previousFetch = fetch || window.fetch;
|
|
50
49
|
const getOidc = VanillaOidc.get;
|
|
51
50
|
const getOidcWithConfigurationName = () => getOidc(configurationName);
|
|
52
51
|
const newFetch = fetchWithToken(previousFetch, getOidcWithConfigurationName);
|
|
53
52
|
return { fetch: newFetch };
|
|
54
|
-
}
|
|
53
|
+
};
|
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import
|
|
1
|
+
import { ComponentType, FC, PropsWithChildren, useEffect, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import AuthenticatingError from './core/default-component/AuthenticateError.component';
|
|
4
|
+
import { Authenticating, CallBackSuccess, Loading, SessionLost } from './core/default-component/index';
|
|
5
|
+
import ServiceWorkerNotSupported from './core/default-component/ServiceWorkerNotSupported.component';
|
|
4
6
|
import OidcRoutes from './core/routes/OidcRoutes';
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import { CustomHistory } from "./core/routes/withRouter";
|
|
7
|
+
import { CustomHistory } from './core/routes/withRouter';
|
|
8
|
+
import { OidcConfiguration } from './vanilla/oidc';
|
|
9
|
+
import { VanillaOidc } from './vanilla/vanillaOidc';
|
|
9
10
|
|
|
10
11
|
export type oidcContext = {
|
|
11
|
-
|
|
12
|
+
(name?: string): VanillaOidc;
|
|
12
13
|
};
|
|
13
14
|
|
|
14
|
-
const defaultEventState = {name:
|
|
15
|
+
const defaultEventState = { name: '', data: null };
|
|
15
16
|
|
|
16
17
|
export type OidcProviderProps = {
|
|
17
18
|
callbackSuccessComponent?: ComponentType<any>;
|
|
@@ -23,11 +24,11 @@ export type OidcProviderProps = {
|
|
|
23
24
|
configurationName?: string;
|
|
24
25
|
configuration?: OidcConfiguration;
|
|
25
26
|
children: any;
|
|
26
|
-
onSessionLost?:
|
|
27
|
-
onLogoutFromAnotherTab?:
|
|
28
|
-
onLogoutFromSameTab?:
|
|
29
|
-
withCustomHistory?: () => CustomHistory
|
|
30
|
-
onEvent?:
|
|
27
|
+
onSessionLost?: () => void;
|
|
28
|
+
onLogoutFromAnotherTab?: () => void;
|
|
29
|
+
onLogoutFromSameTab?: () => void;
|
|
30
|
+
withCustomHistory?: () => CustomHistory;
|
|
31
|
+
onEvent?: (configuration: string, name: string, data:any) => void;
|
|
31
32
|
};
|
|
32
33
|
|
|
33
34
|
export type OidcSessionProps = {
|
|
@@ -35,162 +36,160 @@ export type OidcSessionProps = {
|
|
|
35
36
|
loadingComponent: PropsWithChildren<any>;
|
|
36
37
|
};
|
|
37
38
|
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
const getOidc = VanillaOidc.get;
|
|
39
|
+
const OidcSession : FC<PropsWithChildren<OidcSessionProps>> = ({ loadingComponent, children, configurationName }) => {
|
|
40
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
41
|
+
const getOidc = VanillaOidc.get;
|
|
42
42
|
const oidc = getOidc(configurationName);
|
|
43
43
|
useEffect(() => {
|
|
44
44
|
let isMounted = true;
|
|
45
|
-
if(oidc) {
|
|
45
|
+
if (oidc) {
|
|
46
46
|
oidc.tryKeepExistingSessionAsync().then(() => {
|
|
47
47
|
if (isMounted) {
|
|
48
|
-
|
|
48
|
+
setIsLoading(false);
|
|
49
49
|
}
|
|
50
|
-
})
|
|
50
|
+
});
|
|
51
51
|
}
|
|
52
52
|
return () => {
|
|
53
53
|
isMounted = false;
|
|
54
|
-
}
|
|
54
|
+
};
|
|
55
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
55
56
|
}, [configurationName]);
|
|
56
57
|
const LoadingComponent = loadingComponent;
|
|
57
58
|
return (
|
|
58
59
|
<>
|
|
59
|
-
{
|
|
60
|
+
{isLoading
|
|
61
|
+
? (
|
|
60
62
|
<LoadingComponent configurationName={configurationName}/>
|
|
61
|
-
)
|
|
63
|
+
)
|
|
64
|
+
: (
|
|
62
65
|
<>{children}</>
|
|
63
66
|
)}
|
|
64
67
|
</>
|
|
65
68
|
);
|
|
66
|
-
}
|
|
69
|
+
};
|
|
67
70
|
|
|
68
|
-
const Switch = ({isLoading, loadingComponent, children, configurationName}) => {
|
|
71
|
+
const Switch = ({ isLoading, loadingComponent, children, configurationName }) => {
|
|
69
72
|
const LoadingComponent = loadingComponent;
|
|
70
|
-
if(isLoading){
|
|
73
|
+
if (isLoading) {
|
|
71
74
|
return <LoadingComponent configurationName={configurationName}>{children}</LoadingComponent>;
|
|
72
75
|
}
|
|
73
76
|
return <>{children}</>;
|
|
74
|
-
}
|
|
75
|
-
|
|
77
|
+
};
|
|
76
78
|
|
|
77
|
-
export const OidcProvider : FC<PropsWithChildren<OidcProviderProps>>
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
export const OidcProvider : FC<PropsWithChildren<OidcProviderProps>> = ({
|
|
80
|
+
children,
|
|
81
|
+
configuration,
|
|
82
|
+
configurationName = 'default',
|
|
83
|
+
callbackSuccessComponent = CallBackSuccess,
|
|
81
84
|
authenticatingComponent = Authenticating,
|
|
82
85
|
loadingComponent = Loading,
|
|
83
86
|
serviceWorkerNotSupportedComponent = ServiceWorkerNotSupported,
|
|
84
87
|
authenticatingErrorComponent = AuthenticatingError,
|
|
85
|
-
sessionLostComponent=SessionLost,
|
|
86
|
-
onSessionLost=null,
|
|
87
|
-
onLogoutFromAnotherTab=null,
|
|
88
|
-
onLogoutFromSameTab=null,
|
|
89
|
-
withCustomHistory=null,
|
|
90
|
-
onEvent=null,
|
|
88
|
+
sessionLostComponent = SessionLost,
|
|
89
|
+
onSessionLost = null,
|
|
90
|
+
onLogoutFromAnotherTab = null,
|
|
91
|
+
onLogoutFromSameTab = null,
|
|
92
|
+
withCustomHistory = null,
|
|
93
|
+
onEvent = null,
|
|
91
94
|
}) => {
|
|
92
|
-
const getOidc =(configurationName=
|
|
95
|
+
const getOidc = (configurationName = 'default') => {
|
|
93
96
|
return VanillaOidc.getOrCreate(configuration, configurationName);
|
|
94
|
-
}
|
|
97
|
+
};
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
95
99
|
const [loading, setLoading] = useState(true);
|
|
96
100
|
const [event, setEvent] = useState(defaultEventState);
|
|
97
|
-
const [currentConfigurationName, setConfigurationName] = useState(
|
|
101
|
+
const [currentConfigurationName, setConfigurationName] = useState('default');
|
|
98
102
|
|
|
99
103
|
useEffect(() => {
|
|
100
104
|
const oidc = getOidc(configurationName);
|
|
101
|
-
const newSubscriptionId = oidc.
|
|
102
|
-
if(onEvent)
|
|
103
|
-
{
|
|
105
|
+
const newSubscriptionId = oidc.subscribeEvents((name, data) => {
|
|
106
|
+
if (onEvent) {
|
|
104
107
|
onEvent(configurationName, name, data);
|
|
105
108
|
}
|
|
106
109
|
});
|
|
107
110
|
return () => {
|
|
108
111
|
const previousOidc = getOidc(configurationName);
|
|
109
112
|
previousOidc.removeEventSubscription(newSubscriptionId);
|
|
110
|
-
}
|
|
113
|
+
};
|
|
111
114
|
}, [configurationName, onEvent]);
|
|
112
115
|
|
|
113
116
|
useEffect(() => {
|
|
114
117
|
const oidc = getOidc(configurationName);
|
|
115
|
-
const newSubscriptionId = oidc.
|
|
116
|
-
if(name
|
|
117
|
-
if(onSessionLost != null){
|
|
118
|
+
const newSubscriptionId = oidc.subscribeEvents((name, data) => {
|
|
119
|
+
if (name === VanillaOidc.eventNames.refreshTokensAsync_error || name === VanillaOidc.eventNames.syncTokensAsync_error) {
|
|
120
|
+
if (onSessionLost != null) {
|
|
118
121
|
onSessionLost();
|
|
119
122
|
return;
|
|
120
123
|
}
|
|
121
|
-
setEvent({name, data});
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if(onLogoutFromAnotherTab != null){
|
|
124
|
+
setEvent({ name, data });
|
|
125
|
+
} else if (name === VanillaOidc.eventNames.logout_from_another_tab) {
|
|
126
|
+
if (onLogoutFromAnotherTab != null) {
|
|
125
127
|
onLogoutFromAnotherTab();
|
|
126
128
|
return;
|
|
127
129
|
}
|
|
128
|
-
setEvent({name, data});
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if(onLogoutFromSameTab != null){
|
|
130
|
+
setEvent({ name, data });
|
|
131
|
+
} else if (name === VanillaOidc.eventNames.logout_from_same_tab) {
|
|
132
|
+
if (onLogoutFromSameTab != null) {
|
|
132
133
|
onLogoutFromSameTab();
|
|
133
|
-
return;
|
|
134
134
|
}
|
|
135
|
-
//setEvent({name, data});
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|| name == VanillaOidc.eventNames.loginCallbackAsync_error
|
|
135
|
+
// setEvent({name, data});
|
|
136
|
+
} else if (name === VanillaOidc.eventNames.loginAsync_begin ||
|
|
137
|
+
name === VanillaOidc.eventNames.loginCallbackAsync_end ||
|
|
138
|
+
name === VanillaOidc.eventNames.loginAsync_error ||
|
|
139
|
+
name === VanillaOidc.eventNames.loginCallbackAsync_error
|
|
141
140
|
) {
|
|
142
|
-
setEvent({name, data});
|
|
143
|
-
} else if (name
|
|
144
|
-
setEvent({name, data});
|
|
141
|
+
setEvent({ name, data });
|
|
142
|
+
} else if (name === VanillaOidc.eventNames.service_worker_not_supported_by_browser && configuration.service_worker_only === true) {
|
|
143
|
+
setEvent({ name, data });
|
|
145
144
|
}
|
|
146
145
|
});
|
|
147
|
-
|
|
146
|
+
|
|
148
147
|
setConfigurationName(configurationName);
|
|
149
148
|
setLoading(false);
|
|
150
149
|
return () => {
|
|
151
150
|
const previousOidc = getOidc(configurationName);
|
|
152
151
|
previousOidc.removeEventSubscription(newSubscriptionId);
|
|
153
152
|
setEvent(defaultEventState);
|
|
154
|
-
}
|
|
153
|
+
};
|
|
154
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
155
155
|
}, [configuration, configurationName]);
|
|
156
156
|
|
|
157
|
-
|
|
158
157
|
const SessionLostComponent = sessionLostComponent;
|
|
159
158
|
const AuthenticatingComponent = authenticatingComponent;
|
|
160
159
|
const LoadingComponent = loadingComponent;
|
|
161
160
|
const ServiceWorkerNotSupportedComponent = serviceWorkerNotSupportedComponent;
|
|
162
161
|
const AuthenticatingErrorComponent = authenticatingErrorComponent;
|
|
163
162
|
|
|
164
|
-
const isLoading = (loading || (currentConfigurationName
|
|
163
|
+
const isLoading = (loading || (currentConfigurationName !== configurationName));
|
|
165
164
|
const oidc = getOidc(configurationName);
|
|
166
|
-
|
|
167
|
-
switch(eventName){
|
|
165
|
+
const eventName = event.name;
|
|
166
|
+
switch (eventName) {
|
|
168
167
|
case VanillaOidc.eventNames.service_worker_not_supported_by_browser:
|
|
169
|
-
return <Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
|
|
168
|
+
return (<Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
|
|
170
169
|
<ServiceWorkerNotSupportedComponent configurationName={configurationName} />
|
|
171
|
-
</Switch
|
|
170
|
+
</Switch>);
|
|
172
171
|
case VanillaOidc.eventNames.loginAsync_begin:
|
|
173
|
-
return
|
|
172
|
+
return (<Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
|
|
174
173
|
<AuthenticatingComponent configurationName={configurationName} />
|
|
175
|
-
</Switch
|
|
174
|
+
</Switch>);
|
|
176
175
|
case VanillaOidc.eventNames.loginAsync_error:
|
|
177
176
|
case VanillaOidc.eventNames.loginCallbackAsync_error:
|
|
178
|
-
return <Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
|
|
177
|
+
return (<Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
|
|
179
178
|
<AuthenticatingErrorComponent configurationName={configurationName} />
|
|
180
|
-
</Switch
|
|
179
|
+
</Switch>);
|
|
181
180
|
case VanillaOidc.eventNames.refreshTokensAsync_error:
|
|
182
181
|
case VanillaOidc.eventNames.syncTokensAsync_error:
|
|
183
182
|
case VanillaOidc.eventNames.logout_from_another_tab:
|
|
184
|
-
return <Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
|
|
185
|
-
<SessionLostComponent configurationName={configurationName} />
|
|
186
|
-
</Switch
|
|
183
|
+
return (<Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
|
|
184
|
+
<SessionLostComponent configurationName={configurationName} />
|
|
185
|
+
</Switch>);
|
|
187
186
|
default:
|
|
188
187
|
return (
|
|
189
188
|
<Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
|
|
190
189
|
<OidcRoutes redirect_uri={oidc.configuration.redirect_uri}
|
|
191
190
|
silent_redirect_uri={oidc.configuration.silent_redirect_uri}
|
|
192
191
|
silent_login_uri={oidc.configuration.silent_login_uri}
|
|
193
|
-
callbackSuccessComponent={callbackSuccessComponent}
|
|
192
|
+
callbackSuccessComponent={callbackSuccessComponent}
|
|
194
193
|
callbackErrorComponent={authenticatingErrorComponent}
|
|
195
194
|
authenticatingComponent={authenticatingComponent}
|
|
196
195
|
configurationName={configurationName}
|
package/src/oidc/OidcSecure.tsx
CHANGED
|
@@ -1,36 +1,34 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { FC, PropsWithChildren, useEffect } from 'react';
|
|
2
2
|
|
|
3
|
-
import {StringMap} from
|
|
4
|
-
import {VanillaOidc} from
|
|
3
|
+
import { StringMap } from './vanilla/oidc';
|
|
4
|
+
import { VanillaOidc } from './vanilla/vanillaOidc';
|
|
5
5
|
|
|
6
6
|
export type OidcSecureProps = {
|
|
7
7
|
callbackPath?:string;
|
|
8
|
-
extras?:StringMap
|
|
8
|
+
extras?:StringMap;
|
|
9
9
|
configurationName?: string;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
export const OidcSecure: FC<PropsWithChildren<OidcSecureProps>> = ({children, callbackPath=null, extras=null, configurationName=
|
|
13
|
-
const getOidc =
|
|
12
|
+
export const OidcSecure: FC<PropsWithChildren<OidcSecureProps>> = ({ children, callbackPath = null, extras = null, configurationName = 'default' }) => {
|
|
13
|
+
const getOidc = VanillaOidc.get;
|
|
14
14
|
const oidc = getOidc(configurationName);
|
|
15
15
|
useEffect(() => {
|
|
16
|
-
if(!oidc.tokens){
|
|
16
|
+
if (!oidc.tokens) {
|
|
17
17
|
oidc.loginAsync(callbackPath, extras);
|
|
18
18
|
}
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
}, [configurationName, callbackPath, extras])
|
|
19
|
+
}, [configurationName, callbackPath, extras]);
|
|
22
20
|
|
|
23
|
-
if(!oidc.tokens){
|
|
21
|
+
if (!oidc.tokens) {
|
|
24
22
|
return null;
|
|
25
23
|
}
|
|
26
|
-
return <>{children}
|
|
27
|
-
}
|
|
24
|
+
return <>{children}</>;
|
|
25
|
+
};
|
|
28
26
|
|
|
29
27
|
export const withOidcSecure = (
|
|
30
|
-
WrappedComponent: FC<PropsWithChildren<OidcSecureProps>>,
|
|
31
|
-
callbackPath=null,
|
|
32
|
-
extras=null,
|
|
33
|
-
configurationName=
|
|
28
|
+
WrappedComponent: FC<PropsWithChildren<OidcSecureProps>>,
|
|
29
|
+
callbackPath = null,
|
|
30
|
+
extras = null,
|
|
31
|
+
configurationName = 'default',
|
|
34
32
|
) => (props) => {
|
|
35
33
|
return <OidcSecure callbackPath={callbackPath} extras={extras} configurationName={configurationName}><WrappedComponent {...props} /></OidcSecure>;
|
|
36
|
-
};
|
|
34
|
+
};
|
package/src/oidc/ReactOidc.tsx
CHANGED
|
@@ -1,29 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
import {StringMap} from "./vanilla/oidc";
|
|
3
|
-
import {VanillaOidc} from "./vanilla/vanillaOidc";
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
4
2
|
|
|
5
|
-
|
|
3
|
+
import { StringMap } from './vanilla/oidc';
|
|
4
|
+
import { VanillaOidc } from './vanilla/vanillaOidc';
|
|
6
5
|
|
|
7
|
-
const
|
|
8
|
-
|
|
6
|
+
const defaultConfigurationName = 'default';
|
|
7
|
+
|
|
8
|
+
type GetOidcFn = {
|
|
9
|
+
(configurationName?: string): any;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const defaultIsAuthenticated = (getOidc: GetOidcFn, configurationName: string) => {
|
|
13
|
+
let isAuthenticated = false;
|
|
9
14
|
const oidc = getOidc(configurationName);
|
|
10
|
-
if(oidc){
|
|
15
|
+
if (oidc) {
|
|
11
16
|
isAuthenticated = getOidc(configurationName).tokens != null;
|
|
12
17
|
}
|
|
13
18
|
return isAuthenticated;
|
|
14
|
-
}
|
|
19
|
+
};
|
|
15
20
|
|
|
16
|
-
export const useOidc =(configurationName=defaultConfigurationName) =>{
|
|
17
|
-
const getOidc =
|
|
21
|
+
export const useOidc = (configurationName = defaultConfigurationName) => {
|
|
22
|
+
const getOidc = VanillaOidc.get;
|
|
18
23
|
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(defaultIsAuthenticated(getOidc, configurationName));
|
|
19
24
|
|
|
20
25
|
useEffect(() => {
|
|
21
26
|
let isMounted = true;
|
|
22
27
|
const oidc = getOidc(configurationName);
|
|
23
28
|
setIsAuthenticated(defaultIsAuthenticated(getOidc, configurationName));
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
30
|
+
const newSubscriptionId = oidc.subscribeEvents((name: string, data: any) => {
|
|
31
|
+
if (name === VanillaOidc.eventNames.logout_from_another_tab || name === VanillaOidc.eventNames.logout_from_same_tab || name === VanillaOidc.eventNames.token_aquired) {
|
|
32
|
+
if (isMounted) {
|
|
27
33
|
setIsAuthenticated(defaultIsAuthenticated(getOidc, configurationName));
|
|
28
34
|
}
|
|
29
35
|
}
|
|
@@ -32,107 +38,111 @@ export const useOidc =(configurationName=defaultConfigurationName) =>{
|
|
|
32
38
|
isMounted = false;
|
|
33
39
|
oidc.removeEventSubscription(newSubscriptionId);
|
|
34
40
|
};
|
|
41
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
35
42
|
}, [configurationName]);
|
|
36
43
|
|
|
37
|
-
const login = (callbackPath:string | undefined = undefined, extras:StringMap=null, silentLoginOnly = false) => {
|
|
44
|
+
const login = (callbackPath:string | undefined = undefined, extras:StringMap = null, silentLoginOnly = false) => {
|
|
38
45
|
return getOidc(configurationName).loginAsync(callbackPath, extras, false, undefined, silentLoginOnly);
|
|
39
46
|
};
|
|
40
|
-
const logout = (callbackPath: string | null | undefined = undefined, extras:StringMap=null) => {
|
|
47
|
+
const logout = (callbackPath: string | null | undefined = undefined, extras:StringMap = null) => {
|
|
41
48
|
return getOidc(configurationName).logoutAsync(callbackPath, extras);
|
|
42
49
|
};
|
|
43
|
-
const renewTokens = (extras:StringMap=null) => {
|
|
50
|
+
const renewTokens = (extras:StringMap = null) => {
|
|
44
51
|
return getOidc(configurationName).renewTokensAsync(extras);
|
|
45
52
|
};
|
|
46
53
|
return { login, logout, renewTokens, isAuthenticated };
|
|
47
|
-
}
|
|
54
|
+
};
|
|
48
55
|
|
|
49
|
-
const accessTokenInitialState = {accessToken:null, accessTokenPayload:null};
|
|
56
|
+
const accessTokenInitialState = { accessToken: null, accessTokenPayload: null };
|
|
50
57
|
|
|
51
|
-
const initTokens = (configurationName) => {
|
|
52
|
-
const getOidc =
|
|
58
|
+
const initTokens = (configurationName: string) => {
|
|
59
|
+
const getOidc = VanillaOidc.get;
|
|
53
60
|
const oidc = getOidc(configurationName);
|
|
54
|
-
if(oidc.tokens) {
|
|
61
|
+
if (oidc.tokens) {
|
|
55
62
|
const tokens = oidc.tokens;
|
|
56
|
-
return {accessToken
|
|
63
|
+
return { accessToken: tokens.accessToken, accessTokenPayload: tokens.accessTokenPayload };
|
|
57
64
|
}
|
|
58
65
|
return accessTokenInitialState;
|
|
59
|
-
}
|
|
66
|
+
};
|
|
60
67
|
|
|
61
68
|
export type OidcAccessToken = {
|
|
62
|
-
accessToken?: any
|
|
63
|
-
accessTokenPayload?: any
|
|
69
|
+
accessToken?: any;
|
|
70
|
+
accessTokenPayload?: any;
|
|
64
71
|
}
|
|
65
72
|
|
|
66
|
-
export const useOidcAccessToken =(configurationName=defaultConfigurationName) =>{
|
|
67
|
-
const getOidc =
|
|
73
|
+
export const useOidcAccessToken = (configurationName = defaultConfigurationName) => {
|
|
74
|
+
const getOidc = VanillaOidc.get;
|
|
68
75
|
const [state, setAccessToken] = useState<OidcAccessToken>(initTokens(configurationName));
|
|
69
76
|
|
|
70
77
|
useEffect(() => {
|
|
71
78
|
let isMounted = true;
|
|
72
79
|
const oidc = getOidc(configurationName);
|
|
73
|
-
if(oidc.tokens) {
|
|
80
|
+
if (oidc.tokens) {
|
|
74
81
|
const tokens = oidc.tokens;
|
|
75
|
-
setAccessToken({accessToken
|
|
82
|
+
setAccessToken({ accessToken: tokens.accessToken, accessTokenPayload: tokens.accessTokenPayload });
|
|
76
83
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
84
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
85
|
+
const newSubscriptionId = oidc.subscribeEvents((name: string, data: any) => {
|
|
86
|
+
if (name === VanillaOidc.eventNames.token_renewed ||
|
|
87
|
+
name === VanillaOidc.eventNames.token_aquired ||
|
|
88
|
+
name === VanillaOidc.eventNames.logout_from_another_tab ||
|
|
89
|
+
name === VanillaOidc.eventNames.logout_from_same_tab ||
|
|
90
|
+
name === VanillaOidc.eventNames.refreshTokensAsync_error ||
|
|
91
|
+
name === VanillaOidc.eventNames.syncTokensAsync_error) {
|
|
92
|
+
if (isMounted) {
|
|
85
93
|
const tokens = oidc.tokens;
|
|
86
|
-
setAccessToken(tokens != null
|
|
94
|
+
setAccessToken(tokens != null ? { accessToken: tokens.accessToken, accessTokenPayload: tokens.accessTokenPayload } : accessTokenInitialState);
|
|
87
95
|
}
|
|
88
96
|
}
|
|
89
97
|
});
|
|
90
|
-
return
|
|
98
|
+
return () => {
|
|
91
99
|
isMounted = false;
|
|
92
100
|
oidc.removeEventSubscription(newSubscriptionId);
|
|
93
101
|
};
|
|
102
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
94
103
|
}, [configurationName]);
|
|
95
104
|
return state;
|
|
96
|
-
}
|
|
105
|
+
};
|
|
97
106
|
|
|
98
|
-
const idTokenInitialState = {idToken:null, idTokenPayload:null};
|
|
107
|
+
const idTokenInitialState = { idToken: null, idTokenPayload: null };
|
|
99
108
|
|
|
100
|
-
const initIdToken= (configurationName) =>{
|
|
101
|
-
const getOidc =
|
|
109
|
+
const initIdToken = (configurationName: string) => {
|
|
110
|
+
const getOidc = VanillaOidc.get;
|
|
102
111
|
const oidc = getOidc(configurationName);
|
|
103
|
-
if(oidc.tokens) {
|
|
112
|
+
if (oidc.tokens) {
|
|
104
113
|
const tokens = oidc.tokens;
|
|
105
|
-
return { idToken: tokens.idToken, idTokenPayload:tokens.idTokenPayload };
|
|
114
|
+
return { idToken: tokens.idToken, idTokenPayload: tokens.idTokenPayload };
|
|
106
115
|
}
|
|
107
|
-
return idTokenInitialState
|
|
108
|
-
}
|
|
116
|
+
return idTokenInitialState;
|
|
117
|
+
};
|
|
109
118
|
|
|
110
119
|
export type OidcIdToken = {
|
|
111
|
-
idToken?: any
|
|
112
|
-
idTokenPayload?: any
|
|
120
|
+
idToken?: any;
|
|
121
|
+
idTokenPayload?: any;
|
|
113
122
|
}
|
|
114
123
|
|
|
115
|
-
export const useOidcIdToken =(configurationName= defaultConfigurationName) =>{
|
|
116
|
-
const getOidc =
|
|
124
|
+
export const useOidcIdToken = (configurationName = defaultConfigurationName) => {
|
|
125
|
+
const getOidc = VanillaOidc.get;
|
|
117
126
|
const [state, setIDToken] = useState<OidcIdToken>(initIdToken(configurationName));
|
|
118
127
|
|
|
119
128
|
useEffect(() => {
|
|
120
129
|
let isMounted = true;
|
|
121
130
|
const oidc = getOidc(configurationName);
|
|
122
|
-
if(oidc.tokens) {
|
|
131
|
+
if (oidc.tokens) {
|
|
123
132
|
const tokens = oidc.tokens;
|
|
124
|
-
setIDToken({idToken: tokens.idToken, idTokenPayload:tokens.idTokenPayload});
|
|
133
|
+
setIDToken({ idToken: tokens.idToken, idTokenPayload: tokens.idTokenPayload });
|
|
125
134
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
135
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
136
|
+
const newSubscriptionId = oidc.subscribeEvents((name: string, data: any) => {
|
|
137
|
+
if (name === VanillaOidc.eventNames.token_renewed ||
|
|
138
|
+
name === VanillaOidc.eventNames.token_aquired ||
|
|
139
|
+
name === VanillaOidc.eventNames.logout_from_another_tab ||
|
|
140
|
+
name === VanillaOidc.eventNames.logout_from_same_tab ||
|
|
141
|
+
name === VanillaOidc.eventNames.refreshTokensAsync_error ||
|
|
142
|
+
name === VanillaOidc.eventNames.syncTokensAsync_error) {
|
|
143
|
+
if (isMounted) {
|
|
134
144
|
const tokens = oidc.tokens;
|
|
135
|
-
setIDToken(tokens != null
|
|
145
|
+
setIDToken(tokens != null ? { idToken: tokens.idToken, idTokenPayload: tokens.idTokenPayload } : idTokenInitialState);
|
|
136
146
|
}
|
|
137
147
|
}
|
|
138
148
|
});
|
|
@@ -140,6 +150,7 @@ export const useOidcIdToken =(configurationName= defaultConfigurationName) =>{
|
|
|
140
150
|
isMounted = false;
|
|
141
151
|
oidc.removeEventSubscription(newSubscriptionId);
|
|
142
152
|
};
|
|
153
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
143
154
|
}, [configurationName]);
|
|
144
155
|
return state;
|
|
145
|
-
}
|
|
156
|
+
};
|