feat(auth): Add Authorization Code Flow and new Keycloak recipes with new video tutorials (#4234)
Co-authored-by: Alireza <ar.sedghi@gmail.com>
This commit is contained in:
1 parent
998302eb85
commit
aefa6d94df
81 files changed
+6525
-471
No files matched your search
@@ -24,7 +24,7 @@ import {
|
||||
UserAuthenticationProvider,
|
||||
ToolboxProvider,
|
||||
} from '@ohif/ui';
|
||||
import { ThemeWrapper as ThemeWrapperNext, NotificationProvider } from '@ohif/ui-next';
|
||||
import { ThemeWrapper as ThemeWrapperNext } from '@ohif/ui-next';
|
||||
// Viewer Project
|
||||
// TODO: Should this influence study list?
|
||||
import { AppConfigProvider } from '@state';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import React from 'react';
|
||||
import React, { useEffect } from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
function CallbackPage({ userManager, onRedirectSuccess }) {
|
||||
@@ -6,10 +6,12 @@ function CallbackPage({ userManager, onRedirectSuccess }) {
|
||||
throw new Error(error);
|
||||
};
|
||||
|
||||
userManager
|
||||
.signinRedirectCallback()
|
||||
.then(user => onRedirectSuccess(user))
|
||||
.catch(error => onRedirectError(error));
|
||||
useEffect(() => {
|
||||
userManager
|
||||
.signinRedirectCallback()
|
||||
.then(user => onRedirectSuccess(user))
|
||||
.catch(error => onRedirectError(error));
|
||||
}, [userManager, onRedirectSuccess]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import React from 'react';
|
||||
import { useUserAuthentication } from '@ohif/ui';
|
||||
|
||||
export const PrivateRoute = ({ children, handleUnauthenticated }) => {
|
||||
|
||||
@@ -81,7 +81,7 @@ export default function buildModeRoutes({
|
||||
routes.push({
|
||||
path,
|
||||
children,
|
||||
private: true, // todo: all mode routes are private for now
|
||||
private: true,
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -138,18 +138,20 @@ const createRoutes = ({
|
||||
|
||||
const { userAuthenticationService } = servicesManager.services;
|
||||
|
||||
// Note: PrivateRoutes in react-router-dom 6.x should be defined within
|
||||
// a Route element
|
||||
// All routes are private by default and then we let the user auth service
|
||||
// to check if it is enabled or not
|
||||
// Todo: I think we can remove the second public return below
|
||||
return (
|
||||
<Routes>
|
||||
{allRoutes.map((route, i) => {
|
||||
return route.private === true ? (
|
||||
<Route
|
||||
key={i}
|
||||
exact
|
||||
path={route.path}
|
||||
element={
|
||||
<PrivateRoute handleUnauthenticated={() => userAuthenticationService.handleUnauthenticated()}>
|
||||
<PrivateRoute
|
||||
handleUnauthenticated={() => userAuthenticationService.handleUnauthenticated()}
|
||||
>
|
||||
<RouteWithErrorBoundary route={route} />
|
||||
</PrivateRoute>
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@ import { useEffect } from 'react';
|
||||
import { Route, Routes, useLocation, useNavigate } from 'react-router';
|
||||
import CallbackPage from '../routes/CallbackPage';
|
||||
import SignoutCallbackComponent from '../routes/SignoutCallbackComponent';
|
||||
import getUserManagerForOpenIdConnectClient from './getUserManagerForOpenIdConnectClient.js';
|
||||
import LegacyClient from './legacyOIDCClient';
|
||||
import NextClient from './nextOIDCClient';
|
||||
|
||||
function _isAbsoluteUrl(url) {
|
||||
return url.includes('http://') || url.includes('https://');
|
||||
@@ -43,7 +44,9 @@ const initUserManager = (oidc, routerBasename) => {
|
||||
post_logout_redirect_uri: _makeAbsoluteIfNecessary(post_logout_redirect_uri, baseUri),
|
||||
});
|
||||
|
||||
return getUserManagerForOpenIdConnectClient(openIdConnectConfiguration);
|
||||
const client = firstOpenIdClient.useAuthorizationCodeFlow ? NextClient: LegacyClient
|
||||
|
||||
return client(openIdConnectConfiguration);
|
||||
};
|
||||
|
||||
function LogoutComponent(props) {
|
||||
@@ -147,12 +150,18 @@ function OpenIdConnectRoutes({ oidc, routerBasename, userAuthenticationService }
|
||||
const location = useLocation();
|
||||
const { pathname, search } = location;
|
||||
|
||||
const redirect_uri = new URL(userManager.settings._redirect_uri).pathname.replace(
|
||||
const redirectURI = userManager.settings._redirect_uri ?? userManager.settings.redirect_uri;
|
||||
const silentRedirectURI =
|
||||
userManager.settings._silent_redirect_uri ?? userManager.settings.silent_redirect_uri;
|
||||
const postLogoutRedirectURI =
|
||||
userManager.settings._post_logout_redirect_uri ?? userManager.settings.post_logout_redirect_uri;
|
||||
|
||||
const redirect_uri = new URL(redirectURI).pathname.replace(
|
||||
routerBasename !== '/' ? routerBasename : '',
|
||||
''
|
||||
);
|
||||
const silent_refresh_uri = new URL(userManager.settings._silent_redirect_uri).pathname; //.replace(routerBasename,'')
|
||||
const post_logout_redirect_uri = new URL(userManager.settings._post_logout_redirect_uri).pathname; //.replace(routerBasename,'');
|
||||
const silent_refresh_uri = new URL(silentRedirectURI).pathname; //.replace(routerBasename,'')
|
||||
const post_logout_redirect_uri = new URL(postLogoutRedirectURI).pathname; //.replace(routerBasename,'');
|
||||
|
||||
// const pathnameRelative = pathname.replace(routerBasename,'');
|
||||
|
||||
|
||||
File renamed without changes.
@@ -0,0 +1,39 @@
|
||||
import { UserManager } from 'oidc-client-ts';
|
||||
|
||||
/**
|
||||
* Creates a userManager from oidcSettings
|
||||
* LINK: https://github.com/IdentityModel/oidc-client-js/wiki#configuration
|
||||
*
|
||||
* @param {Object} oidcSettings
|
||||
* @param {string} oidcSettings.authServerUrl,
|
||||
* @param {string} oidcSettings.clientId,
|
||||
* @param {string} oidcSettings.authRedirectUri,
|
||||
* @param {string} oidcSettings.postLogoutRedirectUri,
|
||||
* @param {string} oidcSettings.responseType,
|
||||
* @param {string} oidcSettings.extraQueryParams,
|
||||
*/
|
||||
export default function getUserManagerForOpenIdConnectClient(oidcSettings) {
|
||||
if (!oidcSettings) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!oidcSettings.authority || !oidcSettings.client_id || !oidcSettings.redirect_uri) {
|
||||
console.error('Missing required oidc settings: authority, client_id, redirect_uri');
|
||||
return;
|
||||
}
|
||||
|
||||
const settings = {
|
||||
...oidcSettings,
|
||||
// The next client always use the code flow with PKCE
|
||||
response_type: 'code',
|
||||
revokeTokensOnSignout: oidcSettings.revokeAccessTokenOnSignout ?? true,
|
||||
filterProtocolClaims: true,
|
||||
loadUserInfo: true,
|
||||
// the followings are default values in the lib so no need to set them
|
||||
// automaticSilentRenew: true,
|
||||
};
|
||||
|
||||
const userManager = new UserManager(settings);
|
||||
|
||||
return userManager;
|
||||
}
|
||||
Reference in new issue
Block a user