UNPKG

@microsoft/useragent-sdk

Version:

SDK for building decentralized identity wallets and enterprise agents.

155 lines 6.35 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); const hub_common_js_1 = require("@decentralized-identity/hub-common-js"); const HubError_1 = require("./HubError"); const HubWriteResponse_1 = require("./responses/HubWriteResponse"); const HubObjectQueryResponse_1 = require("./responses/HubObjectQueryResponse"); const HubCommitQueryResponse_1 = require("./responses/HubCommitQueryResponse"); const node_fetch_1 = require("node-fetch"); const DidProtocol_1 = require("../crypto/protocols/did/DidProtocol"); const HttpResolver_1 = require("../resolvers/HttpResolver"); const UserAgentError_1 = require("../UserAgentError"); /** * Options for instantiating a new Hub session. */ class HubSessionOptions { constructor() { /** * The Identifier of the Hub, for addressing request envelopes. */ this.hubId = 'did:test:hub.id'; /** * The HTTPS endpoint of the Hub. */ this.hubEndpoint = 'https://beta.hub.microsoft.com'; } } exports.HubSessionOptions = HubSessionOptions; /** * Represents a communication session with a particular Hub instance. */ class HubSession { constructor(options) { this.client = options.client; this.hubId = options.hubId; this.hubEndpoint = options.hubEndpoint; this.hubOwner = options.hubOwner; this.signingKeyReference = options.signingKeyReference; this.encryptionKeyReference = options.encryptionKeyReference; } async send(request) { const rawRequestJson = await request.getRequestJson(); if (!this.client || !this.hubOwner) { throw new HubError_1.default({ error_code: hub_common_js_1.HubErrorCode.NotFound, developer_message: `Identifiers not Specified`, }); } rawRequestJson.iss = this.client.id; rawRequestJson.aud = this.hubId; rawRequestJson.sub = this.hubOwner.id; //rawRequestJson.commit = rawRequestJson.commit.serialize(); const rawRequestString = JSON.stringify(rawRequestJson); const response = await this.makeRequest(rawRequestString); let responseObject; try { responseObject = response; } catch (e) { throw new HubError_1.default({ error_code: hub_common_js_1.HubErrorCode.ServerError, developer_message: `Unexpected error decoding JSON response: ${e.message}`, inner_error: e, }); } return HubSession.mapResponseToObject(responseObject); } /** * Sends a raw (string) request body to the Hub and receives a response. * * @param message The raw request body to send. * @param accessToken The access token to include in the request, if any. */ async makeRequest(message) { if (!this.client || !this.hubOwner) { throw new HubError_1.default({ error_code: hub_common_js_1.HubErrorCode.AuthenticationFailed, developer_message: `Client Identifier not specified in options`, }); } let resolver; if (this.client.options && this.client.options.resolver) { resolver = this.client.options.resolver; } else { resolver = new HttpResolver_1.default('https://beta.discover.did.microsoft.com/'); } // await this.authentication.getAuthenticatedRequest(message, this.hubDid, accessToken); const didProtocolOptions = { sender: this.client, resolver }; const didProtocol = new DidProtocol_1.default(didProtocolOptions); if (!this.signingKeyReference) { throw new UserAgentError_1.default('Signing key reference is not defined'); } const request = await didProtocol.signAndEncrypt(message, this.hubId); const res = await this.callFetch(this.hubEndpoint, { method: 'POST', body: request, headers: { 'Content-Type': 'application/jwt', 'Content-Length': request.length.toString() }, }); if (res.status !== 200) { const errorResponse = await res.json(); throw new HubError_1.default(errorResponse); } const response = await res.buffer(); return didProtocol.decryptAndVerify(this.encryptionKeyReference, response); } /** * Fetch API wrapper, to allow unit testing. * * @param url The URL to make a request to. * @param init Request initialization details. */ async callFetch(url, init) { return node_fetch_1.default(url, init); } /** * Transforms a JSON blob returned by the Hub into a subclass of HubResponse, based on the `@type` * field of the response. * * @param response The Hub response to be transformed. */ static mapResponseToObject(response) { const responseTypeString = response['@type']; const responseType = HubSession.responseTypes[responseTypeString]; if (responseType) { return new responseType(response); } if (response['@type'] === 'ErrorResponse') { throw new HubError_1.default(response); } throw new HubError_1.default({ error_code: hub_common_js_1.HubErrorCode.NotImplemented, developer_message: `Unexpected response type ${responseTypeString}`, }); } } /** * Mapping of known response types. */ HubSession.responseTypes = { WriteResponse: HubWriteResponse_1.default, ObjectQueryResponse: HubObjectQueryResponse_1.default, CommitQueryResponse: HubCommitQueryResponse_1.default, }; exports.default = HubSession; //# sourceMappingURL=HubSession.js.map