@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.
Files changed (82) hide show
  1. package/README.md +209 -0
  2. package/bin/post-install.mjs +58 -0
  3. package/dist/OidcServiceWorker.js +561 -0
  4. package/dist/OidcTrustedDomains.js +27 -0
  5. package/dist/cache.d.ts +3 -0
  6. package/dist/cache.d.ts.map +1 -0
  7. package/dist/checkSession.d.ts +4 -0
  8. package/dist/checkSession.d.ts.map +1 -0
  9. package/dist/checkSessionIFrame.d.ts +17 -0
  10. package/dist/checkSessionIFrame.d.ts.map +1 -0
  11. package/dist/crypto.d.ts +4 -0
  12. package/dist/crypto.d.ts.map +1 -0
  13. package/dist/events.d.ts +29 -0
  14. package/dist/events.d.ts.map +1 -0
  15. package/dist/index.d.ts +6 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +1236 -0
  18. package/dist/index.umd.cjs +2 -0
  19. package/dist/iniWorker.spec.d.ts +2 -0
  20. package/dist/iniWorker.spec.d.ts.map +1 -0
  21. package/dist/initSession.d.ts +22 -0
  22. package/dist/initSession.d.ts.map +1 -0
  23. package/dist/initWorker.d.ts +30 -0
  24. package/dist/initWorker.d.ts.map +1 -0
  25. package/dist/login.d.ts +8 -0
  26. package/dist/login.d.ts.map +1 -0
  27. package/dist/logout.d.ts +8 -0
  28. package/dist/logout.d.ts.map +1 -0
  29. package/dist/logout.spec.d.ts +1 -0
  30. package/dist/logout.spec.d.ts.map +1 -0
  31. package/dist/oidc.d.ts +101 -0
  32. package/dist/oidc.d.ts.map +1 -0
  33. package/dist/parseTokens.d.ts +37 -0
  34. package/dist/parseTokens.d.ts.map +1 -0
  35. package/dist/parseTokens.spec.d.ts +2 -0
  36. package/dist/parseTokens.spec.d.ts.map +1 -0
  37. package/dist/renewTokens.d.ts +4 -0
  38. package/dist/renewTokens.d.ts.map +1 -0
  39. package/dist/requests.d.ts +33 -0
  40. package/dist/requests.d.ts.map +1 -0
  41. package/dist/requests.spec.d.ts +2 -0
  42. package/dist/requests.spec.d.ts.map +1 -0
  43. package/dist/route-utils.d.ts +13 -0
  44. package/dist/route-utils.d.ts.map +1 -0
  45. package/dist/route-utils.spec.d.ts +2 -0
  46. package/dist/route-utils.spec.d.ts.map +1 -0
  47. package/dist/silentLogin.d.ts +10 -0
  48. package/dist/silentLogin.d.ts.map +1 -0
  49. package/dist/timer.d.ts +13 -0
  50. package/dist/timer.d.ts.map +1 -0
  51. package/dist/types.d.ts +38 -0
  52. package/dist/types.d.ts.map +1 -0
  53. package/dist/user.d.ts +2 -0
  54. package/dist/user.d.ts.map +1 -0
  55. package/dist/vanillaOidc.d.ts +85 -0
  56. package/dist/vanillaOidc.d.ts.map +1 -0
  57. package/package.json +60 -0
  58. package/src/cache.ts +26 -0
  59. package/src/checkSession.ts +60 -0
  60. package/src/checkSessionIFrame.ts +83 -0
  61. package/src/crypto.ts +61 -0
  62. package/src/events.ts +28 -0
  63. package/src/index.ts +10 -0
  64. package/src/iniWorker.spec.ts +21 -0
  65. package/src/initSession.ts +89 -0
  66. package/src/initWorker.ts +321 -0
  67. package/src/login.ts +174 -0
  68. package/src/logout.spec.ts +65 -0
  69. package/src/logout.ts +101 -0
  70. package/src/oidc.ts +613 -0
  71. package/src/parseTokens.spec.ts +50 -0
  72. package/src/parseTokens.ts +194 -0
  73. package/src/renewTokens.ts +37 -0
  74. package/src/requests.spec.ts +9 -0
  75. package/src/requests.ts +169 -0
  76. package/src/route-utils.spec.ts +24 -0
  77. package/src/route-utils.ts +79 -0
  78. package/src/silentLogin.ts +144 -0
  79. package/src/timer.ts +163 -0
  80. package/src/types.ts +41 -0
  81. package/src/user.ts +40 -0
  82. package/src/vanillaOidc.ts +108 -0
package/README.md ADDED
@@ -0,0 +1,209 @@
1
+ # @axa-fr/vanilla-oidc
2
+
3
+ [![Continuous Integration](https://github.com/AxaGuilDEv/react-oidc/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/AxaGuilDEv/react-oidc/actions/workflows/npm-publish.yml)
4
+ [![Quality Gate](https://sonarcloud.io/api/project_badges/measure?project=AxaGuilDEv_react-oidc&metric=alert_status)](https://sonarcloud.io/dashboard?id=AxaGuilDEv_react-oidc) [![Reliability](https://sonarcloud.io/api/project_badges/measure?project=AxaGuilDEv_react-oidc&metric=reliability_rating)](https://sonarcloud.io/component_measures?id=AxaGuilDEv_react-oidc&metric=reliability_rating) [![Security](https://sonarcloud.io/api/project_badges/measure?project=AxaGuilDEv_react-oidc&metric=security_rating)](https://sonarcloud.io/component_measures?id=AxaGuilDEv_react-oidc&metric=security_rating) [![Code Corevage](https://sonarcloud.io/api/project_badges/measure?project=AxaGuilDEv_react-oidc&metric=coverage)](https://sonarcloud.io/component_measures?id=AxaGuilDEv_react-oidc&metric=Coverage) [![Twitter](https://img.shields.io/twitter/follow/GuildDEvOpen?style=social)](https://twitter.com/intent/follow?screen_name=GuildDEvOpen)
5
+
6
+ Try the demo at https://icy-glacier-004ab4303.2.azurestaticapps.net/
7
+
8
+
9
+ - [About](#about)
10
+ - [Getting Started](#getting-started)
11
+ - [Run The Demo](#run-the-demo)
12
+ - [How It Works](#how-it-works)
13
+ - [Hash route](#Hash-route)
14
+ - [Service Worker Support](#service-worker-support)
15
+
16
+
17
+ ## About
18
+
19
+ @axa-fr/vanilla-oidc is a pure OIDC client library agnostic to any framework. It is used by @axa-fr/react-oidc and can be used by any framework.
20
+
21
+ It is a real alternative to existing oidc-client libraries.
22
+
23
+ - **Secure** :
24
+ - With the use of Service Worker, your tokens (refresh_token and access_token) are not accessible to the JavaScript client code (big protection against XSRF attacks)
25
+ - OIDC using client side Code Credential Grant with PKCE only
26
+ - **Lightweight**
27
+ - **Simple** :
28
+ - refresh_token and access_token are auto refreshed in background
29
+ - with the use of the Service Worker, you do not need to inject the access_token in every fetch, you have only to configure `OidcTrustedDomains.js` file
30
+ - **No cookies problem** : You can disable silent signin (that internally use an iframe). For your information, your OIDC server should be in the same domain of your website in order to be able to send OIDC server cookies from your website via an internal IFRAME, else, you may encounter COOKIES problem.
31
+ - **Multiple Authentication** :
32
+ - You can authenticate many times to the same provider with different scope (for example you can acquire a new 'payment' scope for a payment)
33
+ - You can authenticate to multiple different providers inside the same SPA (single page application) website
34
+ - **Flexible** :
35
+ - Work with Service Worker (more secure) and without for older browser (less secure)
36
+
37
+ ![](https://github.com/AxaGuilDEv/react-oidc/blob/master/docs/img/schema_pcke_client_side_with_service_worker.png?raw=true)
38
+
39
+ The service worker catch **access_token** and **refresh_token** that will never be accessible to the client.
40
+
41
+
42
+ ### Getting Started
43
+
44
+ ```sh
45
+ npm install @axa-fr/oidc-client --save
46
+
47
+ # If you have a "public" folder, the 2 files will be created :
48
+ # ./public/OidcServiceWorker.js <-- will be updated at each "npm install"
49
+ # ./public/OidcTrustedDomains.js <-- won't be updated if already exist
50
+ ```
51
+
52
+ If you need a very secure mode where refresh_token and access_token will be hide behind a service worker that will proxify requests.
53
+ The only file you should edit is "OidcTrustedDomains.js".
54
+
55
+ ```javascript
56
+ // OidcTrustedDomains.js
57
+
58
+ // Add bellow trusted domains, access tokens will automatically injected to be send to
59
+ // trusted domain can also be a path like https://www.myapi.com/users,
60
+ // then all subroute like https://www.myapi.com/useers/1 will be authorized to send access_token to.
61
+
62
+ // Domains used by OIDC server must be also declared here
63
+ const trustedDomains = {
64
+ default: ["https://demo.duendesoftware.com", "https://www.myapi.com/users"],
65
+ };
66
+ ```
67
+
68
+ The code of the demo :
69
+
70
+ ```js
71
+ import { VanillaOidc } from '@axa-fr/oidc-client'
72
+
73
+ export const configuration = {
74
+ client_id: 'interactive.public.short',
75
+ redirect_uri: window.location.origin + '/#/authentication/callback',
76
+ silent_redirect_uri: window.location.origin + '/#/authentication/silent-callback',
77
+ scope: 'openid profile email api offline_access',
78
+ authority: 'https://demo.duendesoftware.com',
79
+ service_worker_relative_url:'/OidcServiceWorker.js',
80
+ service_worker_only: false,
81
+ };
82
+
83
+ const href = window.location.href;
84
+ const vanillaOidc = VanillaOidc.getOrCreate(() => fetch)(configuration);
85
+
86
+ console.log(href);
87
+
88
+ vanillaOidc.tryKeepExistingSessionAsync().then(() => {
89
+ if(href.includes(configuration.redirect_uri)){
90
+ vanillaOidc.loginCallbackAsync().then(()=>{
91
+ window.location.href = "/";
92
+ });
93
+ document.body.innerHTML = `<div>
94
+ <h1>@axa-fr/oidc-client demo</h1>
95
+ <h2>Loading</h2>
96
+ </div>`;
97
+ return
98
+ }
99
+
100
+ let tokens = vanillaOidc.tokens;
101
+
102
+ if(tokens){
103
+
104
+ // @ts-ignore
105
+ window.logout = () => vanillaOidc.logoutAsync();
106
+ document.body.innerHTML = `<div>
107
+ <h1>@axa-fr/oidc-client demo</h1>
108
+ <button onclick="window.logout()">Logout</button>
109
+ <h2>Authenticated</h2>
110
+ <pre>${JSON.stringify(tokens,null,'\t')}</pre>
111
+ </div>`
112
+
113
+ }
114
+ else {
115
+ // @ts-ignore
116
+ window.login= () => vanillaOidc.loginAsync("/");
117
+ document.body.innerHTML = `<div>
118
+ <h1>@axa-fr/oidc-client demo</h1>
119
+ <button onclick="window.login()">Login</button>
120
+ </div>`
121
+ }
122
+ })
123
+
124
+
125
+ ```
126
+
127
+ ## Configuration
128
+
129
+ ```javascript
130
+ const configuration: {
131
+ client_id: PropTypes.string.isRequired, // oidc client id
132
+ redirect_uri: PropTypes.string.isRequired, // oidc redirect url
133
+ silent_redirect_uri: PropTypes.string, // Optional activate silent-signin that use cookies between OIDC server and client javascript to restore sessions
134
+ silent_login_uri: PropTypes.string, // Optional, route that trigger the signin
135
+ silent_login_timeout: PropTypes.number, // Optional default is 12000 milliseconds
136
+ scope: PropTypes.string.isRequired, // oidc scope (you need to set "offline_access")
137
+ authority: PropTypes.string.isRequired,
138
+ storage: Storage, // Default sessionStorage, you can set localStorage but it is less secure to XSS attacks
139
+ authority_configuration: PropTypes.shape({
140
+ // Optional for providers that does not implement OIDC server auto discovery via a .wellknowurl
141
+ authorization_endpoint: PropTypes.string,
142
+ token_endpoint: PropTypes.string,
143
+ userinfo_endpoint: PropTypes.string,
144
+ end_session_endpoint: PropTypes.string,
145
+ revocation_endpoint: PropTypes.string,
146
+ check_session_iframe: PropTypes.string,
147
+ issuer: PropTypes.string,
148
+ }),
149
+ refresh_time_before_tokens_expiration_in_second: PropTypes.number, // default is 120 seconds
150
+ service_worker_relative_url: PropTypes.string,
151
+ service_worker_only: PropTypes.boolean, // default false
152
+ service_worker_convert_all_requests_to_cors: PropTypes.boolean, // force all requests that servie worker upgrades to have 'cors' mode. This allows setting authentication token on requests initialted by html parsing(e.g. img tags, download links etc).
153
+ extras: StringMap | undefined, // ex: {'prompt': 'consent', 'access_type': 'offline'} list of key/value that are send to the oidc server (more info: https://github.com/openid/AppAuth-JS)
154
+ token_request_extras: StringMap | undefined, // ex: {'prompt': 'consent', 'access_type': 'offline'} list of key/value that are send to the oidc server during token request (more info: https://github.com/openid/AppAuth-JS)
155
+ withCustomHistory: PropTypes.function, // Override history modification, return instance with replaceState(url, stateHistory) implemented (like History.replaceState())
156
+ authority_time_cache_wellknowurl_in_second: 60 * 60, // Time to cache in second of openid wellknowurl, default is 1 hour
157
+ authority_timeout_wellknowurl_in_millisecond: 10000, // Timeout in millisecond of openid wellknowurl, default is 10 seconds, then error is throwed
158
+ monitor_session: PropTypes.boolean, // Add OpenId monitor session, default is false (more information https://openid.net/specs/openid-connect-session-1_0.html), if you need to set it to true consider https://infi.nl/nieuws/spa-necromancy/
159
+ onLogoutFromAnotherTab: Function, // Optional, can be set to override the default behavior, this function is triggered when user with the same subject is logged out from another tab when session_monitor is active
160
+ onLogoutFromSameTab: Function, // Optional, can be set to override the default behavior, this function is triggered when user is logged out from same tab when session_monitor is active
161
+ token_renew_mode: PropTypes.string, // Optional, update tokens base on the selected token(s) lifetime: "access_token_or_id_token_invalid" (default), "access_token_invalid" , "id_token_invalid"
162
+ logout_tokens_to_invalidate : Array<string> // Optional tokens to invalidate during logout, default: ['access_token', 'refresh_token']
163
+ };
164
+ ```
165
+
166
+ ## Run The Demo
167
+
168
+ ```sh
169
+ git clone https://github.com/AxaGuilDEv/react-oidc.git
170
+ cd react-oidc/packages/vanilla-demo
171
+ npm install
172
+ npm start
173
+ # then navigate to http://localhost:3000
174
+ ```
175
+
176
+ ## How It Works
177
+
178
+ This component is a pure vanilla JS OIDC client library agnostic to any framework.
179
+ It is a real alternative to existing oidc-client libraries.
180
+
181
+ More information about OIDC
182
+
183
+ - [French : Augmentez la sécurité et la simplicité de votre Système d’Information OpenID Connect](https://medium.com/just-tech-it-now/augmentez-la-s%C3%A9curit%C3%A9-et-la-simplicit%C3%A9-de-votre-syst%C3%A8me-dinformation-avec-oauth-2-0-cf0732d71284)
184
+ - [English : Increase the security and simplicity of your information system with openid connect](https://medium.com/just-tech-it-now/increase-the-security-and-simplicity-of-your-information-system-with-openid-connect-fa8c26b99d6d)
185
+
186
+ ## Hash route
187
+
188
+ `vanilla-oidc` work also with hash route.
189
+
190
+ ```javascript
191
+ export const configurationIdentityServerWithHash = {
192
+ client_id: "interactive.public.short",
193
+ redirect_uri: window.location.origin + "#authentication-callback",
194
+ silent_redirect_uri:
195
+ window.location.origin + "#authentication-silent-callback",
196
+ scope: "openid profile email api offline_access",
197
+ authority: "https://demo.duendesoftware.com",
198
+ refresh_time_before_tokens_expiration_in_second: 70,
199
+ service_worker_relative_url: "/OidcServiceWorker.js",
200
+ service_worker_only: false,
201
+ };
202
+ ```
203
+
204
+ ## Service Worker Support
205
+
206
+ - Firefox : tested on Firefox 98.0.2
207
+ - Chrome/Edge : tested on version upper to 90
208
+ - Opera : tested on version upper to 80
209
+ - Safari : tested on Safari/605.1.15
@@ -0,0 +1,58 @@
1
+ import path from 'path';
2
+ import fs from 'fs';
3
+
4
+ try {
5
+
6
+ /**
7
+ * Script to run after npm install
8
+ *
9
+ * Copy selected files to user's directory
10
+ */
11
+ const script_prefix= 'oidc-client';
12
+
13
+ const copyFile = async (src, dest, overwrite) => {
14
+ if(!fileExists(src)) {
15
+ console.log(`[${script_prefix}:skip] file does not exist ${src}`);
16
+ return;
17
+ }
18
+ if (!overwrite) {
19
+ if (fileExists(dest)) {
20
+ console.log(`[${script_prefix}:skip] file exists not overwriting ${dest}`);
21
+ return;
22
+ }
23
+ }
24
+ await fs.promises.copyFile(src, dest);
25
+ console.log(`[${script_prefix}:copy] ${dest}`);
26
+ };
27
+
28
+ const fileExists = (path) => {
29
+ return !!fs.existsSync(path);
30
+ };
31
+
32
+ const initPath = process.env.INIT_CWD;
33
+
34
+ const srcDir = '../oidc-client-service-worker/dist/';
35
+ const destinationDir = path.join(initPath, 'public');
36
+
37
+ const files = [
38
+ {
39
+ fileName: 'OidcServiceWorker.js',
40
+ overwrite: true,
41
+ },
42
+ {
43
+ fileName: 'OidcTrustedDomains.js',
44
+ overwrite: false,
45
+ },
46
+ ];
47
+
48
+ for await (const file of files) {
49
+ await copyFile(
50
+ path.join(srcDir, file.fileName),
51
+ path.join(destinationDir, file.fileName),
52
+ file.overwrite
53
+ );
54
+ }
55
+
56
+ } catch (err) {
57
+ console.warn(err);
58
+ }