UNPKG

@baqhub/sdk

Version:

The official JavaScript SDK for the BAQ federated app platform.

64 lines (63 loc) 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Authentication = void 0; const blobLink_js_1 = require("../model/links/blobLink.js"); const appRecord_js_1 = require("../model/recordTypes/appRecord.js"); const serverCredentialsRecord_js_1 = require("../model/recordTypes/serverCredentialsRecord.js"); const client_js_1 = require("./client.js"); const allowedIconTypes = ["image/jpeg", "image/png"]; async function register(entity, appContent, { icon, signal } = {}) { // Perform discovery. const client = client_js_1.Client.ofEntity(entity); const entityRecord = await client.getEntityRecord(signal); // Upload the app icon, if any. const iconLink = await (async () => { if (!icon) { return undefined; } if (!allowedIconTypes.includes(icon.type)) { throw new Error("Unsupported icon mime type."); } const blob = await client.uploadBlob(icon, signal); return blobLink_js_1.BlobLink.new(blob, icon.type, "icon"); })(); // Add the icon (if any) to the app record content. const fullAppContent = { ...appContent, icon: iconLink, }; // Create the app record. const appRecord = appRecord_js_1.AppRecord.new(entityRecord.author.entity, fullAppContent); const credentialsRecord = (0, serverCredentialsRecord_js_1.buildServerCredentialsRecord)(appRecord); const authenticatedClient = client_js_1.Client.authenticated({ entityRecord, appRecord, credentialsRecord, serverPublicKey: undefined, authorizationId: undefined, }); const [serverPublicKey, serverAppRecord] = await authenticatedClient.postAppRecord(appRecord, credentialsRecord, signal); // Resolve the authentication flow URL. const flowUrl = await client.expandUrlTemplate("auth", { record_id: appRecord.id }, signal); const state = { authorizationId: undefined, entityRecord, appRecord: serverAppRecord, credentialsRecord, serverPublicKey, }; return { flowUrl, state, }; } function complete(state, authorizationId) { return { ...state, authorizationId, }; } exports.Authentication = { register, complete, };