Repository: hashicorp/vault
File: ui/app/components/auth/form/saml.ts (TypeScript)

Code before:
      return;
    }

    // We've got a response from the polling request
    // pass MFA data or use the Vault token (client_token) to continue the auth
    const mfa_requirement = resp?.mfa_requirement;
    const client_token = resp?.client_token;
    if (mfa_requirement) {
      this.handleMfa(mfa_requirement, path);
      return;
    }
    if (client_token) {
      this.continueLogin({ token: client_token });
      return;
    }

    // If there's a problem with the SAML exchange the auth workflow should fail earlier.
    // Including this catch just in case, though it's unlikely this will be hit.
    this.handleSAMLError('Missing token. Please try again.');
    return;
  });

  async pollForToken(samlWindow: Window, { path = '' }) {
    // Poll every one second for the token to become available
    const WAIT_TIME = Ember.testing ? 50 : 1000;
    const MAX_TIME = 180; // 3 minutes in seconds

    const adapter = this.store.adapterFor('auth-method') as AuthMethodAdapter;
    // Wait up to 3 minutes for a token to become available
    for (let attempt = 0; attempt < MAX_TIME; attempt++) {
      await timeout(WAIT_TIME);

      try {
        const resp = await adapter.pollSAMLToken(
          path,
          this.fetchedRole?.tokenPollID,
          this.fetchedRole?.clientVerifier
        );

        if (resp?.auth) {
          // Exit loop if response
          return resp.auth;
        }
      } catch (e) {
        const error = e as AdapterError;
        if (error.httpStatus === 401) {
          // Continue to retry on 401 Unauthorized
          continue;
        }
        throw error;
      }

Review comment (security):
This is a [change I made in my auth service refactor PR](https://github.com/hashicorp/vault/pull/31085/files#diff-07d777459f437809c364ccae0ffa70d44651bf250b7da46de7fdbcd59ff15d1eR117), but the SAML test is quite slow and I think this would improve the tests overall by decreasing the max polling time.

Code after:
      return;
    }

    // We've got a response from the polling request
    // pass MFA data or use the Vault token (client_token) to continue the auth
    const mfa_requirement = resp?.mfa_requirement;
    const client_token = resp?.client_token;
    if (mfa_requirement) {
      this.handleMfa(mfa_requirement, path);
      return;
    }
    if (client_token) {
      this.continueLogin({ token: client_token });
      return;
    }

    // If there's a problem with the SAML exchange the auth workflow should fail earlier.
    // Including this catch just in case, though it's unlikely this will be hit.
    this.handleSAMLError('Missing token. Please try again.');
    return;
  });

  async pollForToken(samlWindow: Window, { path = '' }) {
    // Poll every one second for the token to become available
    const WAIT_TIME = Ember.testing ? 50 : 1000;
    const MAX_TIME = Ember.testing ? 3 : 180; // 180 is 3 minutes in seconds

    const adapter = this.store.adapterFor('auth-method') as AuthMethodAdapter;
    // Wait up to 3 minutes for a token to become available
    for (let attempt = 0; attempt < MAX_TIME; attempt++) {
      await timeout(WAIT_TIME);

      try {
        const resp = await adapter.pollSAMLToken(
          path,
          this.fetchedRole?.tokenPollID,
          this.fetchedRole?.clientVerifier
        );

        if (resp?.auth) {
          // Exit loop if response
          return resp.auth;
        }
      } catch (e) {
        const error = e as AdapterError;
        if (error.httpStatus === 401) {
          // Continue to retry on 401 Unauthorized
          continue;
        }
        throw error;
      }