title: Azure OAuth with Cypress: Infinite Loop
body: <p>Trying to set up Cypress to test an application that uses OAuth against Azure AD. My <code>login</code> command is defined as follows:</p>
  <pre><code>Cypress.Commands.add('login', () =&gt; {
      return cy.request('POST', Cypress.env('AccessTokenUrl') +
          '?grant_type=' + Cypress.env('GrantType') +
          '&amp;client_id=' + Cypress.env('ClientId') +
          '&amp;client_secret=' + Cypress.env('ClientSecret'))
  })
  </code></pre>
  <p>This is what I call in a test:</p>
  <pre><code>        cy.login().then(response =&gt; {
              expect(response.status).to.eq(200)
              expect(response.body).to.have.property('access_token')
              expect(response.body).to.have.property('token_type', 'Bearer')
  
              const {access_token, expires_in, id_token} = response.body
              cy.setCookie('access_token', access_token)
          })
  
          cy.visit('my-url')
  </code></pre>
  <p>The validations pass. The login response contains a valid token. However, the <code>ct.visit</code> call fails with infinite recursion, as the parameters like <code>&amp;iframe-request-id=[some uuid]</code> become added over and over to a <code>login.microsoftonline.com</code> URL, until eventually returning <code>HTTP Error 414. The request URL is too long.</code></p>
  <p>Here's what the URL looks like, with some information redacted and with some formatting for clarity:</p>
  <pre><code>https://login.microsoftonline.com/
      [tenant-id]/oauth2/v2.0/authorize
      ?response_type=code
      &amp;client_id=[client-id]
      &amp;redirect_uri=[my-url]
      &amp;scope=openid+profile+email+https%3A%2F%2Fgraph.microsoft.com%2Fuser.read
      &amp;iframe-request-id=1a9fdcbd-6b9e-46c8-93e3-ce0edf62b600
      &amp;iframe-request-id=b5b5cf2b-e0a6-4d92-9e55-cf32208ab900
      &amp;iframe-request-id=8471e17f-1d36-48f7-8419-f54e14b3b100
      &amp;iframe-request-id=56113dad-6029-4a37-9758-5828f93f0300
      &amp;iframe-request-id=51c06224-98f1-4b83-a8f2-84f8dfe9aa00
      &amp;iframe-request-id=09775645-505c-42e0-ac56-1335b5a7ba00
      &amp;iframe-request-id=5c98158b-b202-41fe-9d65-8fbfe4e46500
      &amp;[and-so-on]
  
  </code></pre>
  <p>I have found various suggestions on the web about using Puppeteer as a task for Azure AD SSO, but none of them works for my purposes. First, they try to resolve the problem of actually obtaining the token, which I have already solved. Second, they rely on the login URL presenting an HTML form, which is not the case with <code>login.microsoftonline.com</code>.</p>
  <p>What do you suggest?</p>
  <p>UPDATE: Trying a different solution, I receive an interesting error. The <code>loginMS</code> command:</p>
  <pre><code>import * as MSAL from '@azure/msal-browser'
  
  Cypress.Commands.add('loginMS', () =&gt; {
      cy.request({
          method: 'POST',
          url: `https://login.microsoftonline.com/${Cypress.env('TenantId')}/oauth2/token`,
          form: true,
          body: {
              scope: Cypress.env('LoginScope'),
              client_id: Cypress.env('ClientId'),
              client_secret: Cypress.env('ClientSecret'),
              redirect_uri: Cypress.env('LoginRedirect'),
              grant_type: Cypress.env('GrantType'),
              username: Cypress.env('Username'),
              password: Cypress.env('Password'),
              response_type: 'code'
          }
      }).then(response =&gt; {
          console.log(response)
          window.localStorage.setItem(`msal.idtoken`, response.body.access_token);
          window.localStorage.setItem(`msal.client.info`, MSAL.clientInfo);
      })
  })
  </code></pre>
  <p>The error is:</p>
  <pre><code>Failed to find a valid digest in the 'integrity' attribute for resource
  'https://aadcdn.msauth.net/shared/1.0/content/js/OldConvergedLogin_PCore_Up8WrFIk8-TG_eqBz8MSlw2.js'
  with computed SHA-256 integrity 'NxfOkHjbTYDy/EOknsK0PMOfym7iLRGY+yBShyznzx4='.
  The resource has been blocked.
  </code></pre>