UNPKG

@tidecloak/js

Version:

TideCloak client side JS SDK

106 lines 4.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Hash_js_1 = require("../Cryptide/Hashing/Hash.js"); const index_js_1 = require("../Cryptide/index.js"); const Serialization_js_1 = require("../Cryptide/Serialization.js"); const Utils_js_1 = require("../Tools/Utils.js"); class BaseTideRequest { /** * * @param {string} name * @param {string} version * @param {string} authFlow * @param {Uint8Array} draft * @param {Uint8Array} dyanmicData */ constructor(name, version, authFlow, draft, dyanmicData = new Uint8Array()) { this.name = name; this.version = version; this.authFlow = authFlow; this.draft = draft.slice(); this.dyanmicData = dyanmicData.slice(); this.authorization = new Uint8Array(); this.authorizerCert = new Uint8Array(); ; this.authorizer = new Uint8Array(); ; this.expiry = BigInt((0, Utils_js_1.CurrentTime)() + 30); // default is 30s this.rules = new Uint8Array(); this.rulesCert = new Uint8Array(); } /** * * @param {number} timeFromNowInSeconds */ setCustomExpiry(timeFromNowInSeconds) { this.expiry = timeFromNowInSeconds; return this; } /** * @param {Uint8Array} authorizer */ addAuthorizer(authorizer) { this.authorizer = authorizer; } /** * * @param {Uint8Array} authorizerCertificate */ addAuthorizerCertificate(authorizerCertificate) { this.authorizerCert = authorizerCertificate; } /** * * @param {Uint8Array} authorization */ addAuthorization(authorization) { this.authorization = authorization; return this; } /** * @param {Uint8Array} rules */ addRules(rules) { this.rules = rules; } /** * * @param {Uint8Array} rulesCert */ addRulesCert(rulesCert) { this.rulesCert = rulesCert; } async dataToAuthorize() { return (0, Serialization_js_1.StringToUint8Array)("<datatoauthorize-" + this.name + ":" + this.version + (0, Serialization_js_1.bytesToBase64)(await (0, Hash_js_1.SHA512_Digest)(this.draft)) + this.expiry.toString() + "-datatoauthorize>"); } encode() { if (this.authorizer == null) throw Error("Authorizer not added to request"); if (this.authorizerCert == null) throw Error("Authorizer cert not provided"); if (this.authorization == null) throw Error("Authorize this request first with an authorizer"); const name_b = (0, Serialization_js_1.StringToUint8Array)(this.name); const version_b = (0, Serialization_js_1.StringToUint8Array)(this.version); const authFlow_b = (0, Serialization_js_1.StringToUint8Array)(this.authFlow); const expiry = new Uint8Array(8); const expiry_view = new DataView(expiry.buffer); expiry_view.setBigInt64(0, this.expiry, true); const req = index_js_1.Serialization.CreateTideMemory(name_b, 44 + // 11 fields * 4 byte length name_b.length + version_b.length + authFlow_b.length + expiry.length + this.draft.length + this.dyanmicData.length + this.authorizer.length + this.authorization.length + this.authorizerCert.length + this.rules.length + this.rulesCert.length); index_js_1.Serialization.WriteValue(req, 1, version_b); index_js_1.Serialization.WriteValue(req, 2, expiry); index_js_1.Serialization.WriteValue(req, 3, this.draft); index_js_1.Serialization.WriteValue(req, 4, authFlow_b); index_js_1.Serialization.WriteValue(req, 5, this.dyanmicData); index_js_1.Serialization.WriteValue(req, 6, this.authorizer); index_js_1.Serialization.WriteValue(req, 7, this.authorization); index_js_1.Serialization.WriteValue(req, 8, this.authorizerCert); index_js_1.Serialization.WriteValue(req, 9, this.rules); index_js_1.Serialization.WriteValue(req, 10, this.rulesCert); return req; } } exports.default = BaseTideRequest; //# sourceMappingURL=BaseTideRequest.js.map