Commit 0a8d8465 authored by Boiget Boiget's avatar Boiget Boiget

fix lint

parent 55cf7e58
Pipeline #2188 failed with stages
in 4 minutes and 7 seconds
......@@ -9,9 +9,7 @@ import i18n from 'meteor/universe:i18n';
import { isActive, getLabel } from '../utils';
import Groups from './groups';
import { addGroup, removeElement } from '../personalspaces/methods';
if (Meteor.isServer) {
import kcClient from '../kcClient';
}
import kcClient from '../kcClient';
export const favGroup = new ValidatedMethod({
name: 'groups.favGroup',
......
import AppRoles from './users/users';
import axios from 'axios';
import { Meteor } from 'meteor/meteor';
import AppRoles from './users/users';
class KeyCloakClient {
constructor() {
......@@ -23,9 +23,10 @@ class KeyCloakClient {
}
});
}
_authenticate() {
const adminUser = Meteor.settings.keycloak.adminUser;
const adminPassword = Meteor.settings.keycloak.adminPassword;
const { adminUser } = Meteor.settings.keycloak;
const { adminPassword } = Meteor.settings.keycloak;
return axios.post(
`${this.kcURL}/realms/master/protocol/openid-connect/token`,
`username=${adminUser}&password=${adminPassword}&grant_type=password&client_id=admin-cli`,
......@@ -36,6 +37,7 @@ class KeyCloakClient {
},
);
}
_refreshToken() {
return axios.post(
`${this.kcURL}/realms/master/protocol/openid-connect/token`,
......@@ -47,22 +49,27 @@ class KeyCloakClient {
},
);
}
_expire() {
this.token = null;
}
_expireRefresh() {
this.refreshToken = null;
}
_setToken(token, timeout) {
this.token = token;
// reset this.token 5 seconds before it expires
setTimeout(this._expire, (timeout - 5) * 1000);
}
_setRefreshToken(refresh_token, timeout) {
this.refreshToken = refresh_token;
_setRefreshToken(refreshToken, timeout) {
this.refreshToken = refreshToken;
// reset this.refreshToken 10 seconds before token expires
setTimeout(this._expireRefresh, (timeout - 10) * 1000);
}
_checkToken() {
if (this.token) return Promise.resolve(this.token);
if (this.refreshToken)
......@@ -80,6 +87,7 @@ class KeyCloakClient {
return newToken;
});
}
_getToken() {
return this._checkToken()
.then((newToken) => {
......@@ -94,6 +102,7 @@ class KeyCloakClient {
return null;
});
}
_ensureClientId(token) {
if (this.clientId === null) {
return axios
......@@ -113,27 +122,28 @@ class KeyCloakClient {
console.log(error.response && error.response.data ? error.response.data : error);
return null;
});
} else return Promise.resolve(this.clientId);
}
return Promise.resolve(this.clientId);
}
_ensureAdminsId(token) {
if (this.adminsGroupId === null) {
return this._getGroupId('admins', token).then((groupId) => {
if (groupId) {
this.adminsGroupId = groupId;
return this.adminsGroupId;
} else {
return this._addGroup('admins', token)
.then(() => this._getGroupId('admins', token))
.then((groupId) => {
this.adminsGroupId = groupId;
return this.adminsGroupId;
});
}
return this._addGroup('admins', token)
.then(() => this._getGroupId('admins', token))
.then((newGroupId) => {
this.adminsGroupId = newGroupId;
return this.adminsGroupId;
});
});
} else {
return Promise.resolve(this.adminsGroupId);
}
return Promise.resolve(this.adminsGroupId);
}
_getGroupId(name, token) {
return axios
.get(`${this.kcURL}/admin/realms/${this.kcRealm}/groups`, {
......@@ -147,6 +157,7 @@ class KeyCloakClient {
return kcGroup === undefined ? undefined : kcGroup.id;
});
}
_getRoleId(name, token) {
return axios
.get(`${this.kcURL}/admin/realms/${this.kcRealm}/clients/${this.clientId}/roles`, {
......@@ -160,6 +171,7 @@ class KeyCloakClient {
return kcRole === undefined ? undefined : kcRole.id;
});
}
_addRole(name, token) {
return axios.post(
`${this.kcURL}/admin/realms/${this.kcRealm}/clients/${this.clientId}/roles`,
......@@ -175,6 +187,7 @@ class KeyCloakClient {
},
);
}
_addGroup(groupName, token) {
return this._addRole(groupName, token).then(() => {
return axios
......@@ -210,6 +223,7 @@ class KeyCloakClient {
});
});
}
addGroup(group) {
AppRoles.filter((role) => role !== 'candidate').forEach((role) => {
const groupName = `${role}_${group.name}`;
......@@ -221,6 +235,7 @@ class KeyCloakClient {
});
});
}
_removeRole(name, token) {
return axios.delete(`${this.kcURL}/admin/realms/${this.kcRealm}/clients/${this.clientId}/roles/${name}`, {
headers: {
......@@ -229,6 +244,7 @@ class KeyCloakClient {
},
});
}
removeGroup(group) {
AppRoles.filter((role) => role !== 'candidate').forEach((role) => {
const groupName = `${role}_${group.name}`;
......@@ -239,20 +255,19 @@ class KeyCloakClient {
if (groupId === undefined) {
console.log(`Keycloak: could not find group ${groupName}`);
return null;
} else {
// delete associated role
return this._removeRole(groupName, token).then(() => {
// delete group
return axios
.delete(`${this.kcURL}/admin/realms/${this.kcRealm}/groups/${groupId}`, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
},
})
.then(() => console.log(`Keycloak: group ${groupName} removed`));
});
}
// delete associated role
return this._removeRole(groupName, token).then(() => {
// delete group
return axios
.delete(`${this.kcURL}/admin/realms/${this.kcRealm}/groups/${groupId}`, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
},
})
.then(() => console.log(`Keycloak: group ${groupName} removed`));
});
});
})
.catch((error) =>
......@@ -263,6 +278,7 @@ class KeyCloakClient {
);
});
}
setAdmin(userId) {
const groupName = `admins`;
const user = Meteor.users.findOne(userId);
......@@ -291,6 +307,7 @@ class KeyCloakClient {
console.log(`Keycloak: could not find Keycloak ID for user ${userId}`);
}
}
unsetAdmin(userId) {
const groupName = `admins`;
const user = Meteor.users.findOne(userId);
......@@ -319,6 +336,7 @@ class KeyCloakClient {
console.log(`Keycloak: could not find Keycloak ID for user ${userId}`);
}
}
setRole(userId, group, role) {
const groupName = `${role}_${group.name}`;
const user = Meteor.users.findOne(userId);
......@@ -347,6 +365,7 @@ class KeyCloakClient {
console.log(`Keycloak: could not find Keycloak ID for user ${userId}`);
}
}
unsetRole(userId, group, role) {
const groupName = `${role}_${group.name}`;
const user = Meteor.users.findOne(userId);
......@@ -377,4 +396,6 @@ class KeyCloakClient {
}
}
export default kcClient = new KeyCloakClient();
const kcClient = Meteor.isServer ? new KeyCloakClient() : null;
export default kcClient;
......@@ -7,7 +7,7 @@ import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { Roles } from 'meteor/alanning:roles';
import { isActive, getLabel, kcAPI } from '../../utils';
import { isActive, getLabel } from '../../utils';
import Groups from '../../groups/groups';
// initialize Meteor.users customizations
import AppRoles from '../users';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment