diff --git a/configuration/build-configuration.json b/configuration/build-configuration.json deleted file mode 100644 index c06fb883..00000000 --- a/configuration/build-configuration.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "authenticationType": "${AUTHENTICATION_TYPE}", - "_authenticationType_comment_": "Authentication type : OIDC or NONE", - "apiUrl": "${API_URL}", - "portail": "${PORTAIL_URL}" -} diff --git a/configuration/configuration.json b/configuration/configuration.json index c06fb883..9bce8478 100644 --- a/configuration/configuration.json +++ b/configuration/configuration.json @@ -2,5 +2,6 @@ "authenticationType": "${AUTHENTICATION_TYPE}", "_authenticationType_comment_": "Authentication type : OIDC or NONE", "apiUrl": "${API_URL}", - "portail": "${PORTAIL_URL}" + "portail": "${PORTAIL_URL}", + "identityProvider": "${IDENTITY_PROVIDER}" } diff --git a/configuration/oidc.json b/configuration/oidc.json deleted file mode 100644 index 8e79c004..00000000 --- a/configuration/oidc.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "origin": "my_origin", - "isEnabled": true, - "config": { - "authority": " ${AUTHORITY}", - "client_id": "${CLIENT_ID}", - "redirect_uri": "my_origin/authentication/callback", - "response_type": "code", - "post_logout_redirect_uri": "my_origin/", - "scope": "openid profile email", - "silent_redirect_uri": "my_origin/authentication/silent_callback", - "automaticSilentRenew": true, - "loadUserInfo": true - } -} diff --git a/package.json b/package.json index e01af2fc..71142532 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "stromae", - "version": "2.2.14", + "version": "2.3.1", "description": "Web application for the management of questionnaires powered by Lunatic", "repository": { "type": "git", diff --git a/public/build-configuration.json b/public/build-configuration.json deleted file mode 100644 index c06fb883..00000000 --- a/public/build-configuration.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "authenticationType": "${AUTHENTICATION_TYPE}", - "_authenticationType_comment_": "Authentication type : OIDC or NONE", - "apiUrl": "${API_URL}", - "portail": "${PORTAIL_URL}" -} diff --git a/public/configuration.json b/public/configuration.json index c06fb883..3daba3af 100644 --- a/public/configuration.json +++ b/public/configuration.json @@ -1,6 +1,7 @@ { - "authenticationType": "${AUTHENTICATION_TYPE}", + "authenticationType": "NONE", "_authenticationType_comment_": "Authentication type : OIDC or NONE", "apiUrl": "${API_URL}", - "portail": "${PORTAIL_URL}" + "portail": "${PORTAIL_URL}", + "identityProvider": "${IDENTITY_PROVIDER}" } diff --git a/scripts/.env b/scripts/.env index a3505631..d2270316 100644 --- a/scripts/.env +++ b/scripts/.env @@ -1,5 +1,7 @@ AUTHENTICATION_TYPE=NONE API_URL=https://stromae-bo.dev.insee.io PORTAIL_URL= +REALM= AUTHORITY= -CLIENT_ID= \ No newline at end of file +CLIENT_ID= +IDENTITY_PROVIDER= \ No newline at end of file diff --git a/scripts/env.sh b/scripts/env.sh index 8cd7cd6e..49b14c01 100644 --- a/scripts/env.sh +++ b/scripts/env.sh @@ -19,10 +19,6 @@ do done < .env envsubst < "./configuration.json" > "configuration.temp" -envsubst < "./build-configuration.json" > "build-configuration.temp" -envsubst < "./build-oidc.json" > "build-oidc.temp" -envsubst < "./oidc.json" > "oidc.temp" +envsubst < "./keycloak.json" > "keycloak.temp" mv configuration.temp configuration.json -mv build-configuration.temp build-configuration.json -mv build-oidc.temp build-oidc.json -mv oidc.temp oidc.json +mv keycloak.temp keycloak.json diff --git a/src/App.js b/src/App.js index 6d413cfb..a639263f 100644 --- a/src/App.js +++ b/src/App.js @@ -1,10 +1,10 @@ -import React, { useEffect, useState } from 'react'; -import { BrowserRouter } from 'react-router-dom'; -import { ErrorBoundary } from 'react-error-boundary'; import { AuthProvider } from 'components/auth'; import { Router } from 'components/router'; -import { StyleProvider } from 'components/style'; import { ErrorFallback } from 'components/shared/error'; +import { StyleProvider } from 'components/style'; +import React, { useEffect, useState } from 'react'; +import { ErrorBoundary } from 'react-error-boundary'; +import { BrowserRouter } from 'react-router-dom'; import { getConfiguration } from 'utils/configuration'; import './App.css'; @@ -30,10 +30,7 @@ const App = () => { > {configuration && ( - + diff --git a/src/components/auth/provider/component.js b/src/components/auth/provider/component.js index a4a5d309..ab43cc5a 100644 --- a/src/components/auth/provider/component.js +++ b/src/components/auth/provider/component.js @@ -1,14 +1,21 @@ -import React, { useEffect, useState } from 'react'; -import { OIDC, NONE } from 'utils/constants'; +import { AppContext } from 'App'; import { LoaderSimple } from 'components/shared/loader'; -import { getOidc } from 'utils/configuration'; import { errorDictionary } from 'i18n'; -import { createKeycloakOidcClient } from 'utils/keycloak'; +import React, { useContext, useEffect, useState } from 'react'; +import { getOidc } from 'utils/configuration'; +import { NONE, OIDC } from 'utils/constants'; import { listenActivity } from 'utils/events'; +import { createKeycloakOidcClient } from 'utils/keycloak'; export const AuthContext = React.createContext(); -const AuthProvider = ({ authType, urlPortail, children }) => { +const AuthProvider = ({ children }) => { + const { + authenticationType: authType, + portail: urlPortail, + identityProvider, + } = useContext(AppContext); + const [oidcClient, setOidcClient] = useState(() => { switch (authType) { case OIDC: @@ -32,13 +39,14 @@ const AuthProvider = ({ authType, urlPortail, children }) => { url: oidcConf['auth-server-url'], realm: oidcConf['realm'], clientId: oidcConf['resource'], + identityProvider: identityProvider, urlPortail, evtUserActivity: listenActivity, }); setOidcClient(oidcClient); })(); - }, [authType, urlPortail]); + }, [authType, identityProvider, urlPortail]); if (oidcClient === null) return ; diff --git a/src/utils/configuration/get-configuration.js b/src/utils/configuration/get-configuration.js index 2df6b5e5..94155d42 100644 --- a/src/utils/configuration/get-configuration.js +++ b/src/utils/configuration/get-configuration.js @@ -2,8 +2,7 @@ import { getEnvVar } from './env'; const basePath = `${window.location.origin}`; -const nameConfigFile = - getEnvVar('NAME_CONFIG_FILE') ?? 'build-configuration.json'; +const nameConfigFile = getEnvVar('NAME_CONFIG_FILE') ?? 'configuration.json'; const nameOidcFile = getEnvVar('NAME_OIDC_FILE') ?? 'keycloak.json'; diff --git a/src/utils/keycloak/keycloakOidcClient.js b/src/utils/keycloak/keycloakOidcClient.js index 3bdad0f5..07d9d85e 100644 --- a/src/utils/keycloak/keycloakOidcClient.js +++ b/src/utils/keycloak/keycloakOidcClient.js @@ -1,4 +1,5 @@ import Keycloak from 'keycloak-js'; +import { READ_ONLY } from 'utils/constants'; const getCurrentSurvey = (path) => { const temp = path.split('/questionnaire/'); @@ -13,6 +14,7 @@ export const createKeycloakOidcClient = async ({ url, realm, clientId, + identityProvider, urlPortail, evtUserActivity, }) => { @@ -27,7 +29,13 @@ export const createKeycloakOidcClient = async ({ .catch((error) => error); const login = async () => { - await keycloakInstance.login({ redirectUri: window.location.href }); + await keycloakInstance.login({ + redirectUri: window.location.href, + // Readonly mode : Internal user login + idpHint: window.location.pathname.startsWith(`/${READ_ONLY}`) + ? identityProvider + : null, + }); return new Promise(() => {}); }; diff --git a/src/utils/oidc/build-configuration.js b/src/utils/oidc/build-configuration.js deleted file mode 100644 index 739ec7b6..00000000 --- a/src/utils/oidc/build-configuration.js +++ /dev/null @@ -1,36 +0,0 @@ -const LAST_SURVEY = 'last-survey'; - -const getCurrentSurvey = (path) => { - if (!path.startsWith('/authentication')) { - const temp = path.split('/questionnaire/'); - if (temp.length > 1) { - const idQ = temp[1].slice(0, temp[1].indexOf('/')); - const survey = idQ.substr(0, idQ.indexOf('2')).toLowerCase(); - window.localStorage.setItem(LAST_SURVEY, survey); - return survey; - } - return ''; - } - return window.localStorage.getItem(LAST_SURVEY) || ''; -}; - -const getKc_idp_hintActive = (path) => { - if (!path.startsWith('/read-only')) { - return ''; - } - return '{"kc_idp_hint":"sso-insee"}'; -}; - -export const buildOidcConfiguration = ({ oidcConf, conf }) => { - const { origin, pathname } = window.location; - const { portail } = conf; - const config = { - ...oidcConf, - redirect_uri: `${origin}/authentication/callback`, - response_type: 'code', - post_logout_redirect_uri: `${portail}/${getCurrentSurvey(pathname)}`, - silent_redirect_uri: `${origin}/authentication/silent_callback`, - extraQueryParams: `${getKc_idp_hintActive(pathname)}`, - }; - return config; -};