【问题标题】:Using microsoft-ada-angular6 and microsoft-graph-client, how do I verify the permissions of the token I receive back from AAD?使用 microsoft-ada-angular6 和 microsoft-graph-client,我如何验证从 AAD 收到的令牌的权限?
【发布时间】:2020-03-04 00:01:39
【问题描述】:

我在 Angular 中使用隐式授予工作流和 microsoft-adal-angular6 库对我的应用中的用户进行身份验证,然后获取访问 Microsoft Graph 的令牌。

身份验证部分正在工作。

我可以通过库从 AAD 获取令牌。但是,当我尝试通过客户端(和通过 http)查询图表时,我收到了无效的受众错误消息(http 查询仅对 401 的帮助稍小)。

statusCode: 401 
   code: "InvalidAuthenticationToken" 
   message: "Access token validation failure. Invalid audience." 
   requestId: "157c3867-3ac6-41e7-aa79-fbd6cc466c4f" 
   date: Tue Mar 03 2020 23:18:44 GMT-0700 (Mountain Standard Time) {} 
   body: "{"code":"InvalidAuthenticationToken","message":"Access token validation failure. Invalid audience.","innerError":{"request-id":"157c3867-3ac6-41e7-aa79-fbd6cc466c4f","date":"2020-03-03T23:18:44"}}"
    __proto__: Object`

在这里我设置了我的 ADAL 服务:

`adalConfig = {
  tenant: AppConfig.settings.aad.tenant,
  clientId: AppConfig.settings.aad.clientId,
  redirectUri: AppConfig.settings.aad.redirectUri,
  endpoints: AppConfig.settings.aad.apiEndpoint,
  navigateToLoginRequestUrl: false,
  cacheLocation: AppConfig.settings.aad.cacheLocation
};`

json:

"aad": {
    "requireAuth": true,
    "apiEndpoint": {
        "https://localhost:44371": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
        "https://graph.microsoft.com/v1.0/": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"  
    },
    "clientId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "tenant": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "resource": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "redirectUri": "http://localhost:4200/frameredirect/",
    "cacheLocation": "sessionStorage",
    "vospEndpoint": "https://localhost:44371",
    "graphEndpoint": "https://graph.microsoft.com/v1.0/",
    "vospAADGroup": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "clientSecret": "",
    "scopes": [
        "GroupMember.Read.All"
    ]
}}`

我为图端点获取令牌的调用:

private async getAccessToken(): Promise<string> {
const resource =  this.adalService.GetResourceForEndpoint(AppConfig.settings.aad.graphEndpoint);
let result = await this.adalService.acquireToken(resource).toPromise().then((token:string) => {return token;}).catch(error => {return error;});

if(result) console.log(result);
return result;

这是我调用初始化图形客户端并调用 api 的代码:

let graphClient = await Client.init({
  authProvider: async(done) => {
    let token = await this.getAccessToken()
    .catch((reason) => {
      done(reason,null);
    });

    if(token) {
      done(null, token);
    } else {
      done("Could not get an access token", null);
    }
  }
});

let aadGroupId = AppConfig.settings.aad.vospAADGroup;
let loggedInUser = this.adalService.LoggedInUserEmail;

let graphUser2 = await graphClient.api("/users/" + loggedInUser + "/memberOf?").filter("id eq '" + aadGroupId + "'").get().catch(error => {return error});

Azure 上的插槽已配置为授予以下 Graph API 范围:

Microsoft Graph (8) 
    Directory.AccessAsUser.all      Delegated       Access directory as the signed in user
    Directory.Read.all              Application     Read directory data
    Group.Read.All                  Delegated       Read all groups
    Group.Read.all                  Application     Read all groups
    User.Read                       Delegated       Sign in and read user profile
    User.Read.All                   Delegated       Read all users' full profiles
    User.Read.All                   Application     Read all users' full profiles
    User.ReadBasic.All              Delegated       Read all users' basic profiles

由于令牌受众,我对图表的调用被拒绝。如何使用 ADAL.js 的 microsoft-adal-angular6 包装器为令牌指定正确的受众和范围?

【问题讨论】:

  • graphEndpoint 不正确。应该是https://graph.microsoft.com
  • 谢谢。我从那个 URL 开始,然后添加了“/v1.0/”,看看这是否有所作为。它没有。我只用“graph.microsoft.com”再次尝试,令牌再次被图形拒绝。
  • @TonyJu,谢谢。见我上面的评论。
  • 你可以使用jwt.ms来解码你的token,aud的值是多少? docs.microsoft.com/en-us/azure/active-directory/develop/…
  • const resource = this.adalService.GetResourceForEndpoint(AppConfig.settings.aad.graphEndpoint); l的值是多少

标签: angular azure-active-directory microsoft-graph-api adal.js microsoft-adal-angular6


【解决方案1】:

使用https://jwt.ms(感谢@TonyJu)我发现令牌受众正在引用我的应用程序的clientId。所以在我的 JSON 中,我将 Endpoint 节点引用更改为 "https://graph.microsoft.com": "https://graph.microsoft.com"

这允许我的 http 拦截器从会话存储中获取端点的“资源”,并为正确的受众获取令牌。

这似乎可以在 microsoft-adal-angular6 documentation 中使用一些说明。但也许这只是常识。

@NgModule({
imports: [
    MsAdalAngular6Module.forRoot({
      tenant: '<YOUR TENANT>',<-------------------------------- ADD
      clientId: '<YOUR CLIENT / APP ID>',<--------------------- ADD
      redirectUri: window.location.origin,
      endpoints: { <------------------------------------------- ADD
        "https://localhost/Api/": "xxx-bae6-4760-b434-xxx",
        ---
        ---
      },
      navigateToLoginRequestUrl: false,
      cacheLocation: '<localStorage / sessionStorage>', <------ ADD
    }),
    ---
    ---
  ],
  ---
  ---
})

【讨论】:

    【解决方案2】:

    访问令牌验证失败。无效的观众

    此错误通常是由于错误的受众而发生的。我们可以使用https://jwt.ms/ 来查看令牌中的受众。如果要调用 Microsoft Graph API,则资源应为https://graph.microsoft.com

    参考:

    https://docs.microsoft.com/en-us/azure/active-directory/develop/access-tokens#payload-claims

    【讨论】:

      猜你喜欢
      • 2021-03-04
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2021-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多