程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用 Keycloak Script Mapper 聚合声明中角色的属性大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用 Keycloak Script Mapper 聚合声明中角色的属性?

开发过程中遇到使用 Keycloak Script Mapper 聚合声明中角色的属性的问题如何解决?下面主要结合日常开发的经验,给出你关于使用 Keycloak Script Mapper 聚合声明中角色的属性的解决方法建议,希望对你解决使用 Keycloak Script Mapper 聚合声明中角色的属性有所启发或帮助;

我们有一个 Keycloak 脚本映射器来将角色的属性添加到 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,分别具有值为 1 和 2 的属性 foo,我们期望 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。我尝试将整个调用包装在一个函数中,以便可能在调用之间丢弃状态,但无济于事。结果如下:

使用 Keycloak Script Mapper 聚合声明中角色的属性

为什么状态会保存在多个调用中?有没有办法同时聚合角色属性的值?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的使用 Keycloak Script Mapper 聚合声明中角色的属性全部内容,希望文章能够帮你解决使用 Keycloak Script Mapper 聚合声明中角色的属性所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。