首页 文章

Azure AD v2登录的JavaScript OAuth2流程不提供access_token

提问于
浏览
0

我正在使用bellhapijs并尝试让office365 provider工作,但似乎https://login.microsoftonline.com/common/oauth2/v2.0/token endpoints 没有给我获取配置文件信息所需的 access_token .

这是我看到的OAuth 2.0流程:

首先它重定向到

https://login.microsoftonline.com/common/oauth2/v2.0/authorize
?client_id=[client-id]
&response_type=code
&redirect_uri=http%3A%2F%2Flocalhost%3A5430%2Fapi%2Fv1%2Flogin%2Fazure-ad
&state=[state]
&scope=openid%20offline_access%20profile

oauth.js#L197

从Microsoft登录成功登录后,它会重定向到服务器,并且使用有效负载执行POST到https://login.microsoftonline.com/common/oauth2/v2.0/token的POST

{
  payload: 'grant_type=authorization_code&code=[code]&redirect_uri=http%3A%2F%2Flocalhost%3A5430%2Fapi%2Fv1%2Flogin%2Fazure-ad&client_id=[client-id]&client_secret=[client-secret]',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
}

oauth.js#L242

这反过来给了我以下回应

{
  "refresh_token": "MCTrMmd...",
  "id_token": "eyJ0eXAiOiJKV..."
}

OAuth 2.0 Authorization Code Flow文档来看,似乎我应该得到更多的东西

{
    "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5HVEZ2ZEstZnl0aEV1Q...",
    "token_type": "Bearer",
    "expires_in": 3599,
    "scope": "https%3A%2F%2Fgraph.microsoft.com%2Fmail.read",
    "refresh_token": "AwABAAAAvPM1KaPlrEqdFSBzjqfTGAMxZGUTdM0t4B4...",
    "id_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJhdWQiOiIyZDRkMTFhMi1mODE0LTQ2YTctOD...",
}

具体来说,我需要 access_token ,因为以下配置文件获取请求(oauth.js#L270)需要它

Authorization: 'Bearer ' + payload.access_token

即使在Calling /token endpoint does not give me an access_token中,似乎 /token 请求在响应中获得了更多字段 .

请求中是否有我遗漏的东西?

2 回答

  • 1

    查看您的第一个请求,它没有 response_mode=query 标头,与documentation形成对比:

    enter image description here

    其中还说明了预期的成功响应:
    enter image description here

    更新:当我在尝试获取令牌时未在有效负载中包含范围时,我能够复制此项:
    enter image description here

    在有效负载中包含范围将返回 access_token
    enter image description here

  • 1

    范围 openid ,_ 272323profileoffline_access 似乎没有返回 access_token .

    添加 User.Read 范围确实提供 access_token .

    对于贝尔,你需要这样的东西:

    server.auth.strategy('office365', 'bell', {
        provider: 'office365',
        scope: [
            'User.Read'
        ],
        password: 'something',
        clientId: 'clientId',
        clientSecret: 'clientSecret',
    });
    

    虽然,bell's office365 provider的 endpoints 仍然存在问题,请在此处记录:How do I get the logged in users profile for Azure AD OAuth logins?

    https://stackoverflow.com/a/49424859/111884的调查中得出这个结论 .

相关问题