All files / src challenge_response_flow.js

100% Statements 317/317
97.43% Branches 76/78
100% Functions 3/3
100% Lines 317/317

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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 3181x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1510x 1510x 1510x 1510x 1510x 1510x 1510x 1510x 1510x 1201x 1201x 1510x 1510x 30x 30x 1480x 1480x 1480x 1480x 1510x 5x 5x 1475x 1475x 1475x 1475x 1475x 1475x 1475x 1475x 1475x 1475x 1475x 1510x 1510x 607x 1510x 607x 607x 607x 607x 401x 607x 607x 607x 253x 253x 354x 354x 607x 14x 14x 340x 607x 607x 607x 607x 607x 607x 607x 607x 607x 607x 15x 15x 340x 340x 340x 340x 256x 256x 256x 256x 607x 2x 2x 254x 254x 254x 254x 607x 20x 20x 230x 230x 230x 230x 230x 230x 230x 607x 176x 176x 176x 176x 176x 176x 176x 176x 230x 230x 230x 230x 230x 230x 230x 230x 230x 230x 230x 1510x 868x 853x 868x 849x 849x 5x 5x 844x 844x 849x 849x 849x 753x 849x 849x 849x 97x 97x 747x 849x 849x 849x 849x 849x 849x 849x 849x 29x 29x 718x 718x 718x 718x 718x 718x 849x 849x 696x 849x 35x 35x 683x 683x 549x 849x 221x 221x 221x 221x 849x 38x 38x 328x 290x 290x 290x 849x 849x 868x 19x 19x 19x 1510x 1510x 1065x 1065x 1065x 1510x 1510x 1510x 1510x 1510x 1510x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 489x 489x 489x 489x 489x 489x 489x 393x 489x 489x 489x 489x 489x 489x 489x 489x 185x 185x 185x 489x 30x 30x 30x 30x 208x 178x 178x 178x 178x 489x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 572x 10x 10x 572x 1x 1x  
/*!
 * Copyright (c) 2026 The Triauth Authors (https://www.triauth.org/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */
 
import { TriauthError } from './error.js';
import { Helpers } from './helpers.js';
import { Validator } from './validator.js';
import { Identity } from './identity.js';
import { Challenge } from './challenge.js';
import { Response } from './response.js';
import { KNOWN_CONFIG_KEYS } from './config.js';
 
/**
 * Internal helper that implements the shared 3-step challenge-response flow used by
 * some of the top-level API methods (e.g., `authenticate`).
 *
 * Each API method delegates to `perform()` with a flow `type` and a pair of callbacks
 * (`onChallenge`, `onResponse`) carrying the per-flow logic.
 *
 * @class
 * @memberof Triauth
 */
export class ChallengeResponseFlow {
 
  /**
   * Executes one stage of the challenge-response flow:
   *  - **Stage 1** (when `identifier`+`callbackUrl` are given, `challenge`/`response` are not): builds a challenge,
   *    invokes `onChallenge` to let the caller mutate `challenge.data`, then returns `{challenge, redirectUrl}`.
   *  - **Stage 3** (when `challenge`+`response` are given): parses both, sanity-checks them against optional
   *    `identifier`/`callbackUrl` constraints, then invokes `onResponse` for per-flow verification.
   *
   * @param type {string} - Flow type: `'auth'`, `'ping'`, `'attest'`, `'sign'`, or `'stamp'`.
   * @param [options={}] {object} - User-facing options. Stage 1 expects `{identifier, callbackUrl, ext?, token?}`
   *                                 (`token` is required for every flow except `'auth'`, where it is rejected). Stage 3 expects
   *                                 `{challenge, response, identifier?, callbackUrl?}` (the optional ones, if
   *                                 provided, are additionally checked against the values embedded in the challenge).
   * @param [callbacks] {{onChallenge: function, onResponse: function}} -
   *        `onChallenge({challenge})` runs after the `Triauth.Challenge` is built (stage 1), giving the caller
   *        a chance to populate `challenge.data` (e.g., `msg`, `attest`, `attachments`).
   *        `onResponse({challenge, response})` performs verification (stage 3); must return the per-flow
   *        success object, `null` for expired-style failures, or `false` for generic verification failures.
   * @param [config={}] {object} - Optional overrides for the global Triauth.config settings.
   *
   * @returns {Promise<{challenge: string, redirectUrl: string}|{error: {code: number, message: string}}|*>}
   *          A promise resolving to `{challenge, redirectUrl}` (stage 1), whatever `onResponse` returned
   *          (stage 3 success), or `{error:{code,message}}` if any error was caught. When
   *          `config.requireSecure` is set, a stage-3 result that is not DNSSEC-secure is rejected with
   *          `{error:{code:404,…}}` instead of being returned (see `assertSecure`).
   */
  static async perform(type, options={}, callbacks={onChallenge:null, onResponse:null}, config={}) {
    config.logger?.info?.('Processing Triauth.' + type);
    const ts = Helpers.now();
 
    try {
 
      // Ensure only recognized options are present to help avoid misspellings.
      // `token` is only meaningful for token-bearing flows (ping/attest/sign/stamp), not for auth.
      const allowedOptions = ['identifier', 'callbackUrl', 'challenge', 'response', 'ext'];
      if (type !== 'auth') {
        allowedOptions.push('token');
      }
 
      if (!Helpers.hasOnlyKnownProperties(options, allowedOptions)) {
        throw new TriauthError(102, 'Unrecognized function argument');
      }
 
      // Reject config overrides carrying unknown keys (typos that would otherwise be silently ignored,
      // sometimes weakening security - e.g. a misspelled `requireSecure`). We validate the MERGED config,
      // so a stray key is caught whether it came from this call's override or a global Triauth.config
      if (!Helpers.hasOnlyKnownProperties(config, KNOWN_CONFIG_KEYS)) {
        throw new TriauthError(103, 'Unrecognized configuration key');
      }
 
      // Read supported options
      const identifier = options.identifier;
      const callbackUrl = options.callbackUrl;
      const challenge = options.challenge;
      const response = options.response;
      const ext = options.ext;
      const token = options.token;
 
      // Stage 1 - identifier, callbackUrl, and ext (optional) are provided, and we need to build and return a challenge and redirectUrl
      if (
        identifier != null &&
        callbackUrl != null &&
        (challenge == null && response == null)
      ) {
        const validationError = (
          Validator.validateIdentifier(identifier).errors[0] ||
          Validator.validateCallbackUrl(callbackUrl).errors[0] ||
          (ext != null && Validator.validateExt(ext).errors[0]) ||
          (token != null && Validator.validateToken(token).errors[0])
        );
 
        if (validationError) {
          throw new TriauthError(validationError.code, validationError.message);
        }
 
        // Token-gated flows (every flow except `auth`) require a token at issue
        if (type !== 'auth' && token == null) {
          throw new TriauthError(226, 'Invalid token');
        }
 
        config.logger?.info?.('Request parameters:', {identifier, callbackUrl, ext});
 
        const identity = new Identity(identifier, {}, config);
 
        // Prepare options for the authentication request
        const challengeOptions = {};
 
        challengeOptions.cburl = callbackUrl;
 
        if (ext != null) {
          challengeOptions.ext = ext;
        }
 
        // Build the authentication request for the given identity with the given options - `callbackUrl` and `ext`.
        const challengeObj = Challenge.build(type, identity, challengeOptions, config);
        await callbacks.onChallenge({challenge: challengeObj});
        const challengeString = challengeObj.toString();
 
        // Fail early if the just-built challenge is not itself a valid challenge
        const challengeError = Validator.validateChallenge(challengeString).errors[0];
        if (challengeError) {
          throw new TriauthError(challengeError.code, challengeError.message);
        }
 
        const authenticationEndpoint = identity.authenticationEndpoint;
 
        // Check if authentication endpoint has been configured for the given identifier's domain name
        if (!(await authenticationEndpoint.resolve())) {
          throw new TriauthError(301, 'Domain is not configured for triauth');
        }
 
        // Enforce config.requireSecure if set
        ChallengeResponseFlow.assertSecure(authenticationEndpoint.secure, config);
 
        const redirectUrlParams = {challenge:challengeString};
 
        // Include the pre-validated token in the request parameters if present
        if (token) {
          const [tokenPublicPart] = token.split(':');
 
          const challengeHMAC = Helpers.arrayBufferToBase64Url(
            await Helpers.hmacSha256(Helpers.stringToUtf8Bytes(token), Helpers.base64UrlToUint8(challengeString))
          );
 
          redirectUrlParams.token = tokenPublicPart + ':' + challengeHMAC;
        }
 
        const redirectUrl = authenticationEndpoint.urlFor(type, redirectUrlParams);
        const redirectUrlString = redirectUrl.toString();
 
        // Return the challenge that the client should remember, and a redirectUrl to which the user's web browser should be redirected.
        return Helpers.clone({
          challenge: challengeString,
          redirectUrl: redirectUrlString
        });
 
      // Stage 3 - both challenge and response are provided, and we need to verify them against each other and return the result.
      } else if (
        challenge != null &&
        response != null
      ) {
 
        if (response === 'false') {
          throw new TriauthError(403, 'Request has been denied');
        }
 
        const validationError = (
          Validator.validateChallenge(challenge).errors[0] ||
          Validator.validateResponse(response).errors[0] ||
          (identifier != null && Validator.validateIdentifier(identifier).errors[0]) ||
          (callbackUrl != null && Validator.validateCallbackUrl(callbackUrl).errors[0])
        );
 
        if (validationError) {
          throw new TriauthError(validationError.code, validationError.message);
        }
 
        config.logger?.info?.('Verifying response with constraints', {type, identifier, callbackUrl});
 
        // Parse the received response (string) into the Triauth.Response object.
        let challengeObj, responseObj;
 
        try {
          challengeObj = Challenge.fromString(options.challenge, config);
        } catch (err) {
          throw new TriauthError(223, 'Invalid challenge', {cause:err});
        }
 
        responseObj = new Response(response, config);
 
        // Make sure the challenge's type matches the flow type, and (if again passed in options)
        // that the identifier and/or callbackUrl matches the one embedded in the challenge.
        if (
          challengeObj.data.type !== type ||
          (identifier != null && challengeObj.identity.identifier !== identifier) ||
          (callbackUrl != null && challengeObj.data.cburl !== callbackUrl)
        ) {
          throw new TriauthError(401, 'Your identity could not be verified');
        }
 
        const retval = await callbacks.onResponse({challenge:challengeObj, response:responseObj});
 
        if (retval) {
          // Enforce config.requireSecure if set
          ChallengeResponseFlow.assertSecure(retval.secure ?? retval.verificationResult?.secure, config);
          return retval;
 
        } else if (retval === null) {
          throw new TriauthError(402, 'Authentication request has expired, please try again');
 
        } else {
          throw new TriauthError(401, 'Your identity could not be verified');
 
        }
 
      // Fail when the given function arguments do not match to any of stages.
      } else {
        throw new TriauthError(101, 'Invalid or missing function arguments - expected either {identifier,callbackUrl,ext,...} or {challenge,response,identifier?,callbackUrl?}');
 
      }
 
    } catch (err) {
      return TriauthError.process(err, config);
 
 
    } finally {
      config.logger?.info?.('Completed in ' + Math.round(Helpers.now() - ts) + 'ms');
 
    }
 
  }
 
  /**
   * Verifies a single-signature challenge response and enforces the success invariant shared by the
   * `auth`, `ping`, `sign`, and `stamp` flows: the envelope must carry exactly one signature, that
   * signature must be valid and backed by at least one key, its identifier must match the one bound
   * in the challenge, and the challenge's `cburl` must be a valid callback URL whose base URL equals
   * the signature's `via`. (The multi-signature `attest` flow has a different shape - 1 user + N
   * attester signatures - and intentionally does NOT use this helper.)
   *
   * @param type {string} - flow type ('auth'|'ping'|'sign'|'stamp'), passed through to Response.verify.
   * @param signedPayload {string} - the exact bytes the signature is verified against (the serialized
   *        challenge for auth/ping; the human-readable `msg` for sign/stamp).
   * @param challenge {Challenge} - the re-hydrated challenge.
   * @param response {Response} - the parsed response envelope.
   * @param timeout {number} - the flow's freshness window (e.g., `config.authTimeout`), subtracted from now.
   * @param config {object} - effective config (with an already-spawned logger).
   *
   * @returns {Promise<{sig: object, verifyResult: object}|null|false>} On success, the verified first
   *          signature plus the full multi-signature result (callers read `secure`/`expires` from the
   *          latter); `null` on a time/expiry failure; `false` on any other verification failure.
   */
  static async verifyChallengeResponse(type, signedPayload, challenge, response, timeout, config) {
    const verifyResult = await response.verify(type, signedPayload, challenge, {
      identifier: challenge.identity.identifier,
      notBefore: Date.now() - (timeout || 0),
      notAfter: Date.now(),
      minSignatures: 1,
      maxSignatures: 1
    });
 
    const sig = verifyResult && verifyResult.signatures[0];
 
    if (
      verifyResult && verifyResult.valid && verifyResult.signatures.length === 1 &&
      sig && sig.valid && sig.keys.length > 0 &&
      sig.identifier === challenge.identity.identifier &&
      Validator.validateCallbackUrl(challenge.data.cburl).valid && Helpers.getBaseUrl(challenge.data.cburl) === sig.via
    ) {
      return {sig, verifyResult};
 
    // For time constraints verification failure, signal "expired" (caller surfaces 402).
    } else if (verifyResult === null) {
      config.logger?.info?.('Verification failed - request expired', {identifier:challenge.identity.identifier, iat:parseInt(challenge.data.iat, 10)});
      return null;
 
    // For other falsy signature verification results, signal a generic failure (caller surfaces 401).
    } else {
      config.logger?.info?.('Verification failed', {identifier:challenge.identity.identifier});
      return false;
 
    }
  }
 
  /**
   * Enforces the optional `config.requireSecure` policy: when the caller has opted in, an
   * otherwise-valid result whose DNS chain was not reported DNSSEC-validated (`secure !== true`)
   * is rejected.
   *
   * @param secure {boolean|undefined} - the aggregate DNSSEC-validation flag of the result.
   * @param config {object} - effective config; `requireSecure` (default false) toggles enforcement.
   * @throws {TriauthError} 404 when `config.requireSecure` is set and `secure` is not strictly `true`.
   */
  static assertSecure(secure, config) {
    if (config.requireSecure && secure !== true) {
      throw new TriauthError(404, 'Your domain does not support DNSSEC, which is required to continue.');
    }
  }
 
}