UNPKG

@iotize/device-client.js

Version:

IoTize Device client for Javascript

85 lines (84 loc) 3.29 kB
import { Observable } from 'rxjs'; import { ResponseInterface } from '../../client/api'; import { ExecutionContext, RequestHandler, RequestInterceptor } from '../../client/impl/interceptors'; import { ApiConfig } from '../config/api-config'; import { ScramService } from '../service'; export interface ScramInterceptorOptions { /** * true if encryption is enabled, false otherwise */ encryption: boolean; /** * Initialization vector period is the number of requests sent * before API will change the initialization vector keys */ initializationVectorResetPeriod: number; /** * Encryption algo options */ keys: EncryptionKeys; /** * Encrypted frame counter */ frameCounter: number; } export interface EncryptionKeys { /** * Initialization vector used from encoding * If undefined, the default initialization vector will be used */ ivEncode?: Uint8Array; /** * Initialization vector used from decoding * If undefined, the default initialization vector will be used */ ivDecode?: Uint8Array; /** * Session key * If undefined, the session key will be regenerated when executing the first command (and if encryption is enabled) */ sessionKey?: Uint8Array; } export declare type InitializationVectorGeneratorFct = (length: number) => Uint8Array; export declare class ScramInterceptor implements RequestInterceptor { scramService: ScramService; apiConfig: ApiConfig; private _lwm2mResponseConverter; private _lwm2mCommandConverter; private _encryptionAlgo?; private _ivFrameCounter; private _ivSupported; private _options; private _cryptedFrameConverter; ivSupported: boolean; /** * Setter for the session key * @param key if null, it will stop encryption and remove session key. If true it will update session key used for encryption */ sessionKey: Uint8Array | undefined; readonly options: ScramInterceptorOptions; constructor(scramService: ScramService, apiConfig: ApiConfig); setEncryptionKeys(options: EncryptionKeys): void; setFrameCounter(value: number): void; setInitializationVectorRefreshPeriod(period: number): void; intercept(context: ExecutionContext, next: RequestHandler): Observable<ResponseInterface<any>>; private _sendWithEncryption; private _buildEncryptedFrame; private _setEncodeIV; private _setDecodeIV; private _toCallObservable; refreshEncryptionInitializationVector(): Promise<void>; initializationVectorGenerator: InitializationVectorGeneratorFct; private refreshEncryptionAlgo; private _startEncryption; private _stopEncryption; /** * Enable/Disable encrytion when communicating with a device * @param enable true if requests must be encrypted * @param resetSessionKey true if you want to reset the the session key. * @param refreshInitializationVector true if you want to change initialization vectors * * @deprecated use {@link encryption()} instead */ encryption(enable: boolean, resetSessionKey?: boolean, refreshInitializationVector?: boolean): Promise<void>; }