feat(keycloak) Optional Keycloak integration via OpenId Connect (OIDC) for Identity and Access Management (#227)
This commit is contained in:
1 parent
03d0798459
commit
33755f7b8b
25 files changed
+294
-11
No files matched your search
@@ -0,0 +1,10 @@
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
|
||||
if (!Meteor.settings.public ||
|
||||
!Meteor.settings.public.custom ||
|
||||
!Meteor.settings.public.custom.keycloak) {
|
||||
|
||||
console.log('To use the ohif-user-keycloak package, you must add relevant Keycloak settings to Meteor.settings.public.custom.keycloak (client-side).');
|
||||
} else {
|
||||
require('../imports/client/index.js')
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<template name="keycloakLoginButton">
|
||||
<hr/>
|
||||
<button class="form-control btn js-login-keycloak">Login with Keycloak</button>
|
||||
</template>
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
import { Template } from 'meteor/templating';
|
||||
import { Router } from 'meteor/clinical:router';
|
||||
import { OHIF } from 'meteor/ohif:core';
|
||||
|
||||
import './keycloakLoginButton.html';
|
||||
|
||||
Template.keycloakLoginButton.events({
|
||||
'click .js-login-keycloak'() {
|
||||
Meteor.loginWithMeteorKeycloak({}, function(error) {
|
||||
if (error) {
|
||||
throw new Error(error);
|
||||
}
|
||||
|
||||
Router.go('/studylist');
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import { Accounts } from 'meteor/accounts-base';
|
||||
import { Meteor } from "meteor/meteor";
|
||||
|
||||
OHIF.user.getAccessToken = () => {
|
||||
const user = Meteor.user();
|
||||
if (!user) {
|
||||
return;
|
||||
}
|
||||
|
||||
return user.services.keycloak.accessToken;
|
||||
};
|
||||
|
||||
Accounts.onLogin(() => {
|
||||
Meteor.subscribe('user.services.keycloak', () => {
|
||||
sessionStorage.token = OHIF.user.getAccessToken();
|
||||
});
|
||||
});
|
||||
|
||||
Accounts.onLogout(() => {
|
||||
const authServerUrl = Meteor.settings.public.custom.keycloak.authServerUrl;
|
||||
const realm = Meteor.settings.public.custom.keycloak.realmName;
|
||||
const redirectUri = Meteor.absoluteUrl('login');
|
||||
const logoutUrl = `${authServerUrl}/realms/${realm}/protocol/openid-connect/logout?redirect_uri=${redirectUri}`;
|
||||
window.location = logoutUrl;
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
import './handlers';
|
||||
import './setup.js';
|
||||
import './components/keycloakLoginButton.js';
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
import { Router } from 'meteor/clinical:router';
|
||||
import { OHIF } from 'meteor/ohif:core';
|
||||
|
||||
Router.waitOn(function() {
|
||||
return [
|
||||
Meteor.subscribe('user.services.keycloak'),
|
||||
];
|
||||
}, { except: ['userLogin'] });
|
||||
|
||||
Router.onBeforeAction(function() {
|
||||
// Check if user is signed in
|
||||
if (!Meteor.userId() && !Meteor.loggingIn()) {
|
||||
this.redirect('userLogin');
|
||||
} else {
|
||||
this.next();
|
||||
}
|
||||
}, {
|
||||
except: ['userLogin', 'entrySignUp', 'forgotPassword', 'resetPassword']
|
||||
});
|
||||
|
||||
OHIF.user.additionalLoginButtons = OHIF.user.additionalLoginButtons || [];
|
||||
|
||||
OHIF.user.additionalLoginButtons.push({
|
||||
template: 'keycloakLoginButton'
|
||||
});
|
||||
|
||||
if (!Meteor.settings.public.userAuthenticationRoutesEnabled) {
|
||||
OHIF.log.error('Please set Meteor.settings.public.userAuthenticationRoutesEnabled=true');
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
import { ServiceConfiguration } from 'meteor/service-configuration';
|
||||
import { Accounts } from "meteor/accounts-base";
|
||||
|
||||
console.log('Keycloak settings were found! Enabling Keycloak integration.');
|
||||
|
||||
ServiceConfiguration.configurations.upsert(
|
||||
{ service: 'keycloak' },
|
||||
{
|
||||
$set: {
|
||||
"realm": Meteor.settings.public.custom.keycloak.realmName,
|
||||
"auth-server-url": Meteor.settings.public.custom.keycloak.authServerUrl,
|
||||
"auth_redirect_uri": Meteor.settings.keycloak.authRedirectUri,
|
||||
"ssl-required": Meteor.settings.keycloak.sslRequired,
|
||||
"resource": Meteor.settings.keycloak.clientId,
|
||||
"client_id": Meteor.settings.keycloak.clientId,
|
||||
"loginStyle": Meteor.settings.keycloak.loginStyle,
|
||||
"secret": Meteor.settings.keycloak.clientSecret,
|
||||
"realm-public-key": Meteor.settings.keycloak.realmPublicKey,
|
||||
"public-client": false,
|
||||
"use-resource-role-mappings": false,
|
||||
"bearer-only": false,
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Meteor.publish('user.services.keycloak', function() {
|
||||
const userId = this.userId;
|
||||
if (!userId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Meteor.users.find(userId, {
|
||||
fields: {
|
||||
'services.keycloak': 1
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Accounts.onLogout(({ user }) => {
|
||||
if (!user) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Erase any Keycloak token that exists
|
||||
Meteor.users.update(user._id, {
|
||||
$unset: {
|
||||
'services.keycloak': 1
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
Package.describe({
|
||||
name: 'ohif:user-keycloak',
|
||||
summary: 'OHIF Integration with Keycloak for Identity and Access Management',
|
||||
version: '0.0.1'
|
||||
});
|
||||
|
||||
Package.onUse(function(api) {
|
||||
api.versionsFrom('1.6');
|
||||
|
||||
api.use('templating');
|
||||
api.use('ecmascript');
|
||||
api.use('service-configuration');
|
||||
api.use('accounts-base');
|
||||
|
||||
// Our custom packages
|
||||
api.use('ohif:core');
|
||||
api.use('ohif:user');
|
||||
|
||||
api.use('mxab:keycloak-oauth@0.0.2');
|
||||
api.use('mxab:keycloak-loader@0.0.2');
|
||||
api.use('mxab:accounts-keycloak');
|
||||
|
||||
// Client imports
|
||||
api.mainModule('client/main.js', 'client');
|
||||
api.mainModule('server/main.js', 'server');
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
|
||||
if (!Meteor.settings.public ||
|
||||
!Meteor.settings.public.custom ||
|
||||
!Meteor.settings.public.custom.keycloak ||
|
||||
!Meteor.settings.keycloak) {
|
||||
|
||||
console.log('To use the ohif-user-keycloak package, you must add relevant Keycloak settings to Meteor.settings.keycloak (server-side).');
|
||||
} else {
|
||||
require('../imports/server/setup.js')
|
||||
}
|
||||
Reference in new issue
Block a user