@axa-fr/oidc-client 6.26.6
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 +209 -0
- package/bin/post-install.mjs +58 -0
- package/dist/OidcServiceWorker.js +561 -0
- package/dist/OidcTrustedDomains.js +27 -0
- package/dist/cache.d.ts +3 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/checkSession.d.ts +4 -0
- package/dist/checkSession.d.ts.map +1 -0
- package/dist/checkSessionIFrame.d.ts +17 -0
- package/dist/checkSessionIFrame.d.ts.map +1 -0
- package/dist/crypto.d.ts +4 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/events.d.ts +29 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1236 -0
- package/dist/index.umd.cjs +2 -0
- package/dist/iniWorker.spec.d.ts +2 -0
- package/dist/iniWorker.spec.d.ts.map +1 -0
- package/dist/initSession.d.ts +22 -0
- package/dist/initSession.d.ts.map +1 -0
- package/dist/initWorker.d.ts +30 -0
- package/dist/initWorker.d.ts.map +1 -0
- package/dist/login.d.ts +8 -0
- package/dist/login.d.ts.map +1 -0
- package/dist/logout.d.ts +8 -0
- package/dist/logout.d.ts.map +1 -0
- package/dist/logout.spec.d.ts +1 -0
- package/dist/logout.spec.d.ts.map +1 -0
- package/dist/oidc.d.ts +101 -0
- package/dist/oidc.d.ts.map +1 -0
- package/dist/parseTokens.d.ts +37 -0
- package/dist/parseTokens.d.ts.map +1 -0
- package/dist/parseTokens.spec.d.ts +2 -0
- package/dist/parseTokens.spec.d.ts.map +1 -0
- package/dist/renewTokens.d.ts +4 -0
- package/dist/renewTokens.d.ts.map +1 -0
- package/dist/requests.d.ts +33 -0
- package/dist/requests.d.ts.map +1 -0
- package/dist/requests.spec.d.ts +2 -0
- package/dist/requests.spec.d.ts.map +1 -0
- package/dist/route-utils.d.ts +13 -0
- package/dist/route-utils.d.ts.map +1 -0
- package/dist/route-utils.spec.d.ts +2 -0
- package/dist/route-utils.spec.d.ts.map +1 -0
- package/dist/silentLogin.d.ts +10 -0
- package/dist/silentLogin.d.ts.map +1 -0
- package/dist/timer.d.ts +13 -0
- package/dist/timer.d.ts.map +1 -0
- package/dist/types.d.ts +38 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/user.d.ts +2 -0
- package/dist/user.d.ts.map +1 -0
- package/dist/vanillaOidc.d.ts +85 -0
- package/dist/vanillaOidc.d.ts.map +1 -0
- package/package.json +60 -0
- package/src/cache.ts +26 -0
- package/src/checkSession.ts +60 -0
- package/src/checkSessionIFrame.ts +83 -0
- package/src/crypto.ts +61 -0
- package/src/events.ts +28 -0
- package/src/index.ts +10 -0
- package/src/iniWorker.spec.ts +21 -0
- package/src/initSession.ts +89 -0
- package/src/initWorker.ts +321 -0
- package/src/login.ts +174 -0
- package/src/logout.spec.ts +65 -0
- package/src/logout.ts +101 -0
- package/src/oidc.ts +613 -0
- package/src/parseTokens.spec.ts +50 -0
- package/src/parseTokens.ts +194 -0
- package/src/renewTokens.ts +37 -0
- package/src/requests.spec.ts +9 -0
- package/src/requests.ts +169 -0
- package/src/route-utils.spec.ts +24 -0
- package/src/route-utils.ts +79 -0
- package/src/silentLogin.ts +144 -0
- package/src/timer.ts +163 -0
- package/src/types.ts +41 -0
- package/src/user.ts +40 -0
- package/src/vanillaOidc.ts +108 -0
package/src/timer.ts
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
const timer = (function () {
|
|
2
|
+
const workerPort = (function () {
|
|
3
|
+
let worker;
|
|
4
|
+
let blobURL;
|
|
5
|
+
|
|
6
|
+
const workerCode = function () {
|
|
7
|
+
const innerIdsByOuterIds = {};
|
|
8
|
+
|
|
9
|
+
const methods = {
|
|
10
|
+
setTimeout: function (port, id, timeout) {
|
|
11
|
+
innerIdsByOuterIds[id] = setTimeout(function () {
|
|
12
|
+
port.postMessage(id);
|
|
13
|
+
innerIdsByOuterIds[id] = null;
|
|
14
|
+
}, timeout);
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
setInterval: function (port, id, timeout) {
|
|
18
|
+
innerIdsByOuterIds[id] = setInterval(function () {
|
|
19
|
+
port.postMessage(id);
|
|
20
|
+
}, timeout);
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
clearTimeout: function (port, id) {
|
|
24
|
+
clearTimeout(innerIdsByOuterIds[id]);
|
|
25
|
+
innerIdsByOuterIds[id] = null;
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
clearInterval: function (port, id) {
|
|
29
|
+
clearInterval(innerIdsByOuterIds[id]);
|
|
30
|
+
innerIdsByOuterIds[id] = null;
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
function onMessage(port, event) {
|
|
35
|
+
const method = event.data[0];
|
|
36
|
+
const id = event.data[1];
|
|
37
|
+
const option = event.data[2];
|
|
38
|
+
|
|
39
|
+
if (methods[method]) {
|
|
40
|
+
methods[method](port, id, option);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// For Dedicated Worker
|
|
45
|
+
this.onmessage = function (event) {
|
|
46
|
+
onMessage(self, event);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// For Shared Worker
|
|
50
|
+
this.onconnect = function (event) {
|
|
51
|
+
const port = event.ports[0];
|
|
52
|
+
|
|
53
|
+
port.onmessage = function (event) {
|
|
54
|
+
onMessage(port, event);
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
}.toString();
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
const blob = new Blob(['(', workerCode, ')()'], { type: 'application/javascript' });
|
|
61
|
+
blobURL = URL.createObjectURL(blob);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
const isInsideBrowser = (typeof process === 'undefined');
|
|
66
|
+
try {
|
|
67
|
+
if (SharedWorker) {
|
|
68
|
+
worker = new SharedWorker(blobURL);
|
|
69
|
+
return worker.port;
|
|
70
|
+
}
|
|
71
|
+
} catch (error) {
|
|
72
|
+
if (isInsideBrowser) {
|
|
73
|
+
console.warn('SharedWorker not available');
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
if (Worker) {
|
|
78
|
+
worker = new Worker(blobURL);
|
|
79
|
+
return worker;
|
|
80
|
+
}
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if (isInsideBrowser) {
|
|
83
|
+
console.warn('Worker not available');
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return null;
|
|
88
|
+
}());
|
|
89
|
+
|
|
90
|
+
if (!workerPort) {
|
|
91
|
+
// In NextJS with SSR (Server Side Rendering) during rending in Node JS, the window object is undefined,
|
|
92
|
+
// the global object is used instead as it is the closest approximation of a browsers window object.
|
|
93
|
+
const bindContext = (typeof window === 'undefined') ? global : window;
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
setTimeout: setTimeout.bind(bindContext),
|
|
97
|
+
clearTimeout: clearTimeout.bind(bindContext),
|
|
98
|
+
setInterval: setInterval.bind(bindContext),
|
|
99
|
+
clearInterval: clearInterval.bind(bindContext),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const getId = (function () {
|
|
104
|
+
let currentId = 0;
|
|
105
|
+
|
|
106
|
+
return function () {
|
|
107
|
+
currentId++;
|
|
108
|
+
return currentId;
|
|
109
|
+
};
|
|
110
|
+
}());
|
|
111
|
+
|
|
112
|
+
const timeoutCallbacksById = {};
|
|
113
|
+
const intervalCallbacksById = {};
|
|
114
|
+
|
|
115
|
+
workerPort.onmessage = function (event) {
|
|
116
|
+
const id = event.data;
|
|
117
|
+
|
|
118
|
+
const timeoutCallback = timeoutCallbacksById[id];
|
|
119
|
+
if (timeoutCallback) {
|
|
120
|
+
timeoutCallback();
|
|
121
|
+
timeoutCallbacksById[id] = null;
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const intervalCallback = intervalCallbacksById[id];
|
|
126
|
+
if (intervalCallback) {
|
|
127
|
+
intervalCallback();
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
function setTimeoutWorker(callback, timeout) {
|
|
132
|
+
const id = getId();
|
|
133
|
+
workerPort.postMessage(['setTimeout', id, timeout]);
|
|
134
|
+
timeoutCallbacksById[id] = callback;
|
|
135
|
+
return id;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function clearTimeoutWorker(id) {
|
|
139
|
+
workerPort.postMessage(['clearTimeout', id]);
|
|
140
|
+
timeoutCallbacksById[id] = null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function setIntervalWorker(callback, timeout) {
|
|
144
|
+
const id = getId();
|
|
145
|
+
workerPort.postMessage(['setInterval', id, timeout]);
|
|
146
|
+
intervalCallbacksById[id] = callback;
|
|
147
|
+
return id;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function clearIntervalWorker(id) {
|
|
151
|
+
workerPort.postMessage(['clearInterval', id]);
|
|
152
|
+
intervalCallbacksById[id] = null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
setTimeout: setTimeoutWorker,
|
|
157
|
+
clearTimeout: clearTimeoutWorker,
|
|
158
|
+
setInterval: setIntervalWorker,
|
|
159
|
+
clearInterval: clearIntervalWorker,
|
|
160
|
+
};
|
|
161
|
+
}());
|
|
162
|
+
|
|
163
|
+
export default timer;
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export type Fetch = typeof window.fetch;
|
|
2
|
+
|
|
3
|
+
export type LogoutToken = 'access_token' | 'refresh_token';
|
|
4
|
+
|
|
5
|
+
export type OidcConfiguration = {
|
|
6
|
+
client_id: string;
|
|
7
|
+
redirect_uri: string;
|
|
8
|
+
silent_redirect_uri?:string;
|
|
9
|
+
silent_login_uri?:string;
|
|
10
|
+
silent_login_timeout?:number;
|
|
11
|
+
scope: string;
|
|
12
|
+
authority: string;
|
|
13
|
+
authority_time_cache_wellknowurl_in_second?: number;
|
|
14
|
+
authority_timeout_wellknowurl_in_millisecond?: number;
|
|
15
|
+
authority_configuration?: AuthorityConfiguration;
|
|
16
|
+
refresh_time_before_tokens_expiration_in_second?: number;
|
|
17
|
+
token_request_timeout?: number;
|
|
18
|
+
service_worker_relative_url?:string;
|
|
19
|
+
service_worker_only?:boolean;
|
|
20
|
+
service_worker_convert_all_requests_to_cors?:boolean;
|
|
21
|
+
extras?:StringMap;
|
|
22
|
+
token_request_extras?:StringMap;
|
|
23
|
+
storage?: Storage;
|
|
24
|
+
monitor_session?: boolean;
|
|
25
|
+
token_renew_mode?: string;
|
|
26
|
+
logout_tokens_to_invalidate?:Array<LogoutToken>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export interface StringMap {
|
|
30
|
+
[key: string]: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface AuthorityConfiguration {
|
|
34
|
+
authorization_endpoint: string;
|
|
35
|
+
token_endpoint: string;
|
|
36
|
+
revocation_endpoint: string;
|
|
37
|
+
end_session_endpoint?: string;
|
|
38
|
+
userinfo_endpoint?: string;
|
|
39
|
+
check_session_iframe?:string;
|
|
40
|
+
issuer:string;
|
|
41
|
+
}
|
package/src/user.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { sleepAsync } from './initWorker.js';
|
|
2
|
+
import { isTokensValid } from './parseTokens.js';
|
|
3
|
+
|
|
4
|
+
export const userInfoAsync = (oidc) => async (noCache = false) => {
|
|
5
|
+
if (oidc.userInfo != null && !noCache) {
|
|
6
|
+
return oidc.userInfo;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// We wait the synchronisation before making a request
|
|
10
|
+
while (oidc.tokens && !isTokensValid(oidc.tokens)) {
|
|
11
|
+
await sleepAsync(200);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (!oidc.tokens) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
const accessToken = oidc.tokens.accessToken;
|
|
18
|
+
if (!accessToken) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const oidcServerConfiguration = await oidc.initAsync(oidc.configuration.authority, oidc.configuration.authority_configuration);
|
|
23
|
+
const url = oidcServerConfiguration.userInfoEndpoint;
|
|
24
|
+
const fetchUserInfo = async (accessToken) => {
|
|
25
|
+
const res = await fetch(url, {
|
|
26
|
+
headers: {
|
|
27
|
+
authorization: `Bearer ${accessToken}`,
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (res.status !== 200) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return res.json();
|
|
36
|
+
};
|
|
37
|
+
const userInfo = await fetchUserInfo(accessToken);
|
|
38
|
+
oidc.userInfo = userInfo;
|
|
39
|
+
return userInfo;
|
|
40
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { LoginCallback, Oidc } from './oidc.js';
|
|
2
|
+
import { getValidTokenAsync, Tokens, ValidToken } from './parseTokens.js';
|
|
3
|
+
import { Fetch, OidcConfiguration, StringMap } from './types.js';
|
|
4
|
+
|
|
5
|
+
export interface EventSubscriber {
|
|
6
|
+
(name: string, data:any);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class VanillaOidc {
|
|
10
|
+
private _oidc: Oidc;
|
|
11
|
+
constructor(oidc: Oidc) {
|
|
12
|
+
this._oidc = oidc;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
subscribeEvents(func:EventSubscriber):string {
|
|
16
|
+
return this._oidc.subscribeEvents(func);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
removeEventSubscription(id:string):void {
|
|
20
|
+
this._oidc.removeEventSubscription(id);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
publishEvent(eventName:string, data:any) : void {
|
|
24
|
+
this._oidc.publishEvent(eventName, data);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static getOrCreate = (getFetch : () => Fetch) => (configuration:OidcConfiguration, name = 'default'): VanillaOidc => {
|
|
28
|
+
return new VanillaOidc(Oidc.getOrCreate(getFetch)(configuration, name));
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
static get(name = 'default'):VanillaOidc {
|
|
32
|
+
return new VanillaOidc(Oidc.get(name));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static eventNames = Oidc.eventNames;
|
|
36
|
+
tryKeepExistingSessionAsync():Promise<boolean> {
|
|
37
|
+
return this._oidc.tryKeepExistingSessionAsync();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
loginAsync(callbackPath:string = undefined, extras:StringMap = null, isSilentSignin = false, scope:string = undefined, silentLoginOnly = false):Promise<unknown> {
|
|
41
|
+
return this._oidc.loginAsync(callbackPath, extras, isSilentSignin, scope, silentLoginOnly);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
logoutAsync(callbackPathOrUrl: string | null | undefined = undefined, extras: StringMap = null):Promise<void> {
|
|
45
|
+
return this._oidc.logoutAsync(callbackPathOrUrl, extras);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
silentLoginCallbackAsync():Promise<void> {
|
|
49
|
+
return this._oidc.silentLoginCallbackAsync();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
renewTokensAsync(extras:StringMap = null):Promise<void> {
|
|
53
|
+
return this._oidc.renewTokensAsync(extras);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
loginCallbackAsync():Promise<LoginCallback> {
|
|
57
|
+
return this._oidc.loginCallbackWithAutoTokensRenewAsync();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
get tokens():Tokens {
|
|
61
|
+
return this._oidc.tokens;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
get configuration():OidcConfiguration {
|
|
65
|
+
return this._oidc.configuration;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async getValidTokenAsync(waitMs = 200, numberWait = 50): Promise<ValidToken> {
|
|
69
|
+
return getValidTokenAsync(this._oidc, waitMs, numberWait);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async userInfoAsync<T extends OidcUserInfo = OidcUserInfo>(noCache = false):Promise<T> {
|
|
73
|
+
return this._oidc.userInfoAsync(noCache);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface OidcUserInfo {
|
|
78
|
+
sub: string;
|
|
79
|
+
name?: string;
|
|
80
|
+
given_name?: string;
|
|
81
|
+
family_name?: string;
|
|
82
|
+
middle_name?: string;
|
|
83
|
+
nickname?: string;
|
|
84
|
+
preferred_username?: string;
|
|
85
|
+
profile?: string;
|
|
86
|
+
picture?: string;
|
|
87
|
+
website?: string;
|
|
88
|
+
email?: string;
|
|
89
|
+
email_verified?: boolean;
|
|
90
|
+
gender?: string;
|
|
91
|
+
birthdate?: string;
|
|
92
|
+
zoneinfo?: string;
|
|
93
|
+
locale?: string;
|
|
94
|
+
phone_number?: string;
|
|
95
|
+
phone_number_verified?: boolean;
|
|
96
|
+
address?: OidcAddressClaim;
|
|
97
|
+
updated_at?: number;
|
|
98
|
+
groups?: string[];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface OidcAddressClaim {
|
|
102
|
+
formatted?: string;
|
|
103
|
+
street_address?: string;
|
|
104
|
+
locality?: string;
|
|
105
|
+
region?: string;
|
|
106
|
+
postal_code?: string;
|
|
107
|
+
country?: string;
|
|
108
|
+
}
|