UNPKG

@c8y/client

Version:

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

360 lines • 11.8 kB
import { __awaiter } from "tslib"; import { btoa } from 'b2a'; import { Service } from '../core/index.js'; import { DeviceRegistrationStatus } from './DeviceRegistrationStatus.js'; /** * This class allows registration of a new device. */ export class DeviceRegistrationService extends Service { constructor() { super(...arguments); this.baseUrl = 'devicecontrol'; this.listUrl = 'newDeviceRequests'; this.propertyName = 'newDeviceRequests'; } /** * Gets the details of device registration. * * @param {string|number|IIdentified} entityOrId Entity or Id of the entity. * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * const entityId: number = 1; * * (async () => { * const {data, res} = await deviceRegistrationService.detail(entityId); * })(); * ``` */ 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); }); } /** * Creates a new device registration. * * @param {IDeviceRegistrationCreate} entity Device registration object with mandantory fragments. * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * const mandantoryObject: IDeviceRegistrationCreate = { * id: 1, * }; * * (async () => { * const {data, res} = await deviceRegistrationService.create(mandantoryObject); * })(); * ``` */ 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); }); } /** * Gets the list of device registrations by parameters. * * @returns Response wrapped in [[IResultList]] * * @param {object} filter Object containing filters for querying registrations. * * **Example** * ```typescript * * const filter: object = { * pageSize: 100, * withTotalPages: true * }; * * (async () => { * const {data, res, paging} = await deviceRegistrationService.list(filter); * })(); * ``` */ list() { const _super = Object.create(null, { list: { get: () => super.list } }); return __awaiter(this, arguments, void 0, function* (filter = {}) { return _super.list.call(this, filter); }); } /** * Removes an registration with given id. * * @returns Response wrapped in [[IResult]] * * @param {string | number | IIdentified} entityOrId entity or id of the registration. * * **Example** * ```typescript * * const id = 'abc'; * * (async () => { * const {data, res} = await deviceRegistrationService.delete(id); * // data will be null * })(); * ``` */ 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); }); } /** * Accepts the device registration for given id. * * @returns Response wrapped in [[IResult]] * * @param {string | number | IIdentified} entityOrId entity or id of registration. Using an entity object one can pass a securityToken as string next to the id. * * @param {string} [securityToken] optional security token to reduce the risk of devices which are not yet registered being taken over by threat actors. * * **Examples** * ```typescript * * const id = 'abc'; * * (async () => { * const {data, res} = await deviceRegistrationService.accept(id); * })(); * ``` * * ```typescript * * const entity = { id: 'abc', securityToken: '<secretToken>' }; * * (async () => { * const {data, res} = await deviceRegistrationService.accept(entity); * })(); * ``` * * ```typescript * * const id = 'abc'; * const securityToken = '<secretToken>'; * * (async () => { * const {data, res} = await deviceRegistrationService.accept(id, securityToken); * })(); * ``` * * Providing a securityToken within an entity object together with another securityToken as a parameter of the * method will always use the securityToken of the entity object. In the example below <secretTokenA> is * used as the value. <secretTokenB> is obsolete and ignored. * * ```typescript * * const entity = { id: 'abc', securityToken: '<secretTokenA>'}; * const securityToken = '<secretTokenB>'; * * (async () => { * const {data, res} = await deviceRegistrationService.accept(entity, securityToken); * })(); * * ``` */ accept(entityOrId, securityToken) { const _super = Object.create(null, { update: { get: () => super.update } }); return __awaiter(this, void 0, void 0, function* () { const token = this.getSecurityToken(entityOrId) || securityToken; let payload = { id: this.getIdString(entityOrId), status: DeviceRegistrationStatus.ACCEPTED }; if (token) { payload = Object.assign(Object.assign({}, payload), { securityToken: token }); } return _super.update.call(this, payload); }); } /** * Accepts the device registration for all registrations in pending acceptance. * * @returns Response wrapped in [[IResultList]] * * **Example** * ```typescript * * (async () => { * const {data, res} = await deviceRegistrationService.acceptAll(); * })(); * ``` */ acceptAll() { const _super = Object.create(null, { fetch: { get: () => super.fetch } }); return __awaiter(this, void 0, void 0, function* () { const res = yield _super.fetch.call(this, `${this.listUrl}/acceptAll`, { method: 'POST' }); const data = (yield res.json()); return { res, data }; }); } /** * Gets limitation information for registering devices to the platform * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * (async () => { * const {data, res} = await deviceRegistrationService.limit(); * })(); * ``` */ limit() { const _super = Object.create(null, { fetch: { get: () => super.fetch } }); return __awaiter(this, void 0, void 0, function* () { const res = yield _super.fetch.call(this, `${this.listUrl}/limit`); const data = (yield res.json()); return { res, data }; }); } /** * Bootstraps the device with given id. * * @param {string | number | IIdentified} entityOrId entity or id of registration. Using an entity object one can pass a securityToken as string next to the id. * @param options for details see [[IDeviceBootstrapOptions]] * @param {string} [securityToken] optional security token to reduce the risk of devices which are not yet registered being taken over by threat actors. * * **Examples** * ```typescript * * const id: 'abc'; * const options: IDeviceBootstrapOptions = { * basicAuthToken: 'Basic dGVuYW50L3VzZXJuYW1lOnBhc3N3b3Jk', * basicAuth: { * user: 'username', * pass: 'password' * } * }; * * (async () => { * const {data, res} = await deviceRegistrationService.bootstrap(id, options); * })(); * ``` * * ```typescript * * const entity: { id: '123', securityToken: '<secretToken>'}; * const options: IDeviceBootstrapOptions = { * basicAuthToken: 'Basic dGVuYW50L3VzZXJuYW1lOnBhc3N3b3Jk', * basicAuth: { * user: 'username', * pass: 'password' * } * }; * * (async () => { * const {data, res} = await deviceRegistrationService.bootstrap(entity, options); * })(); * ``` * * ```typescript * * const id: '123'; * const options: IDeviceBootstrapOptions = { * basicAuthToken: 'Basic dGVuYW50L3VzZXJuYW1lOnBhc3N3b3Jk', * basicAuth: { * user: 'username', * pass: 'password' * } * }; * const securityToken: '<secretToken>' * * (async () => { * const {data, res} = await deviceRegistrationService.bootstrap(id, options, securityToken); * })(); * ``` * * Providing a securityToken within an entity object together with another securityToken as a parameter of the * method will always use the securityToken of the entity object. In the example below <secretTokenA> is * used as the value. <secretTokenB> is obsolete and ignored. * * ```typescript * * const options: IDeviceBootstrapOptions = { * basicAuthToken: 'Basic dGVuYW50L3VzZXJuYW1lOnBhc3N3b3Jk', * basicAuth: { * user: 'username', * pass: 'password' * } * }; * const entity: { id: '123', securityToken: '<secretTokenA>'}; * const securityToken: '<secretTokenB>' * * (async () => { * const {data, res} = await deviceRegistrationService.accept(entity, securityToken); * })(); * ``` */ bootstrap(entityOrId, options, securityToken) { return __awaiter(this, void 0, void 0, function* () { const id = this.getIdString(entityOrId); const token = this.getSecurityToken(entityOrId) || securityToken; const body = token ? JSON.stringify({ id, securityToken: token }) : JSON.stringify({ id }); const url = `${this.baseUrl}/deviceCredentials`; const { basicAuth } = options; let { basicAuthToken } = options; if (basicAuth) { const { user, pass } = basicAuth; basicAuthToken = btoa(`${user}:${pass}`); } const headers = { Authorization: `Basic ${basicAuthToken}`, accept: 'application/json', 'content-type': 'application/json' }; const method = 'POST'; const res = yield this.client.fetch(url, { body, headers, method }); const data = yield res.json(); if (res.status > 400) { throw { res, data }; } return { res, data }; }); } onBeforeCreate(entity) { return entity; } onBeforeUpdate(entity) { const noIdEntity = Object.assign({}, entity); delete noIdEntity.id; return noIdEntity; } getDetailUrl(entityOrId) { const id = encodeURIComponent(this.getEntityId(entityOrId)); return `${this.listUrl}/${id}`; } getSecurityToken(entity) { let securityToken; if (typeof entity === 'object') { securityToken = entity.securityToken ? String(entity.securityToken) : undefined; } return securityToken; } } //# sourceMappingURL=DeviceRegistrationService.js.map