绕过 SSO 的示例 cypress 脚本

Akh*_*ula 5 javascript authentication azure single-sign-on cypress

我正在设置新的 cypress 测试来测试 Dynamics 365 应用程序中的一些功能。但是,我留下了一个带有 URL https://login.microsoftonline.com/__/和文本 Whoops的浏览器窗口,没有要运行的测试。

describe('Initial Cypress Tests', () => {
    it('navigate to D365', () => {
        cy.visit('https://wipropoc.crm8.dynamics.com')
    })
})
Run Code Online (Sandbox Code Playgroud)

Kon*_*man 1

建议您直接执行 POST 调用来获取 SSO 身份验证令牌并cy.visit('https://wipropoc.crm8.dynamics.com')使用获得的令牌进行触发。

以下是官方文档中要遵循的步骤,

  1. 在第三方服务器上完成身份验证后登录。
  2. 使用 解析标记cy.request()
  3. 在本地存储上手动设置令牌。
  4. 映射外部主机并指向本地服务器。

cy.request('POST', 'https://sso.corp.com/auth', { username: 'foo', password: 'bar' })
    .then((response) => {
    // pull out the location redirect
    const loc = response.headers['Location']

    // parse out the token from the url (assuming its in there)
    const token = parseOutMyToken(loc)

    // do something with the token that your web application expects
    // likely the same behavior as what your SSO does under the hood
    // assuming it handles query string tokens like this
    cy.visit('http://localhost:8080?token=' + token)

    // if you don't need to work with the token you can sometimes
    // just visit the location header directly
    cy.visit(loc)
    })
Run Code Online (Sandbox Code Playgroud)

您可以在这里阅读更多相关信息 - https://docs.cypress.io/guides/guides/web-security.html#Form-Submission-Redirects

实时示例 - https://xebia.com/blog/how-to-use-azure-ad-single-sign-on-with-cypress/