@axa-fr/react-oidc 5.7.0-aplha0

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 (160) hide show
  1. package/README.md +502 -0
  2. package/dist/FetchToken.d.ts +10 -0
  3. package/dist/FetchToken.d.ts.map +1 -0
  4. package/dist/FetchToken.js +107 -0
  5. package/dist/FetchToken.js.map +1 -0
  6. package/dist/OidcProvider.d.ts +25 -0
  7. package/dist/OidcProvider.d.ts.map +1 -0
  8. package/dist/OidcProvider.js +133 -0
  9. package/dist/OidcProvider.js.map +1 -0
  10. package/dist/OidcSecure.d.ts +10 -0
  11. package/dist/OidcSecure.d.ts.map +1 -0
  12. package/dist/OidcSecure.js +68 -0
  13. package/dist/OidcSecure.js.map +1 -0
  14. package/dist/OidcServiceWorker.js +272 -0
  15. package/dist/OidcTrustedDomains.js +6 -0
  16. package/dist/ReactOidc.d.ts +17 -0
  17. package/dist/ReactOidc.d.ts.map +1 -0
  18. package/dist/ReactOidc.js +106 -0
  19. package/dist/ReactOidc.js.map +1 -0
  20. package/dist/User.d.ts +15 -0
  21. package/dist/User.d.ts.map +1 -0
  22. package/dist/User.js +48 -0
  23. package/dist/User.js.map +1 -0
  24. package/dist/core/default-component/AuthenticateError.component.d.ts +4 -0
  25. package/dist/core/default-component/AuthenticateError.component.d.ts.map +1 -0
  26. package/dist/core/default-component/AuthenticateError.component.js +32 -0
  27. package/dist/core/default-component/AuthenticateError.component.js.map +1 -0
  28. package/dist/core/default-component/Authenticating.component.d.ts +4 -0
  29. package/dist/core/default-component/Authenticating.component.d.ts.map +1 -0
  30. package/dist/core/default-component/Authenticating.component.js +32 -0
  31. package/dist/core/default-component/Authenticating.component.js.map +1 -0
  32. package/dist/core/default-component/Callback.component.d.ts +5 -0
  33. package/dist/core/default-component/Callback.component.d.ts.map +1 -0
  34. package/dist/core/default-component/Callback.component.js +118 -0
  35. package/dist/core/default-component/Callback.component.js.map +1 -0
  36. package/dist/core/default-component/Loading.component.d.ts +4 -0
  37. package/dist/core/default-component/Loading.component.d.ts.map +1 -0
  38. package/dist/core/default-component/Loading.component.js +29 -0
  39. package/dist/core/default-component/Loading.component.js.map +1 -0
  40. package/dist/core/default-component/ServiceWorkerInstall.component.d.ts +4 -0
  41. package/dist/core/default-component/ServiceWorkerInstall.component.d.ts.map +1 -0
  42. package/dist/core/default-component/ServiceWorkerInstall.component.js +122 -0
  43. package/dist/core/default-component/ServiceWorkerInstall.component.js.map +1 -0
  44. package/dist/core/default-component/ServiceWorkerNotSupported.component.d.ts +4 -0
  45. package/dist/core/default-component/ServiceWorkerNotSupported.component.d.ts.map +1 -0
  46. package/dist/core/default-component/ServiceWorkerNotSupported.component.js +32 -0
  47. package/dist/core/default-component/ServiceWorkerNotSupported.component.js.map +1 -0
  48. package/dist/core/default-component/SessionLost.component.d.ts +4 -0
  49. package/dist/core/default-component/SessionLost.component.d.ts.map +1 -0
  50. package/dist/core/default-component/SessionLost.component.js +14 -0
  51. package/dist/core/default-component/SessionLost.component.js.map +1 -0
  52. package/dist/core/default-component/SilentCallback.component.d.ts +4 -0
  53. package/dist/core/default-component/SilentCallback.component.d.ts.map +1 -0
  54. package/dist/core/default-component/SilentCallback.component.js +96 -0
  55. package/dist/core/default-component/SilentCallback.component.js.map +1 -0
  56. package/dist/core/default-component/index.d.ts +7 -0
  57. package/dist/core/default-component/index.d.ts.map +1 -0
  58. package/dist/core/default-component/index.js +20 -0
  59. package/dist/core/default-component/index.js.map +1 -0
  60. package/dist/core/routes/OidcRoutes.d.ts +12 -0
  61. package/dist/core/routes/OidcRoutes.d.ts.map +1 -0
  62. package/dist/core/routes/OidcRoutes.js +71 -0
  63. package/dist/core/routes/OidcRoutes.js.map +1 -0
  64. package/dist/core/routes/index.d.ts +3 -0
  65. package/dist/core/routes/index.d.ts.map +1 -0
  66. package/dist/core/routes/index.js +9 -0
  67. package/dist/core/routes/index.js.map +1 -0
  68. package/dist/core/routes/route-utils.d.ts +2 -0
  69. package/dist/core/routes/route-utils.d.ts.map +1 -0
  70. package/dist/core/routes/route-utils.js +32 -0
  71. package/dist/core/routes/route-utils.js.map +1 -0
  72. package/dist/core/routes/withRouter.d.ts +19 -0
  73. package/dist/core/routes/withRouter.d.ts.map +1 -0
  74. package/dist/core/routes/withRouter.js +33 -0
  75. package/dist/core/routes/withRouter.js.map +1 -0
  76. package/dist/index.d.ts +6 -0
  77. package/dist/index.d.ts.map +1 -0
  78. package/dist/index.js +19 -0
  79. package/dist/index.js.map +1 -0
  80. package/dist/vanilla/index.d.ts +2 -0
  81. package/dist/vanilla/index.d.ts.map +1 -0
  82. package/dist/vanilla/index.js +6 -0
  83. package/dist/vanilla/index.js.map +1 -0
  84. package/dist/vanilla/initSession.d.ts +11 -0
  85. package/dist/vanilla/initSession.d.ts.map +1 -0
  86. package/dist/vanilla/initSession.js +72 -0
  87. package/dist/vanilla/initSession.js.map +1 -0
  88. package/dist/vanilla/initWorker.d.ts +13 -0
  89. package/dist/vanilla/initWorker.d.ts.map +1 -0
  90. package/dist/vanilla/initWorker.js +211 -0
  91. package/dist/vanilla/initWorker.js.map +1 -0
  92. package/dist/vanilla/memoryStorageBackend.d.ts +10 -0
  93. package/dist/vanilla/memoryStorageBackend.d.ts.map +1 -0
  94. package/dist/vanilla/memoryStorageBackend.js +33 -0
  95. package/dist/vanilla/memoryStorageBackend.js.map +1 -0
  96. package/dist/vanilla/noHashQueryStringUtils.d.ts +5 -0
  97. package/dist/vanilla/noHashQueryStringUtils.d.ts.map +1 -0
  98. package/dist/vanilla/noHashQueryStringUtils.js +31 -0
  99. package/dist/vanilla/noHashQueryStringUtils.js.map +1 -0
  100. package/dist/vanilla/oidc.d.ts +77 -0
  101. package/dist/vanilla/oidc.d.ts.map +1 -0
  102. package/dist/vanilla/oidc.js +814 -0
  103. package/dist/vanilla/oidc.js.map +1 -0
  104. package/dist/vanilla/timer.d.ts +8 -0
  105. package/dist/vanilla/timer.d.ts.map +1 -0
  106. package/dist/vanilla/timer.js +135 -0
  107. package/dist/vanilla/timer.js.map +1 -0
  108. package/package.json +73 -0
  109. package/src/App.css +38 -0
  110. package/src/App.specold.tsx +46 -0
  111. package/src/App.tsx +61 -0
  112. package/src/FetchUser.tsx +53 -0
  113. package/src/Home.tsx +20 -0
  114. package/src/MultiAuth.tsx +114 -0
  115. package/src/Profile.tsx +77 -0
  116. package/src/configurations.ts +53 -0
  117. package/src/index.css +13 -0
  118. package/src/index.tsx +9 -0
  119. package/src/logo.svg +7 -0
  120. package/src/oidc/FetchToken.tsx +51 -0
  121. package/src/oidc/OidcProvider.tsx +165 -0
  122. package/src/oidc/OidcSecure.tsx +32 -0
  123. package/src/oidc/ReactOidc.tsx +112 -0
  124. package/src/oidc/User.ts +38 -0
  125. package/src/oidc/core/default-component/AuthenticateError.component.tsx +13 -0
  126. package/src/oidc/core/default-component/Authenticating.component.tsx +13 -0
  127. package/src/oidc/core/default-component/Callback.component.tsx +49 -0
  128. package/src/oidc/core/default-component/Loading.component.tsx +10 -0
  129. package/src/oidc/core/default-component/ServiceWorkerInstall.component.tsx +51 -0
  130. package/src/oidc/core/default-component/ServiceWorkerNotSupported.component.tsx +13 -0
  131. package/src/oidc/core/default-component/SessionLost.component.tsx +14 -0
  132. package/src/oidc/core/default-component/SilentCallback.component.tsx +31 -0
  133. package/src/oidc/core/default-component/index.ts +6 -0
  134. package/src/oidc/core/routes/OidcRoutes.spec.tsx +16 -0
  135. package/src/oidc/core/routes/OidcRoutes.tsx +69 -0
  136. package/src/oidc/core/routes/__snapshots__/OidcRoutes.spec.tsx.snap +7 -0
  137. package/src/oidc/core/routes/index.ts +2 -0
  138. package/src/oidc/core/routes/route-utils.spec.ts +9 -0
  139. package/src/oidc/core/routes/route-utils.ts +34 -0
  140. package/src/oidc/core/routes/withRouter.spec.tsx +48 -0
  141. package/src/oidc/core/routes/withRouter.tsx +60 -0
  142. package/src/oidc/index.ts +5 -0
  143. package/src/oidc/vanilla/OidcServiceWorker.js +272 -0
  144. package/src/oidc/vanilla/OidcTrustedDomains.js +6 -0
  145. package/src/oidc/vanilla/index.ts +1 -0
  146. package/src/oidc/vanilla/initSession.ts +36 -0
  147. package/src/oidc/vanilla/initWorker.ts +153 -0
  148. package/src/oidc/vanilla/memoryStorageBackend.ts +33 -0
  149. package/src/oidc/vanilla/noHashQueryStringUtils.ts +7 -0
  150. package/src/oidc/vanilla/oidc.ts +600 -0
  151. package/src/oidc/vanilla/timer.ts +157 -0
  152. package/src/override/AuthenticateError.component.tsx +14 -0
  153. package/src/override/Authenticating.component.tsx +14 -0
  154. package/src/override/Callback.component.tsx +13 -0
  155. package/src/override/Loading.component.tsx +13 -0
  156. package/src/override/ServiceWorkerNotSupported.component.tsx +15 -0
  157. package/src/override/SessionLost.component.tsx +21 -0
  158. package/src/override/style.ts +10 -0
  159. package/src/setupTests.js +5 -0
  160. package/tsconfig.json +38 -0
@@ -0,0 +1,77 @@
1
+ import React from 'react';
2
+
3
+ import {OidcSecure, useOidcAccessToken, useOidcIdToken, useOidcUser, OidcUserStatus} from "./oidc";
4
+
5
+
6
+ const DisplayUserInfo = () => {
7
+ const{ oidcUser, oidcUserLoadingState } = useOidcUser();
8
+
9
+ switch (oidcUserLoadingState){
10
+ case OidcUserStatus.Loading:
11
+ return <p>User Information are loading</p>;
12
+ case OidcUserStatus.Unauthenticated:
13
+ return <p>you are not authenticated</p>;
14
+ case OidcUserStatus.LoadingError:
15
+ return <p>Fail to load user information</p>;
16
+ default:
17
+ return (
18
+ <div className="card text-white bg-success mb-3">
19
+ <div className="card-body">
20
+ <h5 className="card-title">User information</h5>
21
+ <p className="card-text">{JSON.stringify(oidcUser)}</p>
22
+ </div>
23
+ </div>
24
+ );
25
+ }
26
+ };
27
+
28
+ export const Profile = () => {
29
+
30
+ return (
31
+ <div className="container mt-3">
32
+ <DisplayAccessToken/>
33
+ <DisplayIdToken/>
34
+ <DisplayUserInfo/>
35
+ </div>
36
+ );
37
+ }
38
+
39
+ const DisplayAccessToken = () => {
40
+ const{ accessToken, accessTokenPayload } = useOidcAccessToken();
41
+
42
+ if(!accessToken){
43
+ return <p>you are not authenticated</p>
44
+ }
45
+ return (
46
+ <div className="card text-white bg-info mb-3">
47
+ <div className="card-body">
48
+ <h5 className="card-title">Access Token</h5>
49
+ <p style={{color:'red', "backgroundColor": 'white'}}>Please consider to configure the ServiceWorker in order to protect your application from XSRF attacks. "access_token" and "refresh_token" will never be accessible from your client side javascript.</p>
50
+ {<p className="card-text">Access Token: {JSON.stringify(accessToken)}</p>}
51
+ {accessTokenPayload != null && <p className="card-text">Access Token Payload: {JSON.stringify(accessTokenPayload)}</p>}
52
+ </div>
53
+ </div>
54
+ )
55
+ };
56
+
57
+
58
+ const DisplayIdToken =() => {
59
+ const{ idToken, idTokenPayload } = useOidcIdToken();
60
+
61
+ if(!idToken){
62
+ return <p>you are not authenticated</p>
63
+ }
64
+
65
+ return (
66
+ <div className="card text-white bg-info mb-3">
67
+ <div className="card-body">
68
+ <h5 className="card-title">ID Token</h5>
69
+ {<p className="card-text">IdToken: {JSON.stringify(idToken)}</p>}
70
+ {idTokenPayload != null && <p className="card-text">IdToken Payload: {JSON.stringify(idTokenPayload)}</p>}
71
+ </div>
72
+ </div>
73
+ );
74
+ }
75
+
76
+
77
+ export const SecureProfile = () => <OidcSecure><Profile /></OidcSecure>;
@@ -0,0 +1,53 @@
1
+ export const configurationIdentityServer = {
2
+ client_id: 'interactive.public.short',
3
+ redirect_uri: window.location.origin+'/authentication/callback',
4
+ silent_redirect_uri: window.location.origin+'/authentication/silent-callback',
5
+ scope: 'openid profile email api offline_access',
6
+ authority: 'https://demo.duendesoftware.com',
7
+ refresh_time_before_tokens_expiration_in_second: 70,
8
+ service_worker_relative_url:'/OidcServiceWorker.js',
9
+ service_worker_only: false,
10
+ };
11
+
12
+ export const configurationIdentityServerWithoutDiscovery = {
13
+ client_id: 'interactive.public.short',
14
+ redirect_uri: window.location.origin+'/authentication/callback',
15
+ silent_redirect_uri: window.location.origin+'/authentication/silent-callback',
16
+ scope: 'openid profile email api offline_access',
17
+ authority: 'https://demo.duendesoftware.com',
18
+ authority_configuration: {
19
+ authorization_endpoint: 'https://demo.duendesoftware.com/connect/authorize',
20
+ token_endpoint: 'https://demo.duendesoftware.com/connect/token',
21
+ userinfo_endpoint: 'https://demo.duendesoftware.com/connect/userinfo',
22
+ end_session_endpoint: 'https://demo.duendesoftware.com/connect/endsession',
23
+ revocation_endpoint: 'https://demo.duendesoftware.com/connect/revocation',
24
+ },
25
+ refresh_time_before_tokens_expiration_in_second: 70,
26
+ service_worker_relative_url:'/OidcServiceWorker.js',
27
+ service_worker_only: false,
28
+ };
29
+
30
+ export const configurationAuth0 = {
31
+ client_id: 'xGZxEAJhzlkuQUlWl90y1ntIX-0UDWHx',
32
+ redirect_uri: window.location.origin+'/callback',
33
+ scope: 'openid profile email api offline_access',
34
+ authority: 'https://kdhttps.auth0.com',
35
+ refresh_time_before_tokens_expiration_in_second: 70,
36
+ service_worker_relative_url:'/OidcServiceWorker.js',
37
+ service_worker_only: false,
38
+ };
39
+
40
+
41
+
42
+ export const configurationGoogle = {
43
+ client_id: '908893276222-f2drloh56ll0g99md38lv2k810d0nk0p.apps.googleusercontent.com',
44
+ redirect_uri: `${window.location.origin}/multi-auth/callback-google`,
45
+ silent_redirect_uri: window.location.origin+'/multi-auth/silent-callback-google',
46
+ scope: 'openid profile email',
47
+ authority: 'https://accounts.google.com/',
48
+ service_worker_relative_url:'/OidcServiceWorker.js',
49
+ service_worker_only: false,
50
+ token_request_extras: {
51
+ client_secret : "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"
52
+ }
53
+ };
package/src/index.css ADDED
@@ -0,0 +1,13 @@
1
+ body {
2
+ margin: 0;
3
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4
+ 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5
+ sans-serif;
6
+ -webkit-font-smoothing: antialiased;
7
+ -moz-osx-font-smoothing: grayscale;
8
+ }
9
+
10
+ code {
11
+ font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12
+ monospace;
13
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,9 @@
1
+ import React from 'react';
2
+ import { createRoot } from 'react-dom/client';
3
+ import './index.css';
4
+ import App from './App';
5
+ import 'bootstrap/dist/css/bootstrap.min.css';
6
+
7
+ const container = document.getElementById('root');
8
+ const root = createRoot(container); // createRoot(container!) if you use TypeScript
9
+ root.render(<App/>);
package/src/logo.svg ADDED
@@ -0,0 +1,7 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 841.9 595.3">
2
+ <g fill="#61DAFB">
3
+ <path d="M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3zm-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9zm-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9zm32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1zM421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32zm-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24 4.7 8 9.5 15.8 14.4 23.4zM420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32zm-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9zm-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6 0-15.7 22.9-35.6 58.3-50.6 8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2zM310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7zm237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1zm38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6zM320.8 78.4z"/>
4
+ <circle cx="420.9" cy="296.5" r="45.7"/>
5
+ <path d="M520.5 78.1z"/>
6
+ </g>
7
+ </svg>
@@ -0,0 +1,51 @@
1
+ import React from 'react';
2
+ import Oidc from "./vanilla/oidc";
3
+
4
+ export type Fetch = typeof window.fetch;
5
+ export interface ComponentWithOidcFetchProps {
6
+ fetch?: Fetch;
7
+ }
8
+ const defaultConfigurationName = "default";
9
+
10
+ const fetchWithToken = (fetch: Fetch, getAccessTokenInjected: () => string | null) => async (
11
+ url: RequestInfo,
12
+ options: RequestInit = { method: 'GET' }
13
+ ) => {
14
+ let headers = new Headers();
15
+ const optionTmp = { ...options };
16
+
17
+ if (optionTmp.headers) {
18
+ headers = !(optionTmp.headers instanceof Headers)
19
+ ? new Headers(optionTmp.headers)
20
+ : optionTmp.headers;
21
+ }
22
+
23
+ const accessToken = getAccessTokenInjected();
24
+ if (!headers.has('Accept')) {
25
+ headers.set('Accept', 'application/json');
26
+ }
27
+ if (accessToken) {
28
+ headers.set('Authorization', `Bearer ${accessToken}`);
29
+ if (!optionTmp.credentials) {
30
+ optionTmp.credentials = 'same-origin';
31
+ }
32
+ }
33
+ const newOptions = { ...optionTmp, headers };
34
+ return await fetch(url, newOptions);
35
+ };
36
+
37
+ export const withOidcFetch = (fetch:Fetch=null, configurationName=defaultConfigurationName) => (
38
+ WrappedComponent
39
+ ) => (props: ComponentWithOidcFetchProps) => {
40
+ const {fetch:newFetch} = useOidcFetch(fetch || props.fetch, configurationName)
41
+ return <WrappedComponent {...props} fetch={newFetch} />;
42
+ };
43
+
44
+
45
+ export const useOidcFetch =(fetch:Fetch=null, configurationName=defaultConfigurationName) =>{
46
+ const previousFetch = fetch || window.fetch;
47
+ const getOidc = Oidc.get;
48
+ const getAccessTokenInjected = () => { return getOidc(configurationName).tokens.accessToken; };
49
+ const newFetch = fetchWithToken(previousFetch, getAccessTokenInjected);
50
+ return { fetch:newFetch };
51
+ }
@@ -0,0 +1,165 @@
1
+ import React, {ComponentType, FC, PropsWithChildren, useEffect, useState} from 'react';
2
+ import Oidc, {OidcConfiguration} from './vanilla/oidc';
3
+ import OidcRoutes from './core/routes/OidcRoutes';
4
+ import {Authenticating, AuthenticateError, SessionLost, Loading, CallBackSuccess} from './core/default-component/index';
5
+ import ServiceWorkerNotSupported from "./core/default-component/ServiceWorkerNotSupported.component";
6
+ import AuthenticatingError from "./core/default-component/AuthenticateError.component";
7
+
8
+ export type oidcContext = {
9
+ getOidc: Function;
10
+ };
11
+
12
+ const defaultEventState = {name:"", data:null};
13
+
14
+ export type OidcProviderProps = {
15
+ callbackSuccessComponent?: ComponentType<any>;
16
+ callbackErrorComponent?: ComponentType<any>;
17
+ sessionLostComponent?: ComponentType<any>;
18
+ authenticatingComponent?: ComponentType<any>;
19
+ loadingComponent?: ComponentType<any>;
20
+ authenticatingErrorComponent?: ComponentType<any>;
21
+ serviceWorkerNotSupportedComponent?: ComponentType<any>;
22
+ configurationName?: string;
23
+ configuration?: OidcConfiguration;
24
+ children: any;
25
+ onSessionLost?: Function,
26
+ };
27
+
28
+ export type OidcSessionProps = {
29
+ configurationName: string;
30
+ loadingComponent: PropsWithChildren<any>;
31
+ };
32
+
33
+
34
+ const OidcSession : FC<PropsWithChildren<OidcSessionProps>> = ({loadingComponent, children, configurationName}) =>{
35
+ const [loading, setLoading] = useState(true);
36
+ const getOidc = Oidc.get;
37
+ const oidc = getOidc(configurationName);
38
+ useEffect(() => {
39
+ let isMounted = true;
40
+ if(oidc) {
41
+ oidc.tryKeepExistingSessionAsync().then(() => {
42
+ if (isMounted) {
43
+ setLoading(false);
44
+ }
45
+ })
46
+ }
47
+ return () => {
48
+ isMounted = false;
49
+ }
50
+ }, [oidc, configurationName]);
51
+ const LoadingComponent = loadingComponent;
52
+ return (
53
+ <>
54
+ {loading ? (
55
+ <LoadingComponent configurationName={configurationName}/>
56
+ ) : (
57
+ <>{children}</>
58
+ )}
59
+ </>
60
+ );
61
+ }
62
+
63
+ const Switch = ({isLoading, loadingComponent, children, configurationName}) => {
64
+ const LoadingComponent = loadingComponent;
65
+ if(isLoading){
66
+ return <LoadingComponent configurationName={configurationName}>{children}</LoadingComponent>;
67
+ }
68
+ return <>{children}</>;
69
+ }
70
+
71
+
72
+ export const OidcProvider : FC<PropsWithChildren<OidcProviderProps>> = ({ children,
73
+ configuration,
74
+ configurationName = "default",
75
+ callbackSuccessComponent = CallBackSuccess,
76
+ callbackErrorComponent = AuthenticateError,
77
+ authenticatingComponent = Authenticating,
78
+ loadingComponent = Loading,
79
+ serviceWorkerNotSupportedComponent = ServiceWorkerNotSupported,
80
+ authenticatingErrorComponent = AuthenticatingError,
81
+ sessionLostComponent=SessionLost,
82
+ onSessionLost=null}) => {
83
+ const getOidc =(configurationName="default") => {
84
+ return Oidc.getOrCreate(configuration, configurationName);
85
+ }
86
+ const [loading, setLoading] = useState(true);
87
+ const [event, setEvent] = useState(defaultEventState);
88
+ const [currentConfigurationName, setConfigurationName] = useState("default");
89
+
90
+ useEffect(() => {
91
+ const oidc = getOidc(configurationName);
92
+ const newSubscriptionId = oidc.subscriveEvents((name, data) => {
93
+ if(name == Oidc.eventNames.refreshTokensAsync_error){
94
+ if(onSessionLost != null){
95
+ onSessionLost();
96
+ return;
97
+ }
98
+ setEvent({name, data});
99
+ }
100
+ else if (name == Oidc.eventNames.loginAsync_begin
101
+ || name == Oidc.eventNames.loginCallbackAsync_end
102
+ || name == Oidc.eventNames.loginAsync_error
103
+ || name == Oidc.eventNames.loginCallbackAsync_error
104
+ ) {
105
+ setEvent({name, data});
106
+ } else if (name == Oidc.eventNames.service_worker_not_supported_by_browser && configuration.service_worker_only === true) {
107
+ setEvent({name, data});
108
+ }
109
+ });
110
+ setConfigurationName(configurationName);
111
+ setLoading(false);
112
+ return () => {
113
+ const previousOidc = getOidc(configurationName);
114
+ previousOidc.removeEventSubscription(newSubscriptionId);
115
+ setEvent(defaultEventState);
116
+ }
117
+ }, [configuration, configurationName]);
118
+
119
+
120
+ const SessionLostComponent = sessionLostComponent;
121
+ const AuthenticatingComponent = authenticatingComponent;
122
+ const LoadingComponent = loadingComponent;
123
+ const ServiceWorkerNotSupportedComponent = serviceWorkerNotSupportedComponent;
124
+ const AuthenticatingErrorComponent = authenticatingErrorComponent;
125
+
126
+ const isLoading = (loading || (currentConfigurationName != configurationName ));
127
+
128
+ let eventName = event.name;
129
+ switch(eventName){
130
+ case Oidc.eventNames.service_worker_not_supported_by_browser:
131
+ return <Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
132
+ <ServiceWorkerNotSupportedComponent configurationName={configurationName} />
133
+ </Switch>;
134
+ case Oidc.eventNames.loginAsync_begin:
135
+ return <Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
136
+ <AuthenticatingComponent configurationName={configurationName} />
137
+ </Switch>;
138
+ case Oidc.eventNames.loginAsync_error:
139
+ case Oidc.eventNames.loginCallbackAsync_error:
140
+ return <Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
141
+ <AuthenticatingErrorComponent configurationName={configurationName} />;
142
+ </Switch>;
143
+ case Oidc.eventNames.refreshTokensAsync_error:
144
+ return <Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
145
+ <SessionLostComponent configurationName={configurationName} />
146
+ </Switch>;
147
+ default:
148
+ return (
149
+ <Switch loadingComponent={LoadingComponent} isLoading={isLoading} configurationName={configurationName}>
150
+ <OidcRoutes redirect_uri={configuration.redirect_uri}
151
+ silent_redirect_uri={configuration.silent_redirect_uri}
152
+ callbackSuccessComponent={callbackSuccessComponent}
153
+ callbackErrorComponent={callbackErrorComponent}
154
+ authenticatingComponent={authenticatingComponent}
155
+ configurationName={configurationName}>
156
+ <OidcSession loadingComponent={LoadingComponent} configurationName={configurationName}>
157
+ {children}
158
+ </OidcSession>
159
+ </OidcRoutes>
160
+ </Switch>
161
+ );
162
+ }
163
+ };
164
+
165
+ export default OidcProvider;
@@ -0,0 +1,32 @@
1
+ import React, {useEffect, PropsWithChildren, FC} from 'react';
2
+
3
+ import Oidc, {StringMap} from "./vanilla/oidc";
4
+
5
+ export type OidcSecureProps = {
6
+ callbackPath?:string;
7
+ extras?:StringMap
8
+ configurationName?: string;
9
+ };
10
+
11
+ export const OidcSecure: FC<PropsWithChildren<OidcSecureProps>> = ({children, callbackPath=null, extras=null, configurationName="default"}) => {
12
+ const getOidc = Oidc.get;
13
+ const oidc = getOidc(configurationName);
14
+ useEffect(() => {
15
+ if(!oidc.tokens){
16
+ oidc.loginAsync(callbackPath, extras);
17
+ }
18
+ return () => {
19
+ }
20
+ }, [configurationName, callbackPath, extras])
21
+
22
+ if(!oidc.tokens){
23
+ return null;
24
+ }
25
+ return <>{children}</>
26
+ }
27
+
28
+ export const withOidcSecure = (
29
+ WrappedComponent, callbackPath=null
30
+ ) => (props) => {
31
+ return <OidcSecure callbackPath={callbackPath}><WrappedComponent {...props} /></OidcSecure>;
32
+ };
@@ -0,0 +1,112 @@
1
+ import React, {useEffect, useState} from 'react';
2
+ import Oidc, {StringMap} from "./vanilla/oidc";
3
+
4
+ const defaultConfigurationName = "default";
5
+ export const useOidc =(configurationName=defaultConfigurationName) =>{
6
+ const getOidc = Oidc.get;
7
+
8
+ const login = (callbackPath:string | undefined = undefined, extras:StringMap=null) => {
9
+ return getOidc(configurationName).loginAsync(callbackPath, extras);
10
+ };
11
+ const logout = (callbackPath: string | undefined = undefined) => {
12
+ return getOidc(configurationName).logoutAsync(callbackPath);
13
+ };
14
+
15
+ let isAuthenticated:boolean = false;
16
+ const oidc = getOidc(configurationName);
17
+ if(oidc){
18
+ isAuthenticated = getOidc(configurationName).tokens != null;
19
+ }
20
+
21
+ return { login, logout, isAuthenticated };
22
+ }
23
+
24
+ const accessTokenInitialState = {accessToken:null, accessTokenPayload:null};
25
+
26
+ const initTokens = (configurationName) => {
27
+ const getOidc = Oidc.get;
28
+ const oidc = getOidc(configurationName);
29
+ if(oidc.tokens) {
30
+ const tokens = oidc.tokens;
31
+ return {accessToken :tokens.accessToken, accessTokenPayload: tokens.accessTokenPayload }
32
+ }
33
+ return accessTokenInitialState;
34
+ }
35
+
36
+ export type OidcAccessToken = {
37
+ accessToken?: any,
38
+ accessTokenPayload?: any
39
+ }
40
+
41
+ export const useOidcAccessToken =(configurationName=defaultConfigurationName) =>{
42
+ const getOidc = Oidc.get;
43
+ const [state, setAccessToken] = useState<OidcAccessToken>(initTokens(configurationName));
44
+
45
+ useEffect(() => {
46
+ let isMounted = true;
47
+ const oidc = getOidc(configurationName);
48
+ if(oidc.tokens) {
49
+ const tokens = oidc.tokens;
50
+ setAccessToken({accessToken :tokens.accessToken, accessTokenPayload: tokens.accessTokenPayload });
51
+ }
52
+ const newSubscriptionId = oidc.subscriveEvents((name, data) => {
53
+ if(name == Oidc.eventNames.token_renewed
54
+ || name == Oidc.eventNames.token_aquired){
55
+ if(isMounted) {
56
+ const tokens = oidc.tokens;
57
+ setAccessToken(tokens != null ? {accessToken :tokens.accessToken, accessTokenPayload: tokens.accessTokenPayload } : accessTokenInitialState);
58
+ }
59
+ }
60
+ });
61
+ return () => {
62
+ isMounted = false;
63
+ oidc.removeEventSubscription(newSubscriptionId);
64
+ };
65
+ }, [configurationName]);
66
+ return state;
67
+ }
68
+
69
+ const idTokenInitialState = {idToken:null, idTokenPayload:null};
70
+
71
+ const initIdToken= (configurationName) =>{
72
+ const getOidc = Oidc.get;
73
+ const oidc = getOidc(configurationName);
74
+ if(oidc.tokens) {
75
+ const tokens = oidc.tokens;
76
+ return { idToken: tokens.idToken, idTokenPayload:tokens.idTokenPayload };
77
+ }
78
+ return idTokenInitialState
79
+ }
80
+
81
+ export type OidcIdToken = {
82
+ idToken?: any,
83
+ idTokenPayload?: any
84
+ }
85
+
86
+ export const useOidcIdToken =(configurationName= defaultConfigurationName) =>{
87
+ const getOidc = Oidc.get;
88
+ const [state, setIDToken] = useState<OidcIdToken>(initIdToken(configurationName));
89
+
90
+ useEffect(() => {
91
+ let isMounted = true;
92
+ const oidc = getOidc(configurationName);
93
+ if(oidc.tokens) {
94
+ const tokens = oidc.tokens;
95
+ setIDToken({idToken: tokens.idToken, idTokenPayload:tokens.idTokenPayload});
96
+ }
97
+ const newSubscriptionId = oidc.subscriveEvents((name, data) => {
98
+ if(name == Oidc.eventNames.token_renewed
99
+ || name == Oidc.eventNames.token_aquired){
100
+ if(isMounted) {
101
+ const tokens = oidc.tokens;
102
+ setIDToken(tokens != null ? {idToken: tokens.idToken, idTokenPayload:tokens.idTokenPayload} : idTokenInitialState);
103
+ }
104
+ }
105
+ });
106
+ return () => {
107
+ isMounted = false;
108
+ oidc.removeEventSubscription(newSubscriptionId);
109
+ };
110
+ }, [configurationName]);
111
+ return state;
112
+ }
@@ -0,0 +1,38 @@
1
+ import { useEffect, useState} from "react";
2
+ import Oidc from "./vanilla/oidc";
3
+
4
+ export enum OidcUserStatus {
5
+ Unauthenticated= 'Unauthenticated',
6
+ Loading = 'Loading user',
7
+ Loaded = 'User loaded',
8
+ LoadingError = 'Error loading user'
9
+ }
10
+
11
+ export type OidcUser = {
12
+ user: any,
13
+ status: OidcUserStatus
14
+ }
15
+
16
+ export const useOidcUser = (configurationName="default") => {
17
+ const [oidcUser, setOidcUser] = useState<OidcUser>({user: null, status: OidcUserStatus.Unauthenticated});
18
+
19
+ const oidc = Oidc.get(configurationName);
20
+ useEffect(() => {
21
+ let isMounted = true;
22
+
23
+ if(oidc && oidc.tokens) {
24
+ setOidcUser({...oidcUser, status: OidcUserStatus.Loading});
25
+ oidc.userInfoAsync()
26
+ .then((info) => {
27
+ if (isMounted) {
28
+ setOidcUser({user: info, status: OidcUserStatus.Loaded});
29
+ }
30
+ })
31
+ .catch(() => setOidcUser({...oidcUser, status: OidcUserStatus.LoadingError}));
32
+ }
33
+
34
+ return () => { isMounted = false };
35
+ }, []);
36
+
37
+ return {oidcUser: oidcUser.user, oidcUserLoadingState: oidcUser.status}
38
+ }
@@ -0,0 +1,13 @@
1
+ import * as React from 'react';
2
+ import {ComponentType} from "react";
3
+
4
+ const AuthenticatingError: ComponentType<any> = () => (
5
+ <div className="oidc-authenticating">
6
+ <div className="oidc-authenticating__container">
7
+ <h1 className="oidc-authenticating__title">Error authentication</h1>
8
+ <p className="oidc-authenticating__content">An error occurred during authentication.</p>
9
+ </div>
10
+ </div>
11
+ );
12
+
13
+ export default AuthenticatingError;
@@ -0,0 +1,13 @@
1
+ import * as React from 'react';
2
+ import {ComponentType} from "react";
3
+
4
+ const Authenticating : ComponentType<any> = () => (
5
+ <div className="oidc-authenticating">
6
+ <div className="oidc-authenticating__container">
7
+ <h1 className="oidc-authenticating__title">Authentication in progress</h1>
8
+ <p className="oidc-authenticating__content">You will be redirected to the login page.</p>
9
+ </div>
10
+ </div>
11
+ );
12
+
13
+ export default Authenticating;
@@ -0,0 +1,49 @@
1
+ import React, {useEffect, useState, ComponentType} from 'react';
2
+ import {getCustomHistory} from "../routes/withRouter";
3
+ import AuthenticatingError from "./AuthenticateError.component";
4
+ import Oidc from "../../vanilla/oidc";
5
+
6
+ export const CallBackSuccess: ComponentType<any> = () => (<div className="oidc-callback">
7
+ <div className="oidc-callback__container">
8
+ <h1 className="oidc-callback__title">Authentication complete</h1>
9
+ <p className="oidc-callback__content">You will be redirected to your application.</p>
10
+ </div>
11
+ </div>);
12
+
13
+ const CallbackManager: ComponentType<any> = ({callBackError, callBackSuccess, configurationName }) => {
14
+ const getOidc = Oidc.get;
15
+ const [error, setError] = useState(false);
16
+
17
+ const CallbackErrorComponent = callBackError || AuthenticatingError;
18
+ const CallbackSuccessComponent = callBackSuccess || CallBackSuccess;
19
+
20
+ useEffect(() => {
21
+ let isMounted = true;
22
+ const playCallbackAsync = async () => {
23
+
24
+ try {
25
+ const state = await getOidc(configurationName).loginCallbackWithAutoTokensRenewAsync();
26
+ if (state != null && isMounted) {
27
+ const history = getCustomHistory()
28
+ history.replaceState(decodeURIComponent(state))
29
+ }
30
+ } catch (error) {
31
+ if(isMounted) {
32
+ setError(true);
33
+ }
34
+ }
35
+ };
36
+ playCallbackAsync();
37
+ return () => {
38
+ isMounted = false;
39
+ };
40
+ },[]);
41
+
42
+ if(error){
43
+ return <CallbackErrorComponent configurationName={configurationName} />
44
+ }
45
+
46
+ return <CallbackSuccessComponent configurationName={configurationName} />;
47
+ };
48
+
49
+ export default CallbackManager;
@@ -0,0 +1,10 @@
1
+ import * as React from 'react';
2
+ import {ComponentType} from "react";
3
+
4
+ const Loading : ComponentType<any> = () => (
5
+ <span className="oidc-loading">
6
+ Loading
7
+ </span>
8
+ );
9
+
10
+ export default Loading;