ohif-viewer/Packages/ohif-user-oidc/imports/client/oidcUserManager.js
Egor Lezhnin 9486522552 feat(google-cloud): Add support for Google Cloud Healthcare API DICOM Store Switch (#325)
* NOTASK: PoC for loading data from Healthcare API

* NOTASK: PoC for loading data from Healthcare API fix

* Add sample dialog for loading test dicom-picker

* Fix incorrect promise resolving

* NOTASK: PoC for loading dicom store

* Add web-components as git submodules. (Probably will be changed later)

* Add rough dicomStore picker

* Add dataset selector

* NOTASK: QIDO metadata first draft

* NOTASK: Not Implemented error for color images

* NOTASK: clean-up

* Dicom files uploader intermediate version

* NOTASK: Accept header

* NOTASK: docker configs

* NOTASK: fix

* NOTASK: howTo

* Add draft of DICOM uploader

* Add missing files

* Fix error with missing clientId

* Update submodule

* NOTASK: HowTo

* NOTASK: config for dev

* NOTASK: new docker file

* clean-up

* Update GCP web-components

* Add integration with async web components

* Fix errors in nginx.conf

* Structured reports views basic implementation

* NOTASK: qido -> wado

* NOTASK: config fix

* NOTASK: dirty copy-paste implementation

* Add "Change dicom store" button.
Add an ability to clear date.
Fix bug, when OAuth dosn't work if URL is not /

* Fix studylist filtering

* Fix dockerfile

* NOTASK: meteor update

* NOTASK: fix of package structure

* NOTASK: merge fix

* improvements

Move check for now SR into SR modal
Use simple button to open SR modal
Remove button styling
cleanup

* copy SR data retrieval to wado

* make search of SR simpler

* Codestyle fix

* NOTASK: palette color error message

* Fix date filter.
Fix server settings on first loading.
Add SR and PS buttons

* NOTASK: PS is now hidden by default

* NOTASK: dirty solution

* NOTASK:  new wadoimage lib version

* Fix PS and SR buttons

* Make modal dialogs vertically centered

* Update web components. Quick UI fixes

* Add missing files to the previous commit

* NOTASK:  show-hide for PS

* NOTASK:  show-hide for PS

* NOTASK:  fix of min-max PixelValue bug

* Add "sign out" functionality for Google OAuth. Update web-components.

* intermediate commit

* Add demo signin page

* Beautifying demo sign in page

* NOTASK:  merge

* NOTASK:  clean-up

* Use npm for getting healthcare-api-adapter instead of git submodule.

* NOTASK:  clean-up

* meteor update

* clean-up

* clean-up

* Clean up code

* Update healthcare-api-adapter version. Add location to dicom store path. Fix buttons style.

* Downgrade meteor and packages to master version. Use meteor-build-client-fixed2
2019-03-22 16:42:26 +01:00

110 lines
3.4 KiB
JavaScript

import { Meteor } from "meteor/meteor";
import { OHIF } from 'meteor/ohif:core';
import Oidc from 'oidc-client';
Oidc.Log.logger = console;
Oidc.Log.level = Oidc.Log.DEBUG;
const { oidc } = Meteor.settings.public.custom;
if (oidc.length > 1) {
OHIF.log.warn("Only one OpenID Connect provider is currently supported. Using the first item in the Meteor.settings.public.custom.oidc array.")
}
const oidcClient = oidc[0];
const redirect_uri = Meteor.absoluteUrl(oidcClient.authRedirectUri);
const silent_redirect_uri = Meteor.absoluteUrl('/packages/ohif_user-oidc/public/silent-refresh.html');
function httpGetSync(theUrl)
{
// TODO: consider making it async somehow
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.send( null );
return xmlHttp.responseText;
}
const id = oidcClient.clientId || httpGetSync( Meteor.absoluteUrl('/gcloud-client-id'));
const settings = {
authority: oidcClient.authServerUrl,
client_id: id,
redirect_uri,
silent_redirect_uri,
post_logout_redirect_uri: Meteor.absoluteUrl(oidcClient.postLogoutRedirectUri),
response_type: oidcClient.responseType || 'id_token token',
scope: oidcClient.scope || 'email profile openid https://www.googleapis.com/auth/cloud-platform.read-only https://www.googleapis.com/auth/cloud-healthcare', // seems like scope is not loaded from settings Note: Request must have scope 'openid' to be considered an OpenID Connect request
automaticSilentRenew: true,
revokeAccessTokenOnSignout: true,
};
const itemName = `oidc.user:${oidcClient.authServerUrl}:${id}`;
function getTokenFromStorage() {
const userDataJSON = sessionStorage.getItem(itemName);
const user = JSON.parse(userDataJSON);
if (!user) {
return;
}
return user.access_token;
}
OHIF.user.getAccessToken = function oidcGetAccessToken() {
if (!OHIF.user.userLoggedIn) {
throw new Error('User is not logged in.');
}
return getTokenFromStorage();
};
OHIF.user.getOidcStorageKey = function () {
return itemName;
}
OHIF.user.getOidcRedirectUri = function () {
return oidcClient.authRedirectUri;
}
OHIF.user.login = function oidcLogin() {
oidcUserManager.signinRedirect({redirect_uri});
}
OHIF.user.logout = function oidcLogout() {
const config = JSON.parse(sessionStorage.getItem(itemName) || null);
if (oidcClient.revokeUrl && config && config.access_token) {
// OIDC from Google doesn't support signing out for some reason
// so we revoke the token manually
sessionStorage.removeItem(itemName);
const revokeUrl = oidcClient.revokeUrl + config.access_token;
fetch(revokeUrl).catch(()=>{}).then(() => location.assign(oidcClient.postLogoutRedirectUri || '/'));
} else {
// simple oidc signout behavior
oidcUserManager.signoutRedirect();
}
}
OHIF.user.userLoggedIn = () => !!getTokenFromStorage();
// See https://github.com/IdentityModel/oidc-client-js/wiki for more information
const oidcUserManager = new Oidc.UserManager(settings);
const LOGIN_REQUIRED = 'login_required'
function handleSilentRenewError(error) {
console.error(error);
if (error.error === LOGIN_REQUIRED) {
OHIF.user.logout();
}
}
oidcUserManager.events.addSilentRenewError(handleSilentRenewError);
oidcUserManager.startSilentRenew();
oidcUserManager.events.addAccessTokenExpired(function(){
OHIF.user.logout();
});
export default oidcUserManager;