0

我正在尝试在我的 Angular SPA 中静默刷新访问令牌。针对 ADFS 的身份验证已完成。它工作正常,配置如下:

oauthService.configure({
      redirectUri:
        window.location.origin + '/login',
      requireHttps: true,
      scope: 'openid profile email',
      responseType: 'id_token token',
      oidc: true,
      clientId: environment.adfsClientId,
      loginUrl: environment.adfsUrl + '/oauth2/authorize',
      issuer: environment.adfsUrl,
      logoutUrl:
        environment.adfsUrl +
        '/ls/?wa=wsignoutcleanup1.0&wreply=' +
        location.protocol +
        '//' +
        location.hostname + '/login',
      postLogoutRedirectUri:
        window.location.origin + '/login',
    });

    this.oauthService.tokenValidationHandler = new JwksValidationHandler();
    this.oauthService.setStorage(localStorage);
    this.oauthService.timeoutFactor = 0.03; // for faster testing
    this.oauthService.silentRefreshRedirectUri = window.location.origin + '/search';
    this.oauthService.setupAutomaticSilentRefresh();

为了刷新令牌,iframe 被添加到当前页面。一切看起来都很好。我已将以下代码添加到 index.html 文件中。

  <script>
    parent.postMessage(location.hash, location.origin);
  </script>

此事件在 silentRefreshPostMessageEventListener 的 angular-oauth2-oidc.js 中被捕获。但问题是我在那个列表中得到的信息是,

MSIS9621:无法在未获得用户输入的情况下处理 OAuth 授权请求。

在 iframe 中生成的 src 标签值如下所示,

https://[adfs domain]/adfs/oauth2/authorize/?response_type=id_token%20token&client_id=af0e4d79-ae9d-4fda-86b3-265d1e86a61e&state=UmtkeUJfNDBCUU1VWkZyeWcubFlldlo3ZHFFbFRuVDI3TnNZUU5FVXJxTXpX&redirect_uri=http%3A%2F%2Flocalhost%3A4200%2Fsearch&scope=openid%20profile% 20email&nonce=UmtkeUJfNDBCUU1VWkZyeWcubFlldlo3ZHFFbFRuVDI3TnNZUU5FVXJxTXpX&prompt=none

如果我在新选项卡中打开此 URL,我会在 Uri 中获得一个新的访问令牌。

谁能告诉我我在这里做错了什么?

更新

谢谢,加里·阿切尔的回答。

我通过在 PowerShell 中执行此代码对 ADFS 进行了更改,

Set-AdfsResponseHeaders -RemoveHeaders "X-Frame-Options"

没有改变。然后我设置下面的命令来解决这个问题。

Set-AdfsResponseHeaders -SetHeaderName "Content-Security-Policy" -SetHeaderValue "frame-ancestors < 来源 >"

4

1 回答 1

2

看起来 ADFS 正在阻止 iframe 请求并发送 X-Frame-Oprions=DENY 标头。根据这篇文章,它可以在 ADFS 2019 中解决。

一种可行的选择是改用刷新令牌,但不建议在 2021 年将其用于生产 SPA,因为刷新令牌不应存储在浏览器中的任何位置。

值得记住的是,在 2021 年也不真正推荐基于静默 iframe 的更新,因为攻击者可能会在自己隐藏的 iframe 上利用它并获取令牌。

2021 年的首选方案是后端换前端方法,其中 API 处理 SPA 的令牌更新。

AZURE AD API 驱动方法

继续的方法是插入一个 API 并将其指向 Azure AD 端点。这将使令牌远离浏览器,仅使用 SameSite cookie。

Curity 的以下解决方案演示了这种方法,并且 API 可以指向任何授权服务器。不过它需要一些消化。

于 2021-07-06T17:54:26.687 回答