UNPKG

@trustcomponent/trustcaptcha-frontend

Version:

TrustCaptcha – Privacy-first CAPTCHA solution. GDPR-compliant, bot protection made in Europe.

108 lines (107 loc) 5.28 kB
import { ApiService } from "./api-service"; import { PowSolver } from "../pow/pow-solver"; import { InformationCollector } from "../information-collector/information-collector"; import { ErrorCode } from "./error/error-code"; import { ErrorModel } from "./error/error-model"; export class ApiManager { constructor(captchaBox, successCallback, failureCallback) { this.isReset = false; this.captchaBox = captchaBox; this.successCallback = successCallback; this.failureCallback = failureCallback; this.abortController = new AbortController(); this.powSolver = new PowSolver(); } verify() { new InformationCollector(this.captchaBox).collectInformation().then(data => { ApiService.verifyUser(this.captchaBox.config, data, this.abortController.signal) .then((verificationResponse) => { if (this.isReset) return; switch (verificationResponse.status) { case 200: verificationResponse.json().then((data) => { this.solveChallenges(data); }); break; case 201: verificationResponse.json().then((response) => this.successCallback(response.verificationToken)); break; case 402: this.failureCallback(new ErrorModel(ErrorCode.PAYMENT_REQUIRED, "Limit of requests has been reached. Please contact support.")); break; case 403: this.failureCallback(new ErrorModel(ErrorCode.CAPTCHA_NOT_ACCESSIBLE, `The captcha is not accessible from '${location.hostname}'.`)); break; case 404: this.failureCallback(new ErrorModel(ErrorCode.SITE_KEY_NOT_VALID, `No captcha found with siteKey ${this.captchaBox.config.sitekey}.`)); break; case 422: this.failureCallback(new ErrorModel(ErrorCode.MODES_NOT_MATCHING, `Captcha mode '${this.captchaBox.config.mode}' not compatible with the captcha mode set in the settings.`)); break; case 423: this.failureCallback(new ErrorModel(ErrorCode.LOCKED, "Captcha blocked. Please contact the support.")); break; default: this.failureCallback(new ErrorModel(ErrorCode.UNKNOWN_ERROR, "Unknown error.")); break; } }) .catch((error) => { if (this.isReset) return; if (error.name === "AbortError") { // Request was aborted } else { this.failureCallback(new ErrorModel(ErrorCode.COMMUNICATION_FAILURE, "Server not reachable. Check your internet connection and try again.")); } }); }); } async solveChallenges(userResponseDto) { if (this.isReset) return; const startSolvingTimestamp = new Date().toISOString(); const nonces = await this.powSolver.solveTasks(userResponseDto); const solvedTimestamp = new Date().toISOString(); if (this.isReset) return; ApiService.sendSolutions(this.captchaBox.config, userResponseDto.challenge.verificationId, startSolvingTimestamp, solvedTimestamp, nonces, this.captchaBox.honeypotField.value, this.captchaBox.eventTracker.getEventsAndResetArray(), this.abortController.signal) .then((solutionResponse) => { if (this.isReset) return; switch (solutionResponse.status) { case 200: solutionResponse.json().then((newChallengeDto) => { this.solveChallenges(newChallengeDto); }); break; case 201: solutionResponse.json().then((response) => this.successCallback(response.verificationToken)); break; case 404: this.failureCallback(new ErrorModel(ErrorCode.SITE_KEY_NOT_VALID, `No captcha found with siteKey ${this.captchaBox.config.sitekey}.`)); break; case 409: this.failureCallback(new ErrorModel(ErrorCode.POW_FAILURE, "The solution of the bot verification tasks failed.")); break; default: this.failureCallback(new ErrorModel(ErrorCode.UNKNOWN_ERROR, "Unknown error.")); break; } }) .catch((error) => { if (this.isReset) return; if (error.name === "AbortError") { // Request was aborted } else { this.failureCallback(new ErrorModel(ErrorCode.COMMUNICATION_FAILURE, "Failed to communicate with the captcha backend services.")); } }); } reset() { this.isReset = true; this.abortController.abort(); this.powSolver.reset(); } } //# sourceMappingURL=api-manager.js.map