All files / src/application/user request-verification-code.use-case.ts

96.29% Statements 26/27
92.85% Branches 13/14
100% Functions 4/4
96.29% Lines 26/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123                                      22x                                       49x 49x 49x 49x             49x                         49x       24x   24x 24x   24x   24x               24x           24x 24x   24x     2x 2x     22x   22x 1x 1x     21x 18x                       21x 1x        
import { setTimeout as sleep } from 'node:timers/promises';
 
import { logger } from '../../shared/logging/logger';
import { UserRepository } from './user-repository';
import { VerificationCodePurpose, VerificationCodeService } from './verification-code-service';
 
/**
 * Every call to this use case takes at least this long, whatever it did.
 *
 * The identical response body is defeated by the clock without it. An address with an account
 * writes a code to Redis and an address without one does not, and that difference is measurable
 * from outside: 161ms against 4ms while the mail was still awaited here, 1.44ms against 0.56ms
 * once it was not. Both are oracles; only the size changed.
 *
 * A fixed floor rather than random padding, because random padding averages out over enough
 * samples and a floor does not — as long as the work stays under it, which is what the warning
 * below watches for. 120ms leaves roughly eighty times the headroom the slowest path now needs,
 * and is not something a person waiting for an e-mail can perceive.
 */
const RESPONSE_FLOOR_MS = 120;
 
export interface RequestVerificationCodeInput {
  email: string;
}
 
/**
 * The request half of both assisted flows: "send me a code". One use case serves passwordless
 * sign-in and password reset because the logic is identical — only the purpose the code is scoped
 * to and the e-mail template differ, both injected.
 *
 * It resolves successfully in every case, including an address that has asked too often — and,
 * when `issueForUnknownEmail` is set, an address with no account gets a real code rather than
 * silence, because for passwordless sign-in that code is what creates the account. The caller must not be able to tell those apart from the outside: an
 * endpoint that answers differently for a registered e-mail is a way to test whether someone has
 * an account here, and this is a health app — the mere fact that an address is registered is
 * something worth protecting.
 */
export class RequestVerificationCodeUseCase {
  constructor(
    private readonly userRepository: UserRepository,
    private readonly verificationCodeService: VerificationCodeService,
    private readonly purpose: VerificationCodePurpose,
    private readonly sendCodeEmail: (to: string, code: string) => Promise<void>,
    /**
     * Writes the issued code to the application log. Wired to `NODE_ENV !== 'production'` because
     * EmailService silently no-ops when RESEND_API_KEY is unset — which is the default for local
     * development, CI and the test suite. Without this, the whole flow is unusable off-production
     * and fails in the one way it is designed never to reveal: a 200 with nothing behind it.
     */
    private readonly logIssuedCode: boolean = false,
    /**
     * Whether an address with no account still gets a code.
     *
     * True for passwordless sign-in, where the code is what creates the account
     * (see VerifyPasswordlessCodeUseCase) — and false for password reset, where
     * there is no password to reset and creating an account from a "forgot my
     * password" screen would be answering a question nobody asked.
     *
     * Neither setting is visible from outside: both purposes answer 200 under
     * the same time floor. What differs is whether an e-mail arrives, which is
     * exactly what already differed for both flows before this existed.
     */
    private readonly issueForUnknownEmail: boolean = false,
  ) {}
 
  async execute(input: RequestVerificationCodeInput): Promise<void> {
    const startedAt = performance.now();
 
    try {
      await this.issueAndSend(input);
    } finally {
      const elapsed = performance.now() - startedAt;
 
      Iif (elapsed >= RESPONSE_FLOOR_MS) {
        // The floor hides the difference only while the work fits under it. Past that it hides
        // nothing, and the useful thing is to say so rather than quietly go back to leaking.
        logger.warn(
          { purpose: this.purpose, elapsedMs: Math.round(elapsed), floorMs: RESPONSE_FLOOR_MS },
          'auth.verification_code_request_exceeded_time_floor',
        );
      } else {
        await sleep(RESPONSE_FLOOR_MS - elapsed);
      }
    }
  }
 
  private async issueAndSend(input: RequestVerificationCodeInput): Promise<void> {
    const email = input.email.trim().toLowerCase();
    const user = await this.userRepository.findByEmail(email);
 
    if (!user && !this.issueForUnknownEmail) {
      // No account and no signing up from here: no code, no mail, no trace of
      // the difference in the response.
      logger.info({ purpose: this.purpose }, 'auth.verification_code_requested_for_unknown_email');
      return;
    }
 
    const code = await this.verificationCodeService.issue(this.purpose, email);
 
    if (!code) {
      logger.warn({ purpose: this.purpose, userId: user?.id }, 'auth.verification_code_request_throttled');
      return;
    }
 
    if (this.logIssuedCode) {
      logger.info({ purpose: this.purpose, email, code }, 'auth.verification_code_issued (non-production only)');
    }
 
    // Deliberately not awaited. This is a network call to a third party, and awaiting it put the
    // whole round trip into the response of a registered address and nothing into the response of
    // an unregistered one — measured at ~156ms of the ~157ms gap, which is the entire enumeration
    // difference this flow exists to avoid, handed back by the clock.
    //
    // A delivery failure is still logged rather than raised, for the reason it always was: Resend
    // being down is not something the caller can act on, and a 500 for a real address beside a 200
    // for an unknown one is that same oracle wearing a different number. The .catch is what keeps
    // an unawaited rejection from becoming an unhandled one.
    void this.sendCodeEmail(user?.email ?? email, code).catch((error: unknown) => {
      logger.error({ err: error, purpose: this.purpose, userId: user?.id }, 'auth.verification_code_email_failed');
    });
  }
}