I'Ve been having the following problem while trying to replicate the Azure Active Directory Authentication for a non-local website that calls the Microsoft loging page directly. I've been told to use the cy.visit clause instead of the cy.origin clause as we are being automatically redirected. Even though I am using this method, I am getting the following error message:
AADSTS9000411: The request is not properly formatted. The parameter 'iframe-request-id' is duplicated.
After a time, I also get this error:
AADSTS90015: Requested query string is too long.
Now, I have added the following tags in the cypress.config.js file:
experimentalModifyObstructiveThirdPartyCode: true, experimentalOriginDependencies: true
As far as I understand it, these two tags as used to address, amongst other things, frame busting issues.
Here is the code I tried to do the login:
function loginViaAAD(url, username, password) { cy.visit(url).then(($username, $password) => {
cy.get('input[type="email"]').type(username, {
log: false
});
cy.get('input[type="submit"]').click().then(($password) => {
cy.get('input[type="password"]').type(password, {
log: false
});
cy.get('input[type="submit"]').click();
cy.get('#idBtn_Back').click()
})
})
// Login to your AAD tenant. (Old cy.origin code from the exampke listed above).
/*cy.origin(
"https://login.microsoftonline.com/",
{
args: {
username,
password
}
},
({ username,password }) => {
cy.get('input[type="email"]').type(username, {
log: false
})
cy.get('input[type="submit"]').click().then(($password) => {
cy.get('input[type="password"]').type(password, {
log: false
})
cy.get('input[type="submit"]').click()
})
}
)*/
// Ensure Microsoft has redirected us back to the sample app with our logged in user.
cy.url().should("equal", url)
cy.get("#welcome-div").should(
"contain",
`Welcome ${Cypress.env("aad_username")}!`
)
}
Cypress.Commands.add("loginToAAD", (url, username, password) => {
cy.session(
${username},
() => {
const log = Cypress.log({
displayName: "Azure Active Directory Login",
message: [ Authenticating | ${username}],
// @ts-ignore
autoEnd: false
})
log.snapshot("before")
loginViaAAD(url, username, password)
log.snapshot("after")
log.end()
},
{
validate: () => {
// this is a very basic form of session validation
cy.visit(url)
cy.get("#welcome-div").should(
"contain",
`Welcome ${Cypress.env("aad_username")}!`)
}
})
})
As far as I can tell, It should work, but as mentionned with the errors above, it is not.
Now, perhaps the issue comes from the fact I am trying this login method on a already deployed site that might have mechanisms in place that does not allow for this login method. Are there any avenues I should be exploring to addresses the errors I listed above?
On the side I also trying to get a login token wih the following code:
Cypress.Commands.add('loginAAD2', (tenantId, clientId, clientSecret, username, password) => {
return cy
.request({
method: 'POST',
url: https://login.microsoftonline.com/${tenantId}/oauth2/token,
form: true,
body: {
grant_type: 'password',
tenant: tenantId,
client_id: clientId,
client_secret: clientSecret,
username: username,
password: password,
resource: clientId,
},
})
.then((response) => {
sessionStorage.setItem('access_token', response.body.access_token);
});
});
Here, I am assuing that I need to ask the IT guys to generate a clientid / client secret so that I am able to receive a token from MS (Assuming this method would still even work)?
Thanks in advance!