Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set user agent in the auth request ? #1489

Closed
amrsa1 opened this issue Nov 16, 2022 · 2 comments
Closed

How to set user agent in the auth request ? #1489

amrsa1 opened this issue Nov 16, 2022 · 2 comments
Assignees
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue.

Comments

@amrsa1
Copy link

amrsa1 commented Nov 16, 2022

our domain is protected with IAP authentication, and its blocking our e2e test.

so we have added API authentication using the code snippet below

import { GoogleAuth } from 'google-auth-library';
const auth = new GoogleAuth();
const url = `${configList.baseURL}/dashboard`;
const targetAudience = "51297171365dbt6s3oc4d09cf.apps.googleusercontent.com";
async function IapAuth() {
    try {
            console.info(`request IAP ${url} with target audience ${targetAudience}`);
            const client = await auth.getIdTokenClient(targetAudience);
            const res = await client.request({
                url, headers: {
                    user-Agent: 'e2e.test'
                }
            });
            IAP_token = res.config.headers.Authorization
            process.env.IAP_token = IAP_token
            console.log(`√ Iap token has been obtained ----> ${JSON.stringify(IAP_token)}`)
    } catch (err: any) {
        console.error(JSON.stringify(`X ---> ${JSON.stringify(err.message)}`));
        process.exitCode = 1;
    }
}

But since our domain is protected by DataDome, we need to pass a certain user agent in the request in order to not be blocked by Recaptcha, but the solution above didn't work out  since my user agent is not overnight the existing one but it appends with it 

and I'm getting 

the response below

response: {
    config: {
      url: 'https://test.net/ch/test/en/accounts/mpre/dashboard',
      headers: [Object],
      paramsSerializer: [Function: paramsSerializer],
      validateStatus: [Function: validateStatus],
      responseType: 'json',
      method: 'GET'
    },
    data: {
      url: 'https://geo.captcha-delivery.com/captcha/?initialCid=AHrlqAAAAAMAcG9_Htx_-xsAJcnjBA==&cid=y4Yi1XOmjjBKzFYD5yHexjTu9D8m4fr7R8VJ0qXItUW6~ml8Qy29O9I4.35XmXgvk4p9x8GKyoGcSJ2a8xWgXhlzp1qBP1wcuZPGoD8XXA-70sd-l5qy1iIIxQ753Gx&referer=http%3A%2F%2Fim-search-demo-predev.pricehubble.net%2Fch%2Fimmoworlddashboard%2Fen%2Faccounts%2Fmpre%2Fdashboard&hash=9F1FB446984167FE5DE89E73FFEE6E&t=fe&s=32675&e=71208da2bdf44b4b6ce35cd599f70dfd86f153f9cde560aab23d80d53832f711'
    },
    headers: {
      'accept-ch': 'Sec-CH-UA,Sec-CH-UA-Mobile,Sec-CH-UA-Platform,Sec-CH-UA-Arch,Sec-CH-UA-Full-Version-List,Sec-CH-UA-Model,Sec-CH-Device-Memory',
      'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000',
      'cache-control': 'max-age=0, private, no-cache, no-store, must-revalidate',
      charset: 'utf-8',
      connection: 'close',
      'content-length': '463',
      'content-type': 'application/json;charset=utf-8',
      date: 'Wed, 16 Nov 2022 13:31:58 GMT',
      pragma: 'no-cache',
      server: 'istio-envoy',
      'set-cookie': 'datadome=y4Yi1XOmjjBKzFYD5yHexjTu9D8m4fr7R8VJ0qXItUW6~ml8Qy29O9I4.35XmXgvk4p9x8GKyoGcSJ2a8xWgXhlzp1qBP1wcuZPGoD8XXA-70sd-l5qy1iIIxQ753Gx; Max-Age=31536000; Domain=.pricehubble.net; Path=/; Secure; SameSite=Lax',
      via: '1.1 google',
      'x-datadome': 'protected',
      'x-datadome-cid': 'AHrlqAAAAAMAcG9_Htx_-xsAJcnjBA==',
      'x-dd-b': '537',
      'x-envoy-upstream-service-time': '39'
    },
    status: 401,
    statusText: 'Unauthorized',
    request: {
      responseURL: 'https://test.net/ch/test/en/accounts/mpre/dashboard'
    }
  },
  config: {
    url: 'https://itest/ch/test/dashboard/en/accounts/mpre/dashboard',
    headers: {
      Authorization: 'Bearer eyJhbGciOiJSUzI1NaWF0IjoxNjY4NjA1NTE3LCJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJzdWIiOiIxMTc5NDk1NzQzODI4NTIwOTg3NTEifQ.HtGYf8R1NCE-MEgPDKvLz77gsdDpLPTbHpK26XYm1gAHhelOlObszZBMBbg3OhWc9VLStsg1rIHdv7VTEm3TB-v_orrpeTyQnqI11MvWvNWevJ4mr6KkDjO5AX8TKM9lOs-VBTVmHqWUcS3IADcpgJbExqwN42_0-VOUc0M9Wk_UC8lH0BaSXBwh5YNRkh5iNUnaTEmHa1bsqvV3Dcmhd1Lso3LtLJ80zcYz0nPVpdQdAESXp3nSOxETEdQ-ySFwF4NNZOy5CTp-AuJjmnpqHaGt7wDZGtf3Gux7f6Ps8e0rmZVnSHrK1T-LVtk6Z32yhb2ctM0ZsULIFFCaA2hOsw',
      'User-Agent': ' e2e_test' google-api-nodejs-client/8.7.0',   -----> need to change this
      'x-goog-api-client': 'gl-node/16.15.1 auth/8.7.0',
      Accept: 'application/json'
    },
    paramsSerializer: [Function: paramsSerializer],
    validateStatus: [Function: validateStatus],
    responseType: 'json',
    method: 'GET'
  },
  code: '401'
}

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

@amrsa1 amrsa1 added priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue. labels Nov 16, 2022
@danielbankhead
Copy link
Member

Hey @amrsa1,

You can extend the DefaultTransporter class and use the configure method to set the User-Agent for now:

class MyTransporter extends DefaultTransporter {
  configure(opts?: GaxiosOptions): GaxiosOptions {
    const options = super.configure(opts);
    if (!options.headers) options.headers = {};
    // Can also use `opts?.headers?` as well if you'd like to set per-request.
    options.headers['User-Agent'] = 'e2e_test';
    return options;
  }
}
client.transporter = new MyTransporter();

const res = await client.request({...});

In the next major release, this functionality will be made easier by this PR: #1406

@danielbankhead danielbankhead self-assigned this Nov 29, 2022
@meredithslota
Copy link

Closing as the question has been addressed, even though the linked PR isn't merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

3 participants