gpt4 book ai didi

java - 授权0 : how to retrieve app_metadata and user_metadata in token?

转载 作者:行者123 更新时间:2023-11-30 06:43:15 31 4
gpt4 key购买 nike

我尝试使用用户名和密码对用户进行身份验证。我想检索响应中的 JWT 并在其中找到他的权限(存储在 app_metadata 中)。

但返回的id_token不包含user_metadata或app_metadata。

我尝试使用 Java 驱动程序和 HTTP 调用。

Java:

 AuthAPI auth = new AuthAPI("my-domain.auth0.com", "my_client_id", "my_secret_id");
AuthRequest request = auth.login(username, password)
.setScope("openid app_metadata user_metadata");
try {
TokenHolder holder = request.execute();
return holder;
} catch (Auth0Exception e) {
throw new AuthentException("Error authenticating " + username, e);
}

HTTP:

     final String req = "{"
+ "\"username\":\"test@domain.com\","
+ "\"password\":\"test\","
+ "\"scope\":\"openid app_metadata user_metadata\","
+ "\"client_id\":\"my_client_id\","
+ "\"client_secret\":\"my_secret_id\","
+ "\"grant_type\":\"password\""
+ "}";
RestTemplate template = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(req, headers);

ResponseEntity<String> response = template.exchange("https://my-domain.auth0.com/oauth/token", HttpMethod.POST, entity, String.class);

返回的 id_token 仅包含:

 {
"email": "test@domain.com",
"email_verified": true,
"iss": "https://my-domain.auth0.com/",
"sub": "auth0|xxx",
"aud": "my_client_id",
"exp": 1497744462,
"iat": 1495116462
}

我尝试添加规则:

function (user, context, callback) {
var namespace = 'https://my-domain.auth0.com/';
if (context.idToken && user.user_metadata) {
context.idToken[namespace + 'user_metadata'] = user.user_metadata;
}
if (context.idToken && user.app_metadata) {
context.idToken[namespace + 'app_metadata'] = user.app_metadata;
}
callback(null, user, context);
}

还有一个钩子(Hook):

module.exports = function(client, scope, audience, context, cb) {
var access_token = {};
access_token.scope = scope;
access_token.scope.push('user_profile');
cb(null, access_token);
};

但是没有任何东西将元数据添加到 id_token。

我如何检索这些元数据?

谢谢。

最佳答案

我发现/oauth/ro 端点正在工作:https://auth0.com/docs/api/authentication#resource-owner

         final String req = "{"
+ "\"username\":\"ambre@domain.com\","
+ "\"password\":\"test\","
+ "\"scope\":\"" + settings.getScope() + "\","
+ "\"connection\":\"Username-Password-Authentication\","
+ "\"client_id\":\"" + settings.getClientId() + "\","
+ "\"grant_type\":\"password\""
+ "}";
RestTemplate template = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(req, headers);

ResponseEntity<String> response = template.exchange("https://my-domain.auth0.com/oauth/ro", HttpMethod.POST, entity, String.class);

但我在 java 驱动程序 1.0.0 中找不到等效项

关于java - 授权0 : how to retrieve app_metadata and user_metadata in token?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44050810/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com