问题描述
我们的测试环境位于Google IAP之后,被测试的应用程序正在使用Bearer令牌进行用户身份验证。
为了访问测试环境,我获取了Google JWT令牌,然后通过扩展Authorization
将其添加为所有请求上的RequestHook
头:
import { RequestHook } from 'testcafe';
import config from '../config/.config.json';
import serviceGoogleAccount from '../config/.service-google-account.json';
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 ${serviceGoogleAccount.client_email}`);
auth.getClient()
.then(client => client.fetchIdToken(`${config.googleAuthSettings.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.
}
}
在手动测试并登录Google帐户以访问测试环境时,此令牌被设置为cookie:
Cookie example set by Google IAP after logging in
到目前为止,很好。
当我尝试使用用户登录应用程序时出现问题。我们的身份终结点为用户返回Bearer令牌,为了访问用户特定的页面,我需要在请求时将此用户的Bearer令牌作为Authorization标头传递。
但是由于上述Google JWT令牌实施,授权标头已经在使用中。而且由于登录到应用程序后,对于需要用户Bearer令牌的页面/端点,我得到401 Unauthorized,因此我认为用户的Bearer令牌被上面的Google JWT令牌覆盖。
有什么办法解决这个问题?
据我了解,根据https://github.com/DevExpress/testcafe/issues/4063
设置cookie并不是那么简单解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)