简体   繁体   English

如何让JWT中添加Auth0的用户管理中的角色?

[英]How to get the roles in Auth0's user management to be added in the JWT?

I have an Auth0 application and I'm maintaining roles through the User Management.我有一个 Auth0 应用程序,我正在通过用户管理维护角色。 I would like to get those roles that are assigned to a user to be added to the JWT returned.我想让那些分配给用户的角色被添加到返回的 JWT 中。

I do have the following in the openid_connect_configuration.conf我在openid_connect_configuration.conf中确实有以下内容

map $host $oidc_scopes {
    default "openid+profile+email+offline_access+openid roles";
}

i have the following in the /.well-known/openid-configuration我在/.well-known/openid-configuration中有以下内容

{
    ...
    "scopes_supported": [
        "openid",
        "profile",
        "offline_access",
        "name",
        "given_name",
        "family_name",
        "nickname",
        "email",
        "email_verified",
        "picture",
        "created_at",
        "identities",
        "phone",
        "address"
    ],
    "response_types_supported": [
        "code",
        "token",
        "id_token",
        "code token",
        "code id_token",
        "token id_token",
        "code token id_token"
    ],
    "code_challenge_methods_supported": [
        "S256",
        "plain"
    ],
    "response_modes_supported": [
        "query",
        "fragment",
        "form_post"
    ],
    "subject_types_supported": ["public"],
    "id_token_signing_alg_values_supported": [
        "HS256",
        "RS256"
    ],
    "token_endpoint_auth_methods_supported": [
        "client_secret_basic",
        "client_secret_post"
    ],
    "claims_supported": [
        "aud",
        "auth_time",
        "created_at",
        "email",
        "email_verified",
        "exp",
        "family_name",
        "given_name",
        "iat",
        "identities",
        "iss",
        "name",
        "nickname",
        "phone_number",
        "picture",
        "sub"
    ],
    "request_uri_parameter_supported": false
}

How do I set things in Auth0 to return the roles assigned to the logged in user?如何在 Auth0 中设置内容以返回分配给已登录用户的角色? I have tried looking into the documentation, but I had no luck.我试过查看文档,但我没有运气。

I found my answer through exploring the extensions in Auth0.我通过探索 Auth0 中的扩展找到了答案。 I installed the Auth0 Authorization extension.我安装了Auth0 授权扩展。 I enabled the groups and roles.我启用了组和角色。

I then added the following rule:然后我添加了以下规则:

function setRolesToUser(user, context, callback) {
  // Roles should only be set to verified users.
  if (!user.email || !user.email_verified) {
    return callback(null, user, context);
  }

  user.app_metadata = user.app_metadata || {};

  auth0.users
    .updateAppMetadata(user.user_id, user.app_metadata)
    .then(function () {
      context.idToken['https://example.com/auth'] = user.app_metadata.authorization;
      callback(null, user, context);
    })
    .catch(function (err) {
      callback(err);
    });
}

I get the following as the JWT payload:我得到以下 JWT 有效负载:

{
    "https://example.com/auth": {
        "groups": ["Samples"],
        "roles": ["Editor"]
    },
    "sub": "auth0|xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "nickname": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "email_verified": true,
    "iss": "https://dev-xxxxxxxxxx.us.auth0.com/",
    "updated_at": "2022-04-29T20:01:14.585Z",
    "iat": 1.651330616E9,
    "picture": "https://s.gravatar.com/avatar/a705adb3d5d8530c35c41a9de260cd3c?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Flo.png",
    "exp": 1.651366616E9,
    "name": "xxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxx",
    "aud": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "nonce": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "email": "xxxxx.xxxxxxxxx@example.com"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM