Last active
October 7, 2021 14:02
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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