Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user store #4437

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 41 additions & 26 deletions platform/app/src/utils/OpenIdConnectRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import CallbackPage from '../routes/CallbackPage';
import SignoutCallbackComponent from '../routes/SignoutCallbackComponent';
import LegacyClient from './legacyOIDCClient';
import NextClient from './nextOIDCClient';
import PropTypes from 'prop-types';

function _isAbsoluteUrl(url) {
return url.includes('http://') || url.includes('https://');
Expand Down Expand Up @@ -44,7 +45,7 @@ const initUserManager = (oidc, routerBasename) => {
post_logout_redirect_uri: _makeAbsoluteIfNecessary(post_logout_redirect_uri, baseUri),
});

const client = firstOpenIdClient.useAuthorizationCodeFlow ? NextClient : LegacyClient
const client = firstOpenIdClient.useAuthorizationCodeFlow ? NextClient : LegacyClient;

return client(openIdConnectConfiguration);
};
Expand Down Expand Up @@ -96,29 +97,6 @@ function LoginComponent(userManager) {
function OpenIdConnectRoutes({ oidc, routerBasename, userAuthenticationService }) {
const userManager = initUserManager(oidc, routerBasename);

const getAuthorizationHeader = () => {
const user = userAuthenticationService.getUser();

// if the user is null return early, next time
// we hit this function we will have a user
if (!user) {
return;
}

return {
Authorization: `Bearer ${user.access_token}`,
};
};

const handleUnauthenticated = () => {
// Note: Don't await the redirect. If you make this component async it
// causes a react error before redirect as it returns a promise of a component rather than a component.
userManager.signinRedirect();

// return null because this is used in a react component
return null;
};

const navigate = useNavigate();

//for multi-tab logout
Expand All @@ -136,16 +114,37 @@ function OpenIdConnectRoutes({ oidc, routerBasename, userAuthenticationService }
return () => {
window.removeEventListener('storage', storageEventListener);
};
}, []);
}, [navigate]);

useEffect(() => {
const handleUnauthenticated = async () => {
await userManager.signinRedirect();

// return null because this is used in a react component
return null;
};

const getAuthorizationHeader = () => {
const user = userAuthenticationService.getUser();

// if the user is null return early, next time
// we hit this function we will have a user
if (!user) {
return;
}

return {
Authorization: `Bearer ${user.access_token}`,
};
};

userAuthenticationService.set({ enabled: true });

userAuthenticationService.setServiceImplementation({
getAuthorizationHeader,
handleUnauthenticated,
});
}, []);
}, [userAuthenticationService, userManager]);

const oidcAuthority = oidc[0].authority;

Expand All @@ -171,6 +170,16 @@ function OpenIdConnectRoutes({ oidc, routerBasename, userAuthenticationService }
sessionStorage.setItem('ohif-redirect-to', JSON.stringify({ pathname, search }));
}

useEffect(() => {
userManager.getUser().then(user => {
if (user && !user.expired) {
userAuthenticationService.setUser(user);
} else {
userManager.signinRedirect();
}
});
}, [userAuthenticationService, userManager]);

return (
<Routes>
<Route
Expand Down Expand Up @@ -227,4 +236,10 @@ function OpenIdConnectRoutes({ oidc, routerBasename, userAuthenticationService }
);
}

OpenIdConnectRoutes.propTypes = {
oidc: PropTypes.array.isRequired,
routerBasename: PropTypes.string.isRequired,
userAuthenticationService: PropTypes.object.isRequired,
};

export default OpenIdConnectRoutes;
3 changes: 2 additions & 1 deletion platform/app/src/utils/legacyOIDCClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UserManager } from 'oidc-client';
import { UserManager, WebStorageStateStore } from 'oidc-client';

/**
* Creates a userManager from oidcSettings
Expand All @@ -23,6 +23,7 @@ export default function getUserManagerForOpenIdConnectClient(oidcSettings) {
revokeAccessTokenOnSignout: true,
filterProtocolClaims: true,
loadUserInfo: true,
userStore: new WebStorageStateStore({ store: window.localStorage }),
};

const userManager = new UserManager(settings);
Expand Down
3 changes: 2 additions & 1 deletion platform/app/src/utils/nextOIDCClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UserManager } from 'oidc-client-ts';
import { UserManager, WebStorageStateStore } from 'oidc-client-ts';

/**
* Creates a userManager from oidcSettings
Expand Down Expand Up @@ -29,6 +29,7 @@ export default function getUserManagerForOpenIdConnectClient(oidcSettings) {
revokeTokensOnSignout: oidcSettings.revokeAccessTokenOnSignout ?? true,
filterProtocolClaims: true,
loadUserInfo: true,
userStore: new WebStorageStateStore({ store: window.localStorage }),
// the followings are default values in the lib so no need to set them
// automaticSilentRenew: true,
};
Expand Down