@trustcomponent/trustcaptcha-frontend
Version:
TrustCaptcha – Privacy-first CAPTCHA solution. GDPR-compliant, bot protection made in Europe.
43 lines (42 loc) • 2.46 kB
JavaScript
import { importSPKI, jwtVerify } from "jose";
import { ErrorModel } from "./api/error/error-model";
import { ErrorCode } from "./api/error/error-code";
export class License {
constructor(customizabilityOption, invisibilityOption) {
this.customizabilityOption = customizabilityOption;
this.invisibilityOption = invisibilityOption;
}
static async create(token, siteKey, failureCallback) {
var _a, _b, _c, _d, _e, _f, _g, _h;
const decoded = await License.verifyAndDecodeToken(token, failureCallback);
if (!decoded) {
failureCallback(new ErrorModel(ErrorCode.LICENSE_INVALID, "License is not valid. Please check the given license."));
return undefined;
}
if (decoded.sub !== 'CaptchaBoxLicense') {
failureCallback(new ErrorModel(ErrorCode.LICENSE_INVALID, "License is not valid. Please check the given license."));
return undefined;
}
if (decoded.siteKey !== siteKey) {
failureCallback(new ErrorModel(ErrorCode.LICENSE_INVALID, "The license does not match your Captcha / the given siteKey. Please check the given license."));
return undefined;
}
return new License((_d = (_b = (_a = decoded.features) === null || _a === void 0 ? void 0 : _a.boxCustomizability) !== null && _b !== void 0 ? _b : (_c = decoded.features) === null || _c === void 0 ? void 0 : _c.customizability) !== null && _d !== void 0 ? _d : false, (_h = (_f = (_e = decoded.features) === null || _e === void 0 ? void 0 : _e.boxInvisibility) !== null && _f !== void 0 ? _f : (_g = decoded.features) === null || _g === void 0 ? void 0 : _g.invisibility) !== null && _h !== void 0 ? _h : false);
}
static async verifyAndDecodeToken(token, failureCallback) {
try {
const key = await importSPKI(this.publicKey, 'ES256');
const { payload } = await jwtVerify(token, key, { algorithms: ['ES256'] });
return payload;
}
catch (err) {
failureCallback(new ErrorModel(ErrorCode.LICENSE_INVALID, "Failed to check the validity of the license. Please check the given license."));
return undefined;
}
}
}
License.publicKey = `-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE7p2F00dj2AawXZNv44YEpFmmONYg
KvumZzJIONO4OcqvLe0j3yE9WYMAHNJSli8kUlxalIfexjXK7JkqGu0lsQ==
-----END PUBLIC KEY-----`;
//# sourceMappingURL=license.js.map