UNPKG

@c8y/client

Version:

Client application programming interface to access the Cumulocity IoT-Platform REST services.

217 lines • 7.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TrustedCertificateService = void 0; const core_1 = require("../core"); /** * This class allows for managing trusted certificates. */ class TrustedCertificateService extends core_1.Service { constructor() { super(...arguments); this.listUrl = 'trusted-certificates'; this.proofOfPossessionUrl = 'trusted-certificates-pop'; this.generateCertificateAuthorityUrl = 'certificate-authority'; this.propertyName = 'certificates'; } get baseUrl() { return `/tenant/tenants/${this.client.tenant}`; } /** * Gets a list of trusted certificates. * * @returns Response wrapped in [[IResultList]]. * * **Example** * ```typescript * * (async () => { * const {data, res} = await trustedCertificateService.list(); * })(); * ``` */ async list(filter = {}) { return super.list(filter); } /** * Gets the details of trusted certificate * * @param entityOrId Trusted certificate object or trusted certificate fingerprint. * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * const fingerprint: string = 'abc'; * * (async () => { * const {data, res} = await trustedCertificateService.detail(fingerprint); * })(); * ``` */ async detail(entityOrId) { return super.detail(entityOrId); } /** * Removes a trusted certificate with given fingerprint. * * @returns Response wrapped in [[IResult]] * * @param entityOrId Trusted certificate object or trusted certificate fingerprint. * * **Example** * ```typescript * * const fingerprint: string = 'abc'; * * (async () => { * const {data, res} = await trustedCertificateService.delete(fingerprint); * })(); * ``` */ async delete(entityOrId) { return super.delete(entityOrId); } /** * Updates trusted certificate data. * * @param entity Trusted certificate partial object. * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * const certificate: Partial<ITrustedCertificate> = { * name: 'Name' * }; * * (async () => { * const {data, res} = await trustedCertificateService.update(certificate); * })(); * ``` */ async update(entity) { return super.update(entity); } /** * Creates a new trusted certificate. * * @param entity Trusted certificate object. * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * const certificate: Partial<ITrustedCertificate> = { * name: 'Name', * certInPemFormat: 'MIID+DCCAuCgAwIBAgIJAO1Q9t/M9gYlMA0GC...', * status: 'ENABLED' * }; * * (async () => { * const {data, res} = await trustedCertificateService.create(certificate); * })(); * ``` */ async create(entity) { return super.create(entity); } /** * Regenerates unsigned verification code for trusted certificate. * * * @param entityOrId Trusted certificate object or finger print. * * @returns Certificate object with new verification code (wrapped in [[IResult]]) * * **Example** * ```typescript * * (async () => { * const fingerPrint = '00a360973e3e8d61e05aedb32c72438e2442279e'; * const {data, res} = await trustedCertificateService.regeneratePoPVerificationCode(fingerPrint); * const newProofOfPossessionUnsignedVerificationCode = data.proofOfPossessionUnsignedVerificationCode; * })(); * ``` */ async regeneratePoPVerificationCode(entityOrId) { const method = 'POST'; const headers = { 'content-type': 'application/json', Accept: 'application/json' }; const url = `${this.proofOfPossessionUrl}/${this.getEntityId(entityOrId)}/verification-code`; const res = await this.fetch(url, { headers, method }); const data = await res.json(); return { res, data }; } /** * Verifies signed verification code for trusted certificate. * * * @param entityOrId Trusted certificate object or finger print. * @param proofOfPossessionSignedVerificationCode Unsigned verification code encrypted by the private key. * * @returns Certificate object with the result of verification (wrapped in [[IResult]]). * * **Example** * ```typescript * * (async () => { * const fingerPrint = '00a360973e3e8d61e05aedb32c72438e2442279e'; * const encryptedVerificationCode ='fuvlVWLfXG3V3bJWAdEhPD0HFCrYo5'; * const {data, res} = await trustedCertificateService.verifySignedVerificationCode(fingerPrint, encryptedVerificationCode); * const isCertificateVerified = data.proofOfPossessionValid; * })(); * ``` */ async verifySignedVerificationCode(entityOrId, proofOfPossessionSignedVerificationCode) { const method = 'POST'; const headers = { 'content-type': 'application/json', Accept: 'application/json' }; const url = `${this.proofOfPossessionUrl}/${this.getEntityId(entityOrId)}/pop`; const body = JSON.stringify({ proofOfPossessionSignedVerificationCode }); const res = await this.fetch(url, { method, body, headers }); const data = await res.json(); return { res, data }; } /** * Create cetificate authority for tenant. Only one certificate authority can be created for tenant. * * @returns Certificate authority object (wrapped in [[IResult]]). * * **Example** * ```typescript * * (async () => { * const {data, res} = await trustedCertificateService.generateCertificateAuthority(); * const certificateAuthority = data; * })(); * ``` */ async generateCertificateAuthority() { const method = 'POST'; const headers = { 'content-type': 'application/json', Accept: 'application/json' }; const url = `${this.generateCertificateAuthorityUrl}`; const res = await this.client.fetch(url, { headers, method }); const data = await res.json(); if (res.status > 400) { throw { res, data }; } return { res, data }; } getDetailUrl(entityOrId) { const id = this.getEntityId(entityOrId); return `${this.listUrl}/${id}`; } getEntityId(entityOrId) { let id; if (typeof entityOrId === 'object' && entityOrId.fingerprint) { id = entityOrId.fingerprint; } else { id = entityOrId; } return id; } } exports.TrustedCertificateService = TrustedCertificateService; //# sourceMappingURL=TrustedCertificateService.js.map