@c8y/client
Version:
Client application programming interface to access the Cumulocity IoT-Platform REST services.
271 lines • 9.09 kB
JavaScript
import { __awaiter } from "tslib";
import { Service } from '../core/index.js';
/**
* This class allows for managing trusted certificates.
*/
export class TrustedCertificateService extends Service {
constructor() {
super(...arguments);
this.listUrl = 'trusted-certificates';
this.proofOfPossessionUrl = 'trusted-certificates-pop';
this.certificateAuthorityUrl = '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();
* })();
* ```
*/
list() {
const _super = Object.create(null, {
list: { get: () => super.list }
});
return __awaiter(this, arguments, void 0, function* (filter = {}) {
return _super.list.call(this, 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);
* })();
* ```
*/
detail(entityOrId) {
const _super = Object.create(null, {
detail: { get: () => super.detail }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.detail.call(this, 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);
* })();
* ```
*/
delete(entityOrId) {
const _super = Object.create(null, {
delete: { get: () => super.delete }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.delete.call(this, 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);
* })();
* ```
*/
update(entity) {
const _super = Object.create(null, {
update: { get: () => super.update }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.update.call(this, 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);
* })();
* ```
*/
create(entity) {
const _super = Object.create(null, {
create: { get: () => super.create }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.create.call(this, 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;
* })();
* ```
*/
regeneratePoPVerificationCode(entityOrId) {
return __awaiter(this, void 0, void 0, function* () {
const method = 'POST';
const headers = { 'content-type': 'application/json', Accept: 'application/json' };
const url = `${this.proofOfPossessionUrl}/${this.getEntityId(entityOrId)}/verification-code`;
const res = yield this.fetch(url, { headers, method });
const data = yield 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;
* })();
* ```
*/
verifySignedVerificationCode(entityOrId, proofOfPossessionSignedVerificationCode) {
return __awaiter(this, void 0, void 0, function* () {
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 = yield this.fetch(url, { method, body, headers });
const data = yield 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;
* })();
* ```
*/
generateCertificateAuthority() {
return __awaiter(this, void 0, void 0, function* () {
const method = 'POST';
const headers = { 'content-type': 'application/json', Accept: 'application/json' };
const url = this.certificateAuthorityUrl;
const res = yield this.client.fetch(url, { headers, method });
const data = yield res.json();
if (res.status >= 400) {
throw { res, data };
}
return { res, data };
});
}
/**
* Renews the certificate authority for the tenant.
*
* @returns The renewed certificate authority object (wrapped in [[IResult]]).
*
* **Example**
* ```typescript
* (async () => {
* const { data, res } = await trustedCertificateService.renewCertificateAuthority();
* const renewedCertificateAuthority = data;
* })();
* ```
*/
renewCertificateAuthority() {
return __awaiter(this, void 0, void 0, function* () {
const method = 'POST';
const headers = { 'content-type': 'application/json', Accept: 'application/json' };
const url = `${this.certificateAuthorityUrl}/renew`;
const res = yield this.client.fetch(url, { headers, method });
const data = yield 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;
}
}
//# sourceMappingURL=TrustedCertificateService.js.map