UNPKG

jinaga

Version:

Data management for web and mobile applications.

75 lines 3.18 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthenticationTest = void 0; const authorization_engine_1 = require("../authorization/authorization-engine"); const storage_1 = require("../storage"); class AuthenticationTest { constructor(store, authorizationRules, userFact, deviceFact) { this.userFact = userFact; this.deviceFact = deviceFact; this.authorizationEngine = authorizationRules && new authorization_engine_1.AuthorizationEngine(authorizationRules, store); } login() { return __awaiter(this, void 0, void 0, function* () { if (!this.userFact) { throw new Error("No logged in user."); } return { userFact: this.userFact, profile: { displayName: "Test user" } }; }); } local() { return __awaiter(this, void 0, void 0, function* () { if (!this.deviceFact) { throw new Error("No persistent device."); } return this.deviceFact; }); } authorize(envelopes) { return __awaiter(this, void 0, void 0, function* () { if (this.authorizationEngine) { const results = yield this.authorizationEngine.authorizeFacts(envelopes, this.userFact); const authorizedEnvelopes = results.map(r => { const envelope = envelopes.find((0, storage_1.factEnvelopeEquals)(r.fact)); if (!envelope) { throw new Error("Fact not found in envelopes."); } if (r.verdict === "Accept") { return { fact: r.fact, signatures: envelope.signatures .filter(s => r.newPublicKeys.includes(s.publicKey)) }; } else if (r.verdict === "Existing") { return envelope; } else { throw new Error("Unexpected verdict."); } }); return authorizedEnvelopes; } else { return envelopes; } }); } } exports.AuthenticationTest = AuthenticationTest; //# sourceMappingURL=authentication-test.js.map