@axa-fr/react-oidc 6.11.4-alpha0 → 6.11.4-alpha2
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/dist/OidcProvider.d.ts +1 -1
- package/dist/OidcProvider.d.ts.map +1 -1
- package/dist/OidcSecure.d.ts +1 -1
- package/dist/OidcSecure.d.ts.map +1 -1
- package/dist/ReactOidc.d.ts +1 -1
- package/dist/ReactOidc.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/vanilla/checkSession.d.ts +5 -0
- package/dist/vanilla/checkSession.d.ts.map +1 -0
- package/dist/vanilla/checkSession.js +68 -0
- package/dist/vanilla/checkSession.js.map +1 -0
- package/dist/vanilla/events.d.ts +29 -0
- package/dist/vanilla/events.d.ts.map +1 -0
- package/dist/vanilla/events.js +32 -0
- package/dist/vanilla/events.js.map +1 -0
- package/dist/vanilla/initWorker.d.ts +1 -1
- package/dist/vanilla/initWorker.d.ts.map +1 -1
- package/dist/vanilla/initWorker.js +2 -2
- package/dist/vanilla/initWorker.js.map +1 -1
- package/dist/vanilla/login.d.ts +4 -0
- package/dist/vanilla/login.d.ts.map +1 -0
- package/dist/vanilla/login.js +125 -0
- package/dist/vanilla/login.js.map +1 -0
- package/dist/vanilla/oidc.d.ts +7 -36
- package/dist/vanilla/oidc.d.ts.map +1 -1
- package/dist/vanilla/oidc.js +53 -349
- package/dist/vanilla/oidc.js.map +1 -1
- package/dist/vanilla/requests.d.ts +2 -0
- package/dist/vanilla/requests.d.ts.map +1 -1
- package/dist/vanilla/requests.js +20 -1
- package/dist/vanilla/requests.js.map +1 -1
- package/dist/vanilla/silentLogin.d.ts +8 -0
- package/dist/vanilla/silentLogin.d.ts.map +1 -0
- package/dist/vanilla/silentLogin.js +95 -0
- package/dist/vanilla/silentLogin.js.map +1 -0
- package/dist/vanilla/types.d.ts +33 -0
- package/dist/vanilla/types.d.ts.map +1 -0
- package/dist/vanilla/types.js +3 -0
- package/dist/vanilla/types.js.map +1 -0
- package/dist/vanilla/user.d.ts +2 -0
- package/dist/vanilla/user.d.ts.map +1 -0
- package/dist/vanilla/user.js +48 -0
- package/dist/vanilla/user.js.map +1 -0
- package/dist/vanilla/vanillaOidc.d.ts +2 -1
- package/dist/vanilla/vanillaOidc.d.ts.map +1 -1
- package/dist/vanilla/vanillaOidc.js.map +1 -1
- package/package.json +1 -1
- package/src/oidc/OidcProvider.tsx +1 -1
- package/src/oidc/OidcSecure.tsx +1 -1
- package/src/oidc/ReactOidc.tsx +1 -1
- package/src/oidc/index.ts +1 -1
- package/src/oidc/vanilla/checkSession.ts +55 -0
- package/src/oidc/vanilla/events.ts +29 -0
- package/src/oidc/vanilla/index.ts +1 -1
- package/src/oidc/vanilla/initWorker.ts +3 -3
- package/src/oidc/vanilla/login.ts +118 -0
- package/src/oidc/vanilla/oidc.ts +23 -372
- package/src/oidc/vanilla/requests.ts +24 -0
- package/src/oidc/vanilla/silentLogin.ts +102 -0
- package/src/oidc/vanilla/types.ts +35 -0
- package/src/oidc/vanilla/user.ts +39 -0
- package/src/oidc/vanilla/vanillaOidc.ts +2 -1
|
@@ -1,5 +1,29 @@
|
|
|
1
|
+
import { getFromCache, setCache } from './cache';
|
|
2
|
+
import { OidcAuthorizationServiceConfiguration } from './oidc';
|
|
1
3
|
import { parseOriginalTokens } from './parseTokens';
|
|
2
4
|
|
|
5
|
+
const oneHourSecond = 60 * 60;
|
|
6
|
+
export const fetchFromIssuer = async (openIdIssuerUrl: string, timeCacheSecond = oneHourSecond, storage = window.sessionStorage):
|
|
7
|
+
Promise<OidcAuthorizationServiceConfiguration> => {
|
|
8
|
+
const fullUrl = `${openIdIssuerUrl}/.well-known/openid-configuration`;
|
|
9
|
+
|
|
10
|
+
const localStorageKey = `oidc.server:${openIdIssuerUrl}`;
|
|
11
|
+
const data = getFromCache(localStorageKey, storage, timeCacheSecond);
|
|
12
|
+
if (data) {
|
|
13
|
+
return new OidcAuthorizationServiceConfiguration(data);
|
|
14
|
+
}
|
|
15
|
+
const response = await fetch(fullUrl);
|
|
16
|
+
|
|
17
|
+
if (response.status !== 200) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const result = await response.json();
|
|
22
|
+
|
|
23
|
+
setCache(localStorageKey, result, storage);
|
|
24
|
+
return new OidcAuthorizationServiceConfiguration(result);
|
|
25
|
+
};
|
|
26
|
+
|
|
3
27
|
const internalFetch = async (url, headers, numberRetry = 0, timeoutMs = 10000) => {
|
|
4
28
|
let response;
|
|
5
29
|
try {
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { eventNames } from './events';
|
|
2
|
+
import { Tokens } from './parseTokens';
|
|
3
|
+
import { OidcConfiguration, StringMap } from './types';
|
|
4
|
+
|
|
5
|
+
type SilentLoginResponse = {
|
|
6
|
+
tokens:Tokens;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
10
|
+
const silentLoginAsync = (configurationName:string, configuration:OidcConfiguration, publishEvent:Function) => (extras:StringMap = null, state:string = null, scope:string = null):Promise<SilentLoginResponse> => {
|
|
11
|
+
if (!configuration.silent_redirect_uri || !configuration.silent_login_uri) {
|
|
12
|
+
return Promise.resolve(null);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
publishEvent(eventNames.silentLoginAsync_begin, {});
|
|
17
|
+
let queries = '';
|
|
18
|
+
|
|
19
|
+
if (state) {
|
|
20
|
+
if (extras == null) {
|
|
21
|
+
extras = {};
|
|
22
|
+
}
|
|
23
|
+
extras.state = state;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (scope) {
|
|
27
|
+
if (extras == null) {
|
|
28
|
+
extras = {};
|
|
29
|
+
}
|
|
30
|
+
extras.scope = scope;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (extras != null) {
|
|
34
|
+
for (const [key, value] of Object.entries(extras)) {
|
|
35
|
+
if (queries === '') {
|
|
36
|
+
queries = `?${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
|
37
|
+
} else {
|
|
38
|
+
queries += `&${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const link = configuration.silent_login_uri + queries;
|
|
43
|
+
const idx = link.indexOf('/', link.indexOf('//') + 2);
|
|
44
|
+
const iFrameOrigin = link.substr(0, idx);
|
|
45
|
+
const iframe = document.createElement('iframe');
|
|
46
|
+
iframe.width = '0px';
|
|
47
|
+
iframe.height = '0px';
|
|
48
|
+
|
|
49
|
+
iframe.id = `${configurationName}_oidc_iframe`;
|
|
50
|
+
iframe.setAttribute('src', link);
|
|
51
|
+
document.body.appendChild(iframe);
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
try {
|
|
54
|
+
let isResolved = false;
|
|
55
|
+
window.onmessage = (e: MessageEvent<any>) => {
|
|
56
|
+
if (e.origin === iFrameOrigin &&
|
|
57
|
+
e.source === iframe.contentWindow
|
|
58
|
+
) {
|
|
59
|
+
const key = `${configurationName}_oidc_tokens:`;
|
|
60
|
+
const key_error = `${configurationName}_oidc_error:`;
|
|
61
|
+
const data = e.data;
|
|
62
|
+
if (data && typeof (data) === 'string') {
|
|
63
|
+
if (!isResolved) {
|
|
64
|
+
if (data.startsWith(key)) {
|
|
65
|
+
const result = JSON.parse(e.data.replace(key, ''));
|
|
66
|
+
publishEvent(eventNames.silentLoginAsync_end, {});
|
|
67
|
+
iframe.remove();
|
|
68
|
+
isResolved = true;
|
|
69
|
+
resolve(result);
|
|
70
|
+
} else if (data.startsWith(key_error)) {
|
|
71
|
+
const result = JSON.parse(e.data.replace(key_error, ''));
|
|
72
|
+
publishEvent(eventNames.silentLoginAsync_error, result);
|
|
73
|
+
iframe.remove();
|
|
74
|
+
isResolved = true;
|
|
75
|
+
reject(new Error('oidc_' + result.error));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
const silentSigninTimeout = configuration.silent_login_timeout;
|
|
82
|
+
setTimeout(() => {
|
|
83
|
+
if (!isResolved) {
|
|
84
|
+
publishEvent(eventNames.silentLoginAsync_error, { reason: 'timeout' });
|
|
85
|
+
iframe.remove();
|
|
86
|
+
isResolved = true;
|
|
87
|
+
reject(new Error('timeout'));
|
|
88
|
+
}
|
|
89
|
+
}, silentSigninTimeout);
|
|
90
|
+
} catch (e) {
|
|
91
|
+
iframe.remove();
|
|
92
|
+
publishEvent(eventNames.silentLoginAsync_error, e);
|
|
93
|
+
reject(e);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
} catch (e) {
|
|
97
|
+
publishEvent(eventNames.silentLoginAsync_error, e);
|
|
98
|
+
throw e;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export default silentLoginAsync;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
|
|
2
|
+
export type OidcConfiguration = {
|
|
3
|
+
client_id: string;
|
|
4
|
+
redirect_uri: string;
|
|
5
|
+
silent_redirect_uri?:string;
|
|
6
|
+
silent_login_uri?:string;
|
|
7
|
+
silent_login_timeout?:number;
|
|
8
|
+
scope: string;
|
|
9
|
+
authority: string;
|
|
10
|
+
authority_time_cache_wellknowurl_in_second?: number;
|
|
11
|
+
authority_configuration?: AuthorityConfiguration;
|
|
12
|
+
refresh_time_before_tokens_expiration_in_second?: number;
|
|
13
|
+
token_request_timeout?: number;
|
|
14
|
+
service_worker_relative_url?:string;
|
|
15
|
+
service_worker_only?:boolean;
|
|
16
|
+
extras?:StringMap;
|
|
17
|
+
token_request_extras?:StringMap;
|
|
18
|
+
storage?: Storage;
|
|
19
|
+
monitor_session?: boolean;
|
|
20
|
+
token_renew_mode?: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export interface StringMap {
|
|
24
|
+
[key: string]: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface AuthorityConfiguration {
|
|
28
|
+
authorization_endpoint: string;
|
|
29
|
+
token_endpoint: string;
|
|
30
|
+
revocation_endpoint: string;
|
|
31
|
+
end_session_endpoint?: string;
|
|
32
|
+
userinfo_endpoint?: string;
|
|
33
|
+
check_session_iframe?:string;
|
|
34
|
+
issuer:string;
|
|
35
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { sleepAsync } from './initWorker';
|
|
2
|
+
import { isTokensValid } from './parseTokens';
|
|
3
|
+
|
|
4
|
+
export const userInfoAsync = async (oidc) => {
|
|
5
|
+
if (oidc.userInfo != null) {
|
|
6
|
+
return oidc.userInfo;
|
|
7
|
+
}
|
|
8
|
+
if (!oidc.tokens) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
const accessToken = oidc.tokens.accessToken;
|
|
12
|
+
if (!accessToken) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// We wait the synchronisation before making a request
|
|
17
|
+
while (oidc.tokens && !isTokensValid(oidc.tokens)) {
|
|
18
|
+
await sleepAsync(200);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const oidcServerConfiguration = await oidc.initAsync(oidc.configuration.authority, oidc.configuration.authority_configuration);
|
|
22
|
+
const url = oidcServerConfiguration.userInfoEndpoint;
|
|
23
|
+
const fetchUserInfo = async (accessToken) => {
|
|
24
|
+
const res = await fetch(url, {
|
|
25
|
+
headers: {
|
|
26
|
+
authorization: `Bearer ${accessToken}`,
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
if (res.status !== 200) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return res.json();
|
|
35
|
+
};
|
|
36
|
+
const userInfo = await fetchUserInfo(accessToken);
|
|
37
|
+
oidc.userInfo = userInfo;
|
|
38
|
+
return userInfo;
|
|
39
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { LoginCallback, Oidc
|
|
1
|
+
import { LoginCallback, Oidc } from './oidc';
|
|
2
2
|
import { getValidTokenAsync, Tokens, ValidToken } from './parseTokens';
|
|
3
|
+
import { OidcConfiguration, StringMap } from './types';
|
|
3
4
|
|
|
4
5
|
export interface EventSubscriber {
|
|
5
6
|
(name: string, data:any);
|