【问题标题】:Auth0 userinfo request fails to processAuth0 用户信息请求无法处理
【发布时间】:2020-01-31 11:54:33
【问题描述】:

我正在尝试使用 Auth0 作为中介对 GitHub 进行经过身份验证的调用。

我正在关注https://auth0.com/docs/connections/calling-an-external-idp-api 上的指南,但是当涉及到第 2 步时。获取 user_id,我遇到了 /userinfo 端点的问题。

const rp = require('request-promise')

const auth0_options = {
method: 'POST',
url: 'https://MY_DEV_ID/oauth/token',
headers: {'content-type': 'application/x-www-form-urlencoded'},
form: {
    grant_type: 'client_credentials',
    client_id: 'MY_CLIENT_ID',
    client_secret: 'MY_CLIENT_SECRET',
    audience: 'https://MY_DEV_ID/api/v2/'
    }
};
const auth0_result = JSON.parse(await rp(auth0_options));
const access_token_one = auth0_result.access_token;

然后我尝试按照https://auth0.com/docs/api/authentication#user-profile 中的描述调用 userinfo 端点

const userinfo_options = { method: 'GET',
url: 'https://MY_DEV_ID/userinfo',
headers: { 'Authorization' : `Bearer ${access_token_one}`} 
};
const userinfo_result = JSON.parse(await rp(userinfo_options));

但是这个不起作用,它返回一个空对象,我认为这是由于它未经授权。

我在 SO 上查找了许多关于 /userinfo 的先前问题,但发现在所有情况下,我发现问题是我已经确定的问题(令牌、受众等)

编辑: 我正在尝试使用的 auth0 api 的 openid 配置:

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"
  ],

【问题讨论】:

    标签: node.js access-token auth0 alexa-skills-kit


    【解决方案1】:

    好吧,很明显你在我的auth0_result 中得到的access token 不是userinfo 的正确的那个,那个需要不同的令牌。我在 alexa 技能中运行此代码,我的初始 access token 来自设置过程中的帐户链接。

    对于遇到此问题的任何人来说,这是最终可行的解决方案: (我的解决方案适用于 amazon alexa,但经过一些更改,它应该适用于您尝试连接的任何站点)

    let attributes = handlerInput.attributesManager.getSessionAttributes();
    attributes.loginToken = handlerInput.requestEnvelope.context.System.user.accessToken;
    login_options = {
        method: 'GET',
        uri: '{YOUR_AUTH0_TENANT_ID}/userinfo',
        headers : {
            Authorization : 'Bearer ' + attributes.loginToken,
        }
    };
    const login_result = JSON.parse(await rp(login_options));
    attributes.loggedInUser = login_result.nickname;
    
    const auth0_options = {
    method: 'POST',
    url: '{YOUR_AUTH0_TENANT_ID}/oauth/token',
    headers: {'content-type': 'application/x-www-form-urlencoded'},
    form: {
        grant_type: 'client_credentials',
        client_id: '{AUTH0_APPLICATION_CLIENT_ID}',
        client_secret: '{AUTH0_APPLICATION_CLIENT_SECRET}',
        audience: '{YOUR_AUTH0_TENANT_ID}/api/v2/',
    }
    };
    const auth0_result = JSON.parse(await rp(auth0_options));
    const access_token_one = auth0_result.access_token;
    
    const github_options = {
        method: 'GET',
        url: `{YOUR_AUTH0_TENANT_ID}/api/v2/users/${login_result.sub}`,
        headers : {'authorization' : 'Bearer ' + access_token_one}
    };
    const github_result = JSON.parse(await rp(github_options));
    attributes.token = github_result.identities[0].access_token;
    handlerInput.attributesManager.setSessionAttributes(attributes);
    

    在此之后,attributes.token 将包含一个访问令牌,您可以使用该令牌在 GitHub 上对自己进行身份验证(在 auth0 上将令牌过期时间设置为最长(30 天)或存储刷新令牌以供以后使用),例如 http 请求:

    function customhttpgetrequest(url, attributes){
        const customHeaderRequest = request.defaults({
            headers: {
                'User-Agent': attributes.loggedInUser,
                'Authorization': 'Bearer ' + attributes.token
            }
        })
        return new Promise((resolve, reject) => {
            customHeaderRequest.get(url, (error, response, body) => {
                if (error) {
                    return reject(error)
                }
                resolve(JSON.parse(body)); /*simply resolve(body) should work aswell, not sure why i parse it here*/
            })
        })
    }
    
    

    【讨论】:

      猜你喜欢
      • 2023-01-14
      • 1970-01-01
      • 2013-06-13
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      相关资源
      最近更新 更多