UNPKG

@trustcomponent/trustcaptcha-nodejs

Version:

TrustCaptcha – Privacy-first CAPTCHA solution for Node.js and TypeScript. GDPR-compliant, bot protection made in Europe.

41 lines (40 loc) 1.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VerificationToken = void 0; var uuid_1 = require("uuid"); var trustCaptcha_1 = require("../trustCaptcha"); var VerificationToken = /** @class */ (function () { function VerificationToken(verificationId, clientFailover) { if (clientFailover === void 0) { clientFailover = false; } if (!(0, uuid_1.validate)(verificationId) && !verificationId.startsWith('00000000-0000-0000-0000-00000000000')) { throw new trustCaptcha_1.VerificationTokenInvalidException('Invalid UUID in verification token'); } this.verificationId = verificationId; this.clientFailover = clientFailover; } VerificationToken.fromBase64 = function (base64String) { try { var jsonString = Buffer.from(base64String, 'base64').toString('utf-8'); var jsonObject = JSON.parse(jsonString); if (!jsonObject.verificationId) { throw new trustCaptcha_1.VerificationTokenInvalidException('Missing required fields in verification token'); } return new VerificationToken(jsonObject.verificationId, jsonObject.clientFailover === true); } catch (e) { if (e instanceof trustCaptcha_1.VerificationTokenInvalidException) throw e; if (e instanceof SyntaxError) { throw new trustCaptcha_1.VerificationTokenInvalidException('Invalid Base64-encoded JSON'); } else if (e instanceof Error) { throw new trustCaptcha_1.VerificationTokenInvalidException(e.message); } else { throw new trustCaptcha_1.VerificationTokenInvalidException('Unknown error'); } } }; return VerificationToken; }()); exports.VerificationToken = VerificationToken;