简体   繁体   English

从Office 365返回的OAuth2令牌不包含preffered_username声明

[英]OAuth2 token returned from Office 365 doesn't contain a preffered_username claim

I followed this tutorial and have gotten to the point where I am decoding the returned token, and extracting the email address (which should be stored in the preferred_username property), ie, the following code: 我按照本教程进行操作 ,现在已经到了解码返回的令牌并提取电子邮件地址(应存储在preferred_username属性中)的地步,即以下代码:

decoded_token = Base64.urlsafe_decode64(encoded_token)
jwt = JSON.parse(decoded_token)
email = jwt['preferred_username']

The problem is that the object returned doesn't contain this property, what I do get back is similar to below: 问题是返回的对象不包含此属性,我得到的返回结果类似于以下内容:

{  
   "ver":"2.0",
   "iss":"https://login.microsoftonline.com/9188040d-6c67-4c5b-b112-36a304b66dad/v2.0",
   "aud":"0ab6433e-84fc-469b-8c72-41f7a0241a61",
   "exp":1458142389,
   "iat":1458055989,
   "at_hash":"0OYaLKpTTdHNBrQNOqwQ0Q",
   "sub":"AAAAAAAAAAAAAAAAAAAAAC1TrOaOmvInYrFAyrQjlFI",
   "tid":"9188040d-6c67-4c5b-b112-36a304b66dad"
}

A quick glance at the spec indicates I am getting the correct object back from Office 365, as preferred_username is mentioned as a potential claim, but it isn't in the object I get back. 快速浏览该规范表明我正在从Office 365找回正确的对象,因为提到了preferred_username是一个潜在的主张,但我没有找回该对象。

It's possible I'm not calling the get_token function with the correct parameters, but the documentation for the library is pretty sparse , so I can't really tell. 我可能没有使用正确的参数调用get_token函数,但是该库的文档非常稀疏 ,所以我无法真正分辨。

I have raised an issue on Github. 我在Github上提出了一个问题。

Is this an error on the Office 365 end, an error with the tutorial, or am I doing something wrong myself? 这是Office 365端的错误,是本教程的错误,还是我自己做错了什么?

I am try to reproduce this issue using normal HTTP request however I could get the preferred_username property successfully. 我尝试使用普通的HTTP请求重现此问题,但是我可以成功获取preferred_username属性。

As far as I know, we can get this property only when we specific the openid scope in the request. 据我所知,只有在请求中指定了openid范围时,我们才能获得此属性。 To narrow down this issue, I suggest that you trying use Fiddler or Postman without Ruby. 为了缩小此问题的范围,建议您尝试在不使用Ruby的情况下使用Fiddler或Postman。

Here is the test using web browser and Fiddler to get the id token for your reference: 这是使用Web浏览器和Fiddler获取ID令牌以供参考的测试:

  1. Register the app in the portal using Office 365 account( which you can refer to the tutorial) 使用Office 365帐户在门户中注册应用程序(您可以参考该教程)
  2. Get the auth code in a web broswer via the link below: https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id= {ClientID}&response_type=code&redirect_uri={RedirectURL}&response_mode=query&scope=https%3A%2F%2Foutlook.office.com%2Fmail.read%20https%3A%2F%2Foutlook.office.com%2Fmail.send%20 openid &state=12345 通过以下链接在网络浏览器中获取身份验证代码: https ://login.microsoftonline.com/common/oauth2/v2.0/authorize ? client_id = {ClientID}&response_type = code&redirect_uri = {RedirectURL}&response_mode = query&scope = https %3A%2F%2Foutlook.office.com%2Fmail.read%20https%3A%2F%2Foutlook.office.com%2Fmail.send%20 openid &state = 12345
  3. Replace the auth code from preview request and using Fiddler to post the request to get the tokens: 替换预览请求中的身份验证代码,并使用Fiddler发布请求以获取令牌:

    POST: https://login.microsoftonline.com/common/oauth2/v2.0/token grant_type=authorization_code&client_id={ClientID}&scope=https%3A%2F%2Foutlook.office.com%2Fmail.read%20https%3A%2F%2Foutlook.office.com%2Fmail.send%20 openid &redirect_uri=http%3A%2F%2Flocalhost%3A55065%2F&client_secret={ClientSecret}&code={AuthCode} POST: https: //login.microsoftonline.com/common/oauth2/v2.0/token grant_type = authorization_code&client_id = {ClientID}&scope = https%3A%2F%2Foutlook.office.com%2Fmail.read%20https%3A% 2F%2Foutlook.office.com%2Fmail.send%20 openid &redirect_uri = http%3A%2F%2Flocalhost%3A55065%2F&client_secret = {ClientSecret}&code = {AuthCode}

  4. Decode the ID token from the link below: 从下面的链接解码ID令牌:

    https://jwt.io/ https://jwt.io/

Then I could get the preferred_username property from the ID token successfully. 然后,我可以从ID令牌中成功获取preferred_username属性。

Answered here by Jason Johnston from Microsoft (author of the tutorial): 来自Microsoft(本教程的作者)的Jason Johnston 在这里回答:

The Azure team deployed a breaking change to their v2 auth endpoint, which is causing the preferred_username to not be present. Azure团队对其v2身份验证终结点部署了重大更改,这导致不存在preferred_username。 You need to add profile to the SCOPES array in auth_helper.rb. 您需要在auth_helper.rb中将配置文件添加到SCOPES阵列。 I'll post an update to the tutorial after the Build conference. 在构建会议之后,我将发布教程的更新。

The SCOPES array in auth_helper.rb now looks like so: 现在, auth_helper.rb中的SCOPES数组如下所示:

SCOPES = [ 'openid', 'https://outlook.office.com/mail.read', 'profile' ]

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

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