【问题标题】:How does TestCafe authenticate to an IAP secured applicationTestCafe 如何向 IAP 安全应用程序进行身份验证
【发布时间】:2020-10-20 16:25:23
【问题描述】:

有人可以指导一下 TestCafe 如何对 IAP 安全服务/应用进行身份验证吗?我试过阅读本指南here,但对我来说似乎有点复杂。如果有人之前做过这件事,如果你能分享,那就太好了。提前谢谢你。

【问题讨论】:

  • TestCafe 在 Node.js 上运行测试。因此,您可以像在 Node.js 应用程序中一样访问任何安全服务。请更详细地描述您的场景。您需要在测试中执行哪些操作?您需要从用户帐户还是从服务帐户进行身份验证?

标签: authentication testing automated-tests testcafe identity-aware-proxy


【解决方案1】:

我们通过使用 google-auth-library 并在 Testcafe 中扩展 RequestHook 来实现它。

  • 您需要拥有 Google IAP 凭据(您的第 1 步和第 2 步) link) 获取 JWT 令牌。
  • 获得 JWT 令牌后,添加它 作为您对应用程序执行的每个请求的授权标头 (通过扩展 RequestHook 实现)。

辅助函数的代码大致如下:

import { RequestHook } from 'testcafe';

import { GoogleAuth } from 'google-auth-library';

export class GoogleIapJWTAuthorization extends RequestHook {

  constructor () {
    // No URL filtering applied to this hook
    // so it will be used for all requests.
    super();

    const auth = new GoogleAuth({
      credentials: serviceGoogleAccount
    });

    console.log('Google Authentication');

    console.log(`Loaded Service Account ${GoogleAccount.client_email}`);
    auth.getClient()
    .then(client => client.fetchIdToken(`${GoogleAccount.targetAudience}`))
    .then(token => {
        console.log(`Successfully authenticated with Identity Aware Proxy. Id Token: ${token}`);
        this._token = token;
        return token;
    })
    .catch(err => {
        console.log(`Identity Aware Proxy Authentication Failed. Id Token: ${token}`);
        console.log(JSON.stringify(err));
        process.exitCode = 1;
    });
}
  getGoogleJwtToken() {
    return this._token;
  }

  onRequest (e) {
    //Authorization header for authentication into Google Auth IAP
    e.requestOptions.headers['Authorization']= `Bearer ${this._token}`;
  }

  onResponse (e) {
      // This method must also be overridden,
      // but you can leave it blank.
  }
}

【讨论】:

    猜你喜欢
    • 2019-08-31
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 2014-04-13
    • 2011-06-27
    • 1970-01-01
    相关资源
    最近更新 更多