-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #469 from InseeFr/v2-auth-oidc-spa
Fix: auth with oidc-spa library
- Loading branch information
Showing
10 changed files
with
125 additions
and
341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,60 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { errorDictionary } from '../../../i18n'; | ||
import { createOidcClient } from '../../../utils/auth'; | ||
import { NONE, OIDC } from '../../../utils/constants'; | ||
import { listenActivity } from '../../../utils/events'; | ||
import { LoaderSimple } from 'components/shared/loader'; | ||
import { createOidcProvider } from 'oidc-spa/react'; | ||
import React from 'react'; | ||
import { OIDC, READ_ONLY } from '../../../utils/constants'; | ||
import { environment, oidcConf } from '../../../utils/read-env-vars'; | ||
import { LoaderSimple } from '../../shared/loader'; | ||
|
||
const dummyOidcClient = { | ||
isUserLoggedIn: true, | ||
getUser: () => ({ accessToken: null, sub: '' }), | ||
logout: () => (window.location.href = '/'), | ||
getTokens: () => ({ | ||
accessToken: null, | ||
idToken: null, | ||
refreshToken: null, | ||
refreshTokenExpirationTime: null, | ||
accessTokenExpirationTime: null, | ||
}), | ||
renewToken: () => {}, | ||
}; | ||
|
||
const { AUTH_TYPE, IDENTITY_PROVIDER, PORTAIL_URL } = environment; | ||
const { authUrl, realm, client_id } = oidcConf; | ||
|
||
function getCurrentSurvey(path) { | ||
const temp = path.split('/questionnaire/'); | ||
if (temp.length > 1) { | ||
const idQ = temp[1].slice(0, temp[1].indexOf('/')); | ||
return idQ.substr(0, idQ.indexOf('2')).toLowerCase(); | ||
} | ||
return ''; | ||
} | ||
|
||
export const getLogoutUrl = () => | ||
`${PORTAIL_URL}/${getCurrentSurvey(window.location.href)}`; | ||
|
||
const isReadOnlyMode = window.location.pathname.startsWith(`/${READ_ONLY}`); | ||
|
||
const getExtraQueryParams = () => { | ||
if (isReadOnlyMode) return { kc_idp_hint: IDENTITY_PROVIDER }; | ||
return {}; | ||
}; | ||
|
||
export const AuthContext = React.createContext(); | ||
|
||
export function AuthProvider({ children }) { | ||
const [oidcClient, setOidcClient] = useState(() => { | ||
switch (AUTH_TYPE) { | ||
case OIDC: | ||
return null; | ||
case NONE: | ||
return dummyOidcClient; | ||
default: | ||
throw new Error(errorDictionary.noAuthFile); | ||
} | ||
}); | ||
|
||
useEffect(() => { | ||
if (AUTH_TYPE !== OIDC) { | ||
return; | ||
} | ||
|
||
(async () => { | ||
const oidcClient = await createOidcClient({ | ||
url: oidcConf.authUrl, | ||
realm: oidcConf.realm, | ||
clientId: oidcConf.client_id, | ||
identityProvider: IDENTITY_PROVIDER, | ||
urlPortail: PORTAIL_URL, | ||
evtUserActivity: listenActivity, | ||
}); | ||
|
||
setOidcClient(oidcClient); | ||
})(); | ||
}, []); | ||
|
||
if (oidcClient === null) return <LoaderSimple />; | ||
if (AUTH_TYPE === OIDC) { | ||
const { OidcProvider } = createOidcProvider({ | ||
issuerUri: `${authUrl}/realms/${realm}`, | ||
clientId: client_id, | ||
getExtraQueryParams: getExtraQueryParams, | ||
// See above for other parameters | ||
}); | ||
return <OidcProvider fallback={<LoaderSimple />}>{children}</OidcProvider>; | ||
} | ||
|
||
return ( | ||
<AuthContext.Provider value={oidcClient}>{children}</AuthContext.Provider> | ||
<AuthContext.Provider value={dummyOidcClient}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.