惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
小众软件
小众软件
Last Week in AI
Last Week in AI
月光博客
月光博客
博客园 - 聂微东
Recent Announcements
Recent Announcements
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
博客园 - Franky
云风的 BLOG
云风的 BLOG
量子位
N
Netflix TechBlog - Medium
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
博客园 - 司徒正美
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享

szhshp 的第三边境研究所

假设, AI 把我代替的那一刻真的到来 | szhshp 的第三边境研究所 小傻瓜都能懂的 AstrBot QQ 机器人集成 MCP 功能实战指南 | szhshp 的第三边境研究所 《智人之上: 从石器时代到 AI 时代的信息网络简史》阅读笔记 | szhshp 的第三边境研究所 iPadOS 26 无法设置空间场景图片壁纸的解决方法 | szhshp 的第三边境研究所 《一路云海》(终) | szhshp 的第三边境研究所 《一路云海》(四): 如何不按套路旅行 | szhshp 的第三边境研究所 《一路云海》(三): In Ya Mellow Tone | szhshp 的第三边境研究所 《一路云海》(二): 关西世博参观纪实 | szhshp 的第三边境研究所 《一路云海》(一): 新的征程 | szhshp 的第三边境研究所 2025 大阪世博会 [ 3 天前-先到先得 ] 阶段 场馆预约必中独家攻略 | szhshp 的第三边境研究所 Hackathon 随想 | szhshp 的第三边境研究所 一杯双皮奶 | szhshp 的第三边境研究所 Armbian + CasaOS + NAS 配置指南 | szhshp 的第三边境研究所 Docker 构建镜像报错: error getting credentials - err: exit status 1, out: `` | szhshp 的第三边境研究所 Disqus RIP! 论过高的维护成本如何治疗固执的坏习惯 | szhshp 的第三边境研究所 炸弹猫桌游变体规则 | szhshp 的第三边境研究所 《小岛经济学》阅读笔记 | szhshp 的第三边境研究所 《金钱心理学》阅读笔记 | szhshp 的第三边境研究所 为知笔记 RIP: 迁移剩余的笔记 | szhshp 的第三边境研究所 2025 博客第十年展望 - 再见我的过去 | szhshp 的第三边境研究所 我在独立游戏里面致敬的作品 | szhshp 的第三边境研究所 《How to make thing faster》阅读笔记 | szhshp 的第三边境研究所 《The Art of Clean Code》阅读笔记 | szhshp 的第三边境研究所 《Clean Architecture: A Craftsman Guide to Software Structure and Design》阅读笔记 | szhshp 的第三边境研究所 《How AI Works》阅读笔记 | szhshp 的第三边境研究所 游戏策划废案 - Project Uranus | szhshp 的第三边境研究所 游戏策划废案 - Project X | szhshp 的第三边境研究所 人生第一款独立游戏开发复盘 | szhshp 的第三边境研究所 Trap of Life | szhshp 的第三边境研究所 如果我用手搓了个暗物质雏形 | szhshp 的第三边境研究所
Cypress - End-to-End Testing: OKTA Authentication with MF...
2022-12-02 · via szhshp 的第三边境研究所

Meta

目录

The official Cypress documentation outlines an approach for programmatic OKTA authentication.

Why authenticate programmatically?

Typically, logging in a user within your app by authenticating via a third-party provider requires visiting login pages hosted on a different domain. Since each Cypress test is limited to visiting domains of the same origin, we can subvert visiting and testing third-party login pages by programmatically interacting with the third-party authentication API to login a user.

But for several cases the OKTA account requires MFA (Multi-Factor Authentication) due to company/organization security policy. This can create challenges in the login process during automated testing.

To pass the MFA auth in your test, we need to:

  1. Consult your security team to determine if MFA can be temporarily disabled for test accounts.
  2. If MFA cannot be disabled, configure it to use Security Questions (view all OKTA MFA options here). This allows automation by providing predefined answers in test cases.
  3. Fetch the stateToken with UserName/Password
  4. Submit the MFA Answers together with stateToken
  5. Fetch the sessionToken
  6. Set sessionToken via okta lib
  7. Get the ID Token + Access Token

The key step is to get the sessionToken with MFA answers (see OKTA API here)

Lets see how I do the command:


Cypress.Commands.add('loginByOktaApi', (username = 'testaccount', password = 'testaccount_1234') => {
  /**
   * Visit the homepage before initiating OKTA authentication to prevent postMessage errors 
   * @see https://github.com/cypress-io/cypress/issues/16310#issuecomment-1279523540
   */
  cy.visit('https://localhost:3000');

  return cy
    .request({
      method: 'POST',
      url: `https://${process.env.OKTA_DOMAIN}/api/v1/authn`,
      body: {
        username,
        password
      }
    })
    .then(({ body }) => {
      const user = body._embedded.user;
      const stateToken = body.stateToken;
      const factorId = body._embedded.factors[0].id;

      cy.request({
        method: 'POST',
        url: `https://${process.env.OKTA_DOMAIN}/api/v1/authn/factors/${factorId}/verify`,
        body: {
          stateToken,
          answer: process.env.OKTA_MFA_ANSWER
        }
      }).then(({ body }) => {
        const sessionToken = body.sessionToken;
        const config = {
          issuer: `https://${process.env.OKTA_DOMAIN}/oauth2/default`,
          clientId: process.env.OKTA_CLIENT_ID,
          redirectUri: 'https://localhost:3000/auth/callback',
          scope: ['openid', 'email', 'profile']
        };

        const authClient = new OktaAuth(config);

        return authClient.token.getWithoutPrompt({ sessionToken }).then(({ tokens }) => {
          window.localStorage.setItem('okta-token-storage', JSON.stringify(tokens));
        });
      });
    });
});

Next Step

Consider using cy.session() to persist authentication across tests, reducing login overhead. Note that this is still an experimental feature as of December 2022, and configuration adjustments may be required.