|
1 | | -import Keycloak from "keycloak-js"; |
2 | | -import React from "react"; |
3 | | -import ReactDOM from "react-dom"; |
4 | | - |
5 | | -import { SnackbarProvider } from "notistack"; |
| 1 | +import React from 'react'; |
| 2 | +import ReactDOM from 'react-dom'; |
| 3 | +import initAuth from './auth'; |
6 | 4 | import { datastore } from "./datastore"; |
7 | 5 | import { ContextMainProvider } from "./ContextMain"; |
| 6 | +import { SnackbarProvider } from "notistack"; |
8 | 7 | import ValidationFramework from "./ValidationFramework"; |
9 | 8 |
|
10 | | -// We start by configuring the Keycloak javascript client |
11 | | -// It needs to know your app id in order to authenticate users for it |
12 | | -const keycloak = Keycloak({ |
13 | | - url: "https://iam.ebrains.eu/auth", |
14 | | - realm: "hbp", |
15 | | - clientId: "model-catalog", // TODO: change clientID to validation-framework, once client is registered |
16 | | -}); |
17 | | -const YOUR_APP_SCOPES = "team email profile"; // full list at https://iam.ebrains.eu/auth/realms/hbp/.well-known/openid-configuration |
18 | | - |
19 | | -// When ready, we initialise the keycloak client |
20 | | -// Once done, it will call our `checkAuth` function |
21 | | -window.addEventListener("DOMContentLoaded", initKeycloak); |
22 | | - |
23 | | -function initKeycloak() { |
24 | | - console.log("DOM content is loaded, initialising Keycloak client..."); |
25 | | - keycloak.init({ flow: "implicit" }).success(checkAuth).error(console.log); |
26 | | -} |
27 | | - |
28 | | -function checkAuth() { |
29 | | - console.log("Keycloak client is initialised, verifying authentication..."); |
30 | | - |
31 | | - // Is the user anonymous or authenticated? |
32 | | - const isAuthenticated = keycloak.authenticated; |
33 | | - const isAnonymous = !keycloak.authenticated; |
34 | | - // Is this app a standalone app, a framed app or a delegate? |
35 | | - const isParent = window.opener == null; |
36 | | - const isIframe = window !== window.parent; |
37 | | - const isMainFrame = window === window.parent; |
38 | | - const isStandaloneApp = isMainFrame && isParent; |
39 | | - const isFramedApp = isIframe && isParent; |
40 | | - const isDelegate = window.opener != null; |
41 | | - // Posting and listening to messages |
42 | | - const postMessageToParentTab = (message, parentTabOrigin) => |
43 | | - window.opener.postMessage(message, parentTabOrigin); |
44 | | - const listenToMessage = (callback) => |
45 | | - window.addEventListener("message", callback); |
46 | | - const AUTH_MESSAGE = "clb.authenticated"; |
47 | | - const myAppOrigin = window.location.origin; |
48 | | - // Manipulating URLs and tabs |
49 | | - const openTab = (url) => window.open(url); |
50 | | - const getCurrentURL = () => new URL(window.location); |
51 | | - const closeCurrentTab = () => window.close(); |
52 | | - |
53 | | - const login = (scopes) => keycloak.login({ scope: scopes }); |
54 | | - |
55 | | - // A standalone app should simply login if the user is not authenticated |
56 | | - // and do its business logic otherwise |
57 | | - if (isStandaloneApp) { |
58 | | - console.log("This is a standalone app..."); |
59 | | - if (isAnonymous) { |
60 | | - console.log("...which is not authenticated, starting login..."); |
61 | | - return login(YOUR_APP_SCOPES); |
62 | | - } |
63 | | - if (isAuthenticated) { |
64 | | - console.log( |
65 | | - "...which is authenticated, starting business logic..." |
66 | | - ); |
67 | | - return doBusinessLogic(keycloak); |
68 | | - } |
69 | | - } |
70 | | - |
71 | | - // A framed app should open a delegate to do the authentication for it and listen to its messages and verify them |
72 | | - // If the user is authenticated, it should do its business logic |
73 | | - if (isFramedApp) { |
74 | | - console.log("This is a framed app..."); |
75 | | - if (isAnonymous) { |
76 | | - console.log( |
77 | | - "...which is not authenticated, delegating to new tab..." |
78 | | - ); |
79 | | - listenToMessage(verifyMessage); |
80 | | - return openTab(getCurrentURL()); |
81 | | - } |
82 | | - if (isAuthenticated) { |
83 | | - console.log( |
84 | | - "...which is authenticated, starting business logic..." |
85 | | - ); |
86 | | - return doBusinessLogic(keycloak); |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - // A delegate should login if the user is not authenticated |
91 | | - // Otherwise, it should inform its opener that the user is authenticated and close itself |
92 | | - if (isDelegate) { |
93 | | - console.log("This is a delegate tab..."); |
94 | | - if (isAnonymous) { |
95 | | - console.log("...which is not authenticated, starting login..."); |
96 | | - return login(YOUR_APP_SCOPES); |
97 | | - } |
98 | | - if (isAuthenticated) { |
99 | | - console.log("...which is authenticated, warn parent and close..."); |
100 | | - postMessageToParentTab(AUTH_MESSAGE, myAppOrigin); |
101 | | - return closeCurrentTab(); |
102 | | - } |
103 | | - } |
104 | | -} |
105 | | - |
106 | | -function verifyMessage(event) { |
107 | | - console.log("Message receveived, verifying it..."); |
108 | | - |
109 | | - const AUTH_MESSAGE = "clb.authenticated"; |
110 | | - const receivedMessage = event.data; |
111 | | - const messageOrigin = event.origin; |
112 | | - const myAppOrigin = window.location.origin; |
113 | | - // const reload = () => window.location.reload(); // TODO: remove? |
114 | | - const login = (scopes) => keycloak.login({ scope: scopes }); |
115 | | - |
116 | | - // Stop if the message is not the auth message |
117 | | - if (receivedMessage !== AUTH_MESSAGE) return; |
118 | | - |
119 | | - // Stop if the message is not coming from our app origin |
120 | | - if (messageOrigin !== myAppOrigin) return; |
121 | | - |
122 | | - // Login otherwise |
123 | | - return login(YOUR_APP_SCOPES); |
124 | | -} |
125 | | - |
126 | | -function doBusinessLogic(auth) { |
| 9 | +function renderApp(auth) { |
127 | 10 | datastore.auth = auth; |
128 | 11 | ReactDOM.render( |
129 | 12 | <ContextMainProvider> |
130 | 13 | <SnackbarProvider maxSnack={3}> |
131 | 14 | <ValidationFramework auth={auth} /> |
132 | 15 | </SnackbarProvider> |
133 | 16 | </ContextMainProvider>, |
134 | | - document.getElementById("root") |
| 17 | + document.getElementById('root') |
135 | 18 | ); |
136 | | -} |
| 19 | +}; |
| 20 | + |
| 21 | +window.addEventListener('DOMContentLoaded', () => initAuth(renderApp)); |
0 commit comments