completeChallenge method

Future<T> completeChallenge(
  1. Session session, {
  2. required String completionToken,
  3. required Transaction transaction,
})

Validates a completion token with all protection mechanisms.

This method handles the complete completion token validation flow including:

  • Token decoding
  • Request retrieval
  • Not-verified detection (completion challenge missing)
  • Code verification
  • Expiration checks

Throws:

Returns the validated request that can be used to complete the operation.

Implementation

Future<T> completeChallenge(
  final Session session, {
  required final String completionToken,
  required final Transaction transaction,
}) async {
  final config = _completionConfig;

  final credentials = _decodeCompletionToken(completionToken);

  if (await config.hasTooManyAttempts(
    session,
    nonce: credentials.requestId,
  )) {
    throw ChallengeRateLimitExceededException();
  }

  final request = await config.getRequest(
    session,
    credentials.requestId,
    transaction: transaction,
  );
  if (request == null) {
    throw ChallengeRequestNotFoundException();
  }

  final completionChallenge = config.getCompletionChallenge(request);
  if (completionChallenge == null) {
    throw ChallengeNotVerifiedException();
  }

  if (!await _validateVerificationCode(
    verificationCode: credentials.verificationCode,
    challenge: completionChallenge,
  )) {
    throw ChallengeInvalidVerificationCodeException();
  }

  if (config.isExpired(request)) {
    await config.onExpired(session, request);
    throw ChallengeExpiredException();
  }

  return request;
}