ef-keycloak-connect 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/services/keycloakService.js +163 -3
package/package.json
CHANGED
|
@@ -913,6 +913,166 @@ class KeycloakService extends Keycloak {
|
|
|
913
913
|
|
|
914
914
|
}
|
|
915
915
|
|
|
916
|
+
//function to be used only in teams implementation. We give the list of ids of groups and it returns all its members and supervisors
|
|
917
|
+
async getGroupMembers(groupIds) {
|
|
918
|
+
|
|
919
|
+
return new Promise(async (resolve, reject) => {
|
|
920
|
+
|
|
921
|
+
let token;
|
|
922
|
+
let groupsData = [];
|
|
923
|
+
var URL = keycloakConfig["auth-server-url"] + 'realms/' + keycloakConfig.realm + '/protocol/openid-connect/token';
|
|
924
|
+
|
|
925
|
+
var config = {
|
|
926
|
+
method: 'post',
|
|
927
|
+
url: URL,
|
|
928
|
+
headers: {
|
|
929
|
+
'Accept': 'application/json',
|
|
930
|
+
'cache-control': 'no-cache',
|
|
931
|
+
'Content-Type': 'application/x-www-form-urlencoded'
|
|
932
|
+
},
|
|
933
|
+
data: {
|
|
934
|
+
client_id: keycloakConfig.CLIENT_ID,
|
|
935
|
+
username: keycloakConfig.USERNAME_ADMIN,
|
|
936
|
+
password: keycloakConfig.PASSWORD_ADMIN,
|
|
937
|
+
grant_type: keycloakConfig.GRANT_TYPE,
|
|
938
|
+
client_secret: keycloakConfig.credentials.secret
|
|
939
|
+
}
|
|
940
|
+
};
|
|
941
|
+
|
|
942
|
+
try {
|
|
943
|
+
|
|
944
|
+
let adminTokenResponse = await requestController.httpRequest(config, true);
|
|
945
|
+
token = adminTokenResponse.data.access_token;
|
|
946
|
+
|
|
947
|
+
if (groupIds.length > 0) {
|
|
948
|
+
|
|
949
|
+
config.method = 'get';
|
|
950
|
+
delete config.data;
|
|
951
|
+
delete config.url;
|
|
952
|
+
config.headers.Authorization = 'Bearer ' + token;
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
for (let i = 0; i < groupIds.length; i++) {
|
|
956
|
+
|
|
957
|
+
try {
|
|
958
|
+
|
|
959
|
+
let groupData = {};
|
|
960
|
+
|
|
961
|
+
let URL2 = keycloakConfig["auth-server-url"] + 'admin/realms/' + keycloakConfig.realm + '/groups/' + groupIds[i] + '/';
|
|
962
|
+
config.url = URL2;
|
|
963
|
+
let groupInfo = await requestController.httpRequest(config, true);
|
|
964
|
+
|
|
965
|
+
groupData.teamId = groupInfo.data.id;
|
|
966
|
+
groupData.teamName = groupInfo.data.name;
|
|
967
|
+
|
|
968
|
+
if (Object.keys(groupInfo.data.attributes).length == 0) {
|
|
969
|
+
|
|
970
|
+
groupData.supervisors = [];
|
|
971
|
+
} else {
|
|
972
|
+
|
|
973
|
+
let attributes = groupInfo.data.attributes;
|
|
974
|
+
|
|
975
|
+
if ('supervisor' in attributes) {
|
|
976
|
+
|
|
977
|
+
let supervisorList = attributes['supervisor'][0].split(",");
|
|
978
|
+
let supervisors = [];
|
|
979
|
+
|
|
980
|
+
for (let j = 0; j < supervisorList.length; j++) {
|
|
981
|
+
|
|
982
|
+
let URL3 = keycloakConfig["auth-server-url"] + 'admin/realms/' + keycloakConfig.realm + '/users?username=' + supervisorList[j] + '&exact=true';
|
|
983
|
+
config.url = URL3;
|
|
984
|
+
|
|
985
|
+
|
|
986
|
+
try {
|
|
987
|
+
|
|
988
|
+
let supervisorUser = await requestController.httpRequest(config, true);
|
|
989
|
+
|
|
990
|
+
if (supervisorUser.data.length > 0) {
|
|
991
|
+
supervisors.push({
|
|
992
|
+
supervisorId: supervisorUser.data[0].id,
|
|
993
|
+
supervisorName: supervisorUser.data[0].username
|
|
994
|
+
})
|
|
995
|
+
}
|
|
996
|
+
} catch (err) {
|
|
997
|
+
if (err.response) {
|
|
998
|
+
reject({
|
|
999
|
+
status: err.response.status,
|
|
1000
|
+
errorMessage: err.response.data
|
|
1001
|
+
});
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
reject(err);
|
|
1005
|
+
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
groupData.supervisors = supervisors;
|
|
1010
|
+
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
let URL4 = keycloakConfig["auth-server-url"] + 'admin/realms/' + keycloakConfig.realm + '/groups/' + groupIds[i] + '/members';
|
|
1016
|
+
config.url = URL4;
|
|
1017
|
+
let users = await requestController.httpRequest(config, true);
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
if (users.data.length > 0) {
|
|
1021
|
+
|
|
1022
|
+
let agents = users.data;
|
|
1023
|
+
agents = agents.map(agent => {
|
|
1024
|
+
return {
|
|
1025
|
+
agentId: agent.id,
|
|
1026
|
+
agentName: agent.username
|
|
1027
|
+
}
|
|
1028
|
+
});
|
|
1029
|
+
|
|
1030
|
+
groupData.agents = agents;
|
|
1031
|
+
|
|
1032
|
+
} else {
|
|
1033
|
+
groupData.agents = [];
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
groupsData.push(groupData);
|
|
1037
|
+
|
|
1038
|
+
} catch (err) {
|
|
1039
|
+
|
|
1040
|
+
if (err.response) {
|
|
1041
|
+
reject({
|
|
1042
|
+
status: err.response.status,
|
|
1043
|
+
errorMessage: err.response.data
|
|
1044
|
+
});
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
reject(err);
|
|
1048
|
+
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
resolve(groupsData);
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
resolve([]);
|
|
1057
|
+
|
|
1058
|
+
} catch (err) {
|
|
1059
|
+
|
|
1060
|
+
if (err.response) {
|
|
1061
|
+
reject({
|
|
1062
|
+
status: err.response.status,
|
|
1063
|
+
errorMessage: err.response.data
|
|
1064
|
+
});
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
reject(err);
|
|
1068
|
+
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
});
|
|
1072
|
+
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
|
|
916
1076
|
// this function requires comma separated list of roles in parameter e.g ["robot","human","customer"];
|
|
917
1077
|
getUsersByRole(keycloak_roles) {
|
|
918
1078
|
return new Promise(async (resolve, reject) => {
|
|
@@ -1073,14 +1233,14 @@ class KeycloakService extends Keycloak {
|
|
|
1073
1233
|
async createUser(username, password, token, userRoles) {
|
|
1074
1234
|
|
|
1075
1235
|
let assignRole = [];
|
|
1076
|
-
let assignGroups = ['
|
|
1236
|
+
let assignGroups = ['agents_permission', 'default'];
|
|
1077
1237
|
|
|
1078
1238
|
return new Promise(async (resolve, reject) => {
|
|
1079
1239
|
|
|
1080
1240
|
|
|
1081
1241
|
for (let group of assignGroups) {
|
|
1082
1242
|
|
|
1083
|
-
let URL2 = keycloakConfig["auth-server-url"] + 'admin/realms/' + keycloakConfig.realm + '/groups?search=' + group;
|
|
1243
|
+
let URL2 = keycloakConfig["auth-server-url"] + 'admin/realms/' + keycloakConfig.realm + '/groups?search=' + group + '&exact=true';
|
|
1084
1244
|
|
|
1085
1245
|
let config1 = {
|
|
1086
1246
|
method: 'get',
|
|
@@ -1121,7 +1281,7 @@ class KeycloakService extends Keycloak {
|
|
|
1121
1281
|
temporary: false
|
|
1122
1282
|
}
|
|
1123
1283
|
],
|
|
1124
|
-
groups: ["
|
|
1284
|
+
groups: ["agents_permission", "default"]
|
|
1125
1285
|
}
|
|
1126
1286
|
|
|
1127
1287
|
let config = {
|