本文介紹了使用Keyloak腳本映射器聚合聲明中的角色屬性的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我們有一個Keyloak腳本映射器來將角色屬性添加到ID令牌。目標是聚合角色屬性中的可用值。映射器如下所示:
/**
* Merge with concatenation for the values of attributes obtained
* from the token with the attributes obtained from the roles. If for
* example a group and a role have the same attribute key, the values
* for that key from the group and role will be concatenated.
*
* Known limitations:
* When only roles have a certain attribute, and not a group, the
* mapper only uses the first role it can find. Bug was difficult to
* fix because state in the variable currentClaims seems to be
* persisted over multiple calls.
* Workaround: Also add this specific attribute to a group with a
* dummy value.
*
* NOTE: there is no role attribute mapper out-of-the-box in
* Keycloak.
*
* Available variables in script:
* user - the current user
* realm - the current realm
* token - the current token
* userSession - the current userSession
* keycloakSession - the current keycloakSession
*
* Documentation on available variables:
* https://stackoverflow.com/a/52984849
*/
var currentClaims = {};
token.getOtherClaims().forEach(function(k, v) {
currentClaims[k] = v;
});
function isMultiValued(v) {
// From experience, multivalued attribute values are sometimes
// Arrays and sometimes Objects. Thus look for negative case:
// anything other than a string is multivalued.
return !(typeof v === 'string' || v instanceof String);
}
function addToList(l, values) {
for each(var v in values) {
l.add(v);
}
return l;
}
function toStringArray(arr) {
return Java.to(arr, "java.lang.String[]");
}
user.getRealmRoleMappings().forEach(function(roleModel) {
roleModel.getAttributes().forEach(function(k, v) {
var currentValue = currentClaims[k];
if (k in currentClaims) {
if (!isMultiValued(currentValue)) {
v = toStringArray([currentValue].concat(v));
} else {
v = addToList(currentValue, v);
}
}
currentClaims[k] = v; // <= to also aggregate over roles!
token.setOtherClaims(k, v);
});
});
添加了currentClaims[k] = v
的部分以聚合角色中的可用值,因此,如果兩個角色包含相同屬性,則它們的值也會聚合。
例如,如果我們有一個用戶的角色a和b的屬性foo的值分別為1和2,則我們預計ID令牌將包含值為1和2的foo的聲明。
user:
role a foo -> 1
role b foo -> 2
expected ID token:
foo -> [1, 2]
但在當前代碼中,currentClaims
變量似乎在多次調用函數時保持狀態。每次檢查ID令牌時,都會將更多2
的值添加到令牌中,從而導致類似[1, 2, 2, ..., 2]
的foo聲明,每次檢索令牌時都會添加越來越多的2
。我嘗試將整個調用包裝在一個函數中,以便可能在調用之間丟棄狀態,但無濟于事。結果如下:
為什么在多個呼叫期間保留狀態?有沒有辦法也聚合角色屬性的值?
推薦答案
為什么在多個調用期間保留狀態?
未知。也許是和納肖恩有關的事。
[i]有沒有辦法也聚合角色屬性的值?
要不保持聚合值,請在addToList
函數中檢查它是否已經存在(解決方法):
function addToList(l, values) {
for each(var v in values) {
if (!l.contains(v)) {
l.add(v);
}
}
return l;
}
并通過保持某種狀態進行聚合:
var newClaims = {};
newClaims = currentClaims;
user.getRealmRoleMappings().forEach(function(roleModel) {
roleModel.getAttributes().forEach(function(k, v) {
var currentValue = newClaims[k];
if (k in newClaims) {
if (isMultiValued(currentValue)) {
v = addToList(currentValue, v);
}
}
newClaims[k] = v;
});
});
token.setOtherClaims("new-claims", newClaims);
這會將new-claims
字段中的所有索賠放入JWT。
/編輯:
注意:addToList
中的此JavaScript/Java互操作存在問題(如更改角色的實際屬性.)。最好使用純JavaScript進行連接,然后再轉換回Java:
function toStringArray(arr) {
return Java.to(arr, "java.lang.String[]");
}
Array.prototype.includes = function(obj) {
var i = this.length;
while (i--) {
if (this[i] === obj) {
return true;
}
}
return false;
}
function addToList(l, values) {
// Add values to new instance to avoid magical addition to
// current values in role attributes
var arr = [];
for each(var v in l) {
arr.push(v);
}
for each(var v in values) {
if (!arr.includes(v)) {
arr.push(v);
}
}
return toStringArray(arr);
}
這篇關于使用Keyloak腳本映射器聚合聲明中的角色屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,