-
Notifications
You must be signed in to change notification settings - Fork 3
/
tenants-mapper.js
177 lines (166 loc) · 6.9 KB
/
tenants-mapper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* return a JSON claim structured as:
* "tenants": {
* "Tenant2": {
* "roles": [
* "tenant-admin",
* "ngsi:entity:op",
* "ngsi:subscription:read",
* "ngsi:registration:write",
* "ngsi:entity:read",
* "ngsi:registration:delete",
* "ngsi:subscription:write",
* "ngsi:registration:read",
* "ngsi:subscription:delete",
* "ngsi:entity:write",
* "ngsi:entity:delete"
* ],
* "groups": [
* "/Admin2",
* "/Group2"
* ],
* "id": "a6662e3f-0328-43d1-b5cf-25f3209c57e8"
* }
* },
* TODO: enable filter tenant using scope tenant:tenantId -> this requires to add scopes to client for each tenant, may be complex.
* TODO: enable return only tenant lists using scope only-tenants
* v0.6:
* - added support for roles attached to Tenant (be careful with that! all user
* in a tenant will have such roles)
* - added support for composed realm roles
*/
var HashMap = Java.type('java.util.HashMap');
var HashSet = Java.type('java.util.HashSet');
var ArrayList = Java.type('java.util.ArrayList');
var ModelToRepresentation = Java.type('org.keycloak.models.utils.ModelToRepresentation');
var groups = user.getGroups();
var tenants = new HashMap();
groups.forEach(scanTenant);
groups.forEach(scanGroups);
function scanTenant(group){
if ( isTenant(group)===true ) {
found = getTenant(group.getName());
if ( !found ) {
var groups = new ArrayList();
var roles = new ArrayList();
var tenantConfiguration = new HashMap();
var rep = ModelToRepresentation.toRepresentation(group, true);
if (rep.getRealmRoles())
addToArrayList(rep.getRealmRoles(), roles, "");
if( keycloakSession.getContext().getClient() ){
var clientId = keycloakSession.getContext().getClient().getClientId();
if (clientId == 'api') {
if(rep.getClientRoles().get('ngsi')) addToArrayList(rep.getClientRoles().get('ngsi'), roles, 'ngsi');
if(rep.getClientRoles().get('iot-agent')) addToArrayList(rep.getClientRoles().get('iot-agent'), roles, 'iot-agent');
}
if(rep.getClientRoles().get(clientId)) addToArrayList(rep.getClientRoles().get(clientId), roles, clientId);
} else {
var clients = realm.getClients();
for (i= 0; i<clients.size(); i++){
item = clients.get(i);
var clientId = item.getClientId();
if(rep.getClientRoles().get(clientId)) addToArrayList(rep.getClientRoles().get(clientId), roles, clientId);
}
}
tenantConfiguration.put("groups",groups);
tenantConfiguration.put("roles",roles);
tenants.put(group.getName(),tenantConfiguration);
}
} else if( group !== null && group.getParent() !== null ) {
scanTenant(group.getParent());
}
}
function scanGroups(group){
if ( isTenant(group)===false ) {
found = getTenant(getRoot(group).getName());
if ( found ) {
groups = found[1].get("groups");
roles = found[1].get("roles");
var rep = ModelToRepresentation.toRepresentation(group, true);
var cleanPath = rep.getPath().substring(found[0].length + 1);
current = getGroup(groups, cleanPath);
if (! current ){
groups.add(cleanPath);
if (rep.getRealmRoles())
addToArrayList(rep.getRealmRoles(), roles, "");
if( keycloakSession.getContext().getClient()){
var clientId = keycloakSession.getContext().getClient().getClientId();
if (clientId == 'api') {
if(rep.getClientRoles().get('ngsi')) addToArrayList(rep.getClientRoles().get('ngsi'), roles, 'ngsi');
if(rep.getClientRoles().get('iot-agent')) addToArrayList(rep.getClientRoles().get('iot-agent'), roles, 'iot-agent');
}
if(rep.getClientRoles().get(clientId)) addToArrayList(rep.getClientRoles().get(clientId), roles, clientId);
} else {
var clients = realm.getClients();
for (i= 0; i<clients.size(); i++){
item = clients.get(i);
var clientId = item.getClientId();
if(rep.getClientRoles().get(clientId)) addToArrayList(rep.getClientRoles().get(clientId), roles, clientId);
}
}
}
}
}
if( group.getParent() !== null ) {
scanGroups(group.getParent());
}
}
function getTenant(name){
if (name in tenants){
return [name, tenants[name]];
}
return null;
}
function getGroup(groups, name){
for (i= 0; i<groups.size(); i++){
item = groups.get(i);
if (item===name)
return item;
}
return null;
}
function getRoot(group){
if(group.getParent() !== null)
return getRoot(group.getParent());
return group;
}
function isTenant(group){
if (group.getParent()===null &&
group.getFirstAttribute("tenant") == "true"){
return true;
}
return false;
}
function addToArrayList(source, destination, clientid){
for (var i = 0; i < source.size(); i++){
if (clientid != "" && !destination.contains(clientid + ":" + source.get(i))) {
destination.add(clientid + ":" + source.get(i));
} else if (clientid == "" && !destination.contains(source.get(i))) {
destination.add(source.get(i));
var role = realm.getRole(source.get(i));
processComposedRoles(role.getCompositesStream().toArray(), destination);
}
}
}
function processComposedRoles(composedRoles, destination){
for (var k = 0; k < composedRoles.length; k++){
if(composedRoles[k].isClientRole()){
var roleName = composedRoles[k].getName();
var id = composedRoles[k].getContainer().getClientId();
if( keycloakSession.getContext().getClient()){
var clientId = keycloakSession.getContext().getClient().getClientId();
if (clientId == 'api') {
if(id == 'ngsi' && !destination.contains(id + ":" + roleName)) destination.add(id + ":" + roleName);
if(id == 'iot-agent' && !destination.contains(id + ":" + roleName)) destination.add(id + ":" + roleName);
}
if (id == clientId && !destination.contains(id + ":" + roleName)) destination.add(id + ":" + roleName);
} else if (!destination.contains(id + ":" + roleName)) destination.add(id + ":" + roleName);
} else {
var roleName = composedRoles[k].getName();
if (!destination.contains(roleName)) destination.add(roleName);
if (composedRoles[k].getCompositesStream().toArray().length>0)
processComposedRoles(composedRoles[k].getCompositesStream().toArray(), destination);
}
}
}
token.setOtherClaims("tenants", tenants);