Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #32 from coltdorsey/kirk/cypress_change_to_proxy_a…
Browse files Browse the repository at this point in the history
…uth_header

Use Proxy-Authentication header
  • Loading branch information
coltdorsey authored Jan 26, 2022
2 parents 4bfec41 + d477f70 commit ee5a87d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cypress-iap",
"version": "0.0.0-development",
"description": "Cypress command to authenticate with IAP",
"version": "2.0.0",
"description": "Cypress command to authenticate to Google Identity Aware Proxy(IAP)",
"main": "index.js",
"types": "index.d.ts",
"author": "Colt Dorsey <coltdorsey@gmail.com>",
Expand Down
22 changes: 11 additions & 11 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module.exports = (on, config) => {
on('task', {
async getIAPToken({ url, cid }) {
const { GoogleAuth } = require('google-auth-library');
const auth = new GoogleAuth();
const client = await auth.getIdTokenClient(cid);
const res = await client.request({ url });
return res.config.headers.Authorization.split(" ")[1]
}
})
return config
}
on("task", {
async getIAPToken({ url, cid }) {
const { GoogleAuth } = require("google-auth-library");
const auth = new GoogleAuth();
const client = await auth.getIdTokenClient(cid);
const res = await client.request({ url });
return res.config.headers.Authorization;
},
});
return config;
};
46 changes: 28 additions & 18 deletions visit.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
/// <reference types="cypress">
Cypress.Commands.overwrite('visit', (originalFn, subject, ...args) => {
if (Cypress.config().baseUrl.indexOf('localhost') != -1) {
return originalFn(subject, ...args);
} else {
cy.exec('curl -I ' + Cypress.config().baseUrl + " | awk '/^location/ {split($NF, a, /[=&]/); print a[2]}'").then((client_id) => {
return client_id.stdout
}).then(client_id => {
cy.task('getIAPToken', { url: Cypress.config().baseUrl, cid: client_id }).then((token) => {
console.log(token)
cy.setCookie('GCP_IAAP_AUTH_TOKEN', token)
.then( () => {
return originalFn(subject, ...args)
})
})
})

}
})
Cypress.Commands.overwrite("visit", (originalFn, subject, ...args) => {
if (Cypress.config().baseUrl.indexOf("localhost") != -1) {
return originalFn(subject, ...args);
} else {
cy.exec(
`curl -I ${
Cypress.config().baseUrl
} | awk '/^location/ {split($NF, a, /[=&]/); print a[2]}'`
)
.then((client_id) => {
return client_id.stdout;
})
.then((client_id) => {
cy.task("getIAPToken", {
url: Cypress.config().baseUrl,
cid: client_id,
}).then((iapToken) => {
cy.intercept(`${Cypress.config().baseUrl}/**`, (req) => {
req.headers["Proxy-Authorization"] = `${iapToken}`;
})
.as("Cypress-IAP")
.then(() => {
return originalFn(subject, ...args);
});
});
});
}
});

0 comments on commit ee5a87d

Please sign in to comment.