Skip to content

Instantly share code, notes, and snippets.

@ryan-buckman
Last active October 7, 2021 14:02
Show Gist options
  • Save ryan-buckman/c2dc2ab52df1ca15aa033e96fe6593ef to your computer and use it in GitHub Desktop.
Save ryan-buckman/c2dc2ab52df1ca15aa033e96fe6593ef to your computer and use it in GitHub Desktop.
Programmatically authenticate with identity server 4 (code flow + PKCE) via a custom Cypress command
// Programmatically authenticate with identity server 4 (code flow + PKCE) via a custom Cypress command
Cypress.Commands.add('Login', (email,password) => {
const identityServerUrl = '<your identity server url>';
const getLoginPage = {
method: 'GET',
url: identityServerUrl + '/connect/authorize',
qs: {
response_type: 'code',
client_id: '<your client id>',
redirect_uri: '<your redirect url'>,
scope: 'api1',
// https://tonyxu-io.github.io/pkce-generator/
code_challenge: '<your code challenge>',
code_challenge_method: 'S256',
}
};
cy.request(getLoginPage).then(response => {
const htmlDocument = document.createElement('html');
htmlDocument.innerHTML = response.body;
const requestVerificationToken = htmlDocument.getElementsByTagName('form')[0].elements.__RequestVerificationToken.value;
const loginUrl = response.allRequestResponses[1]["Request URL"];
cy.request({
method: 'POST',
url: loginUrl,
followRedirect: true,
form: true,
body: {
Email: email,
Password:password,
__RequestVerificationToken: requestVerificationToken
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment