2021-04-29 18:25:38 +03:00
|
|
|
///
|
2023-01-31 10:43:56 +02:00
|
|
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
2021-04-29 18:25:38 +03:00
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
2021-05-13 16:20:39 +03:00
|
|
|
import { Component, forwardRef, OnDestroy } from '@angular/core';
|
|
|
|
|
import {
|
|
|
|
|
ControlValueAccessor,
|
2023-02-02 15:55:06 +02:00
|
|
|
UntypedFormBuilder,
|
|
|
|
|
UntypedFormGroup,
|
2021-05-13 16:20:39 +03:00
|
|
|
NG_VALIDATORS,
|
|
|
|
|
NG_VALUE_ACCESSOR,
|
|
|
|
|
ValidationErrors,
|
|
|
|
|
Validator,
|
|
|
|
|
Validators
|
|
|
|
|
} from '@angular/forms';
|
2021-04-29 18:25:38 +03:00
|
|
|
import {
|
2021-05-13 16:20:39 +03:00
|
|
|
getDefaultClientSecurityConfig,
|
2021-11-08 11:31:50 +02:00
|
|
|
getDefaultServerSecurityConfig, Lwm2mClientKeyTooltipTranslationsMap,
|
2021-04-29 18:25:38 +03:00
|
|
|
Lwm2mSecurityConfigModels,
|
|
|
|
|
Lwm2mSecurityType,
|
|
|
|
|
Lwm2mSecurityTypeTranslationMap
|
|
|
|
|
} from '@shared/models/lwm2m-security-config.models';
|
|
|
|
|
import { Subject } from 'rxjs';
|
|
|
|
|
import { takeUntil } from 'rxjs/operators';
|
2021-05-13 16:20:39 +03:00
|
|
|
import { isDefinedAndNotNull } from '@core/utils';
|
2021-04-29 18:25:38 +03:00
|
|
|
|
|
|
|
|
@Component({
|
2021-07-12 10:45:58 +03:00
|
|
|
selector: 'tb-device-credentials-lwm2m',
|
|
|
|
|
templateUrl: './device-credentials-lwm2m.component.html',
|
|
|
|
|
styleUrls: ['./device-credentials-lwm2m.component.scss'],
|
2021-05-13 16:20:39 +03:00
|
|
|
providers: [
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
2021-07-12 10:45:58 +03:00
|
|
|
useExisting: forwardRef(() => DeviceCredentialsLwm2mComponent),
|
2021-05-13 16:20:39 +03:00
|
|
|
multi: true
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALIDATORS,
|
2021-07-12 10:45:58 +03:00
|
|
|
useExisting: forwardRef(() => DeviceCredentialsLwm2mComponent),
|
2021-05-13 16:20:39 +03:00
|
|
|
multi: true
|
|
|
|
|
}
|
|
|
|
|
]
|
2021-04-29 18:25:38 +03:00
|
|
|
})
|
|
|
|
|
|
2021-07-12 10:45:58 +03:00
|
|
|
export class DeviceCredentialsLwm2mComponent implements ControlValueAccessor, Validator, OnDestroy {
|
2021-04-29 18:25:38 +03:00
|
|
|
|
2023-02-02 15:55:06 +02:00
|
|
|
lwm2mConfigFormGroup: UntypedFormGroup;
|
2021-04-29 18:25:38 +03:00
|
|
|
securityConfigLwM2MType = Lwm2mSecurityType;
|
|
|
|
|
securityConfigLwM2MTypes = Object.keys(Lwm2mSecurityType);
|
|
|
|
|
credentialTypeLwM2MNamesMap = Lwm2mSecurityTypeTranslationMap;
|
2021-11-08 11:31:50 +02:00
|
|
|
clientKeyTooltipNamesMap = Lwm2mClientKeyTooltipTranslationsMap;
|
2021-04-29 18:25:38 +03:00
|
|
|
|
2023-02-06 13:09:43 +02:00
|
|
|
private destroy$ = new Subject<void>();
|
2021-05-13 16:20:39 +03:00
|
|
|
private propagateChange = (v: any) => {};
|
|
|
|
|
|
2023-02-02 15:55:06 +02:00
|
|
|
constructor(private fb: UntypedFormBuilder) {
|
2021-05-13 16:20:39 +03:00
|
|
|
this.lwm2mConfigFormGroup = this.initLwm2mConfigForm();
|
2021-04-29 18:25:38 +03:00
|
|
|
}
|
|
|
|
|
|
2021-05-13 16:20:39 +03:00
|
|
|
writeValue(obj: string) {
|
|
|
|
|
if (isDefinedAndNotNull(obj)) {
|
|
|
|
|
this.initClientSecurityConfig(JSON.parse(obj));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any) {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any) {}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
if (isDisabled) {
|
|
|
|
|
this.lwm2mConfigFormGroup.disable({emitEvent: false});
|
|
|
|
|
} else {
|
|
|
|
|
this.lwm2mConfigFormGroup.enable({emitEvent: false});
|
2021-07-07 15:25:48 +03:00
|
|
|
this.securityConfigClientUpdateValidators(this.lwm2mConfigFormGroup.get('client.securityConfigClientMode').value);
|
2021-05-13 16:20:39 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validate(): ValidationErrors | null {
|
|
|
|
|
return this.lwm2mConfigFormGroup.valid ? null : {
|
|
|
|
|
securityConfigLWm2m: false
|
|
|
|
|
};
|
2021-04-29 18:25:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
|
this.destroy$.next();
|
|
|
|
|
this.destroy$.complete();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 16:20:39 +03:00
|
|
|
private initClientSecurityConfig(config: Lwm2mSecurityConfigModels): void {
|
|
|
|
|
this.lwm2mConfigFormGroup.patchValue(config, {emitEvent: false});
|
|
|
|
|
this.securityConfigClientUpdateValidators(config.client.securityConfigClientMode);
|
2021-04-29 18:25:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private securityConfigClientModeChanged(type: Lwm2mSecurityType): void {
|
2021-05-13 16:20:39 +03:00
|
|
|
const config = getDefaultClientSecurityConfig(type, this.lwm2mConfigFormGroup.get('client.endpoint').value);
|
2021-04-29 18:25:38 +03:00
|
|
|
switch (type) {
|
|
|
|
|
case Lwm2mSecurityType.PSK:
|
2021-05-13 16:20:39 +03:00
|
|
|
config.key = this.lwm2mConfigFormGroup.get('client.key').value;
|
2021-04-29 18:25:38 +03:00
|
|
|
break;
|
|
|
|
|
case Lwm2mSecurityType.RPK:
|
2021-05-13 16:20:39 +03:00
|
|
|
config.key = this.lwm2mConfigFormGroup.get('client.key').value;
|
2021-04-29 18:25:38 +03:00
|
|
|
break;
|
|
|
|
|
}
|
2021-07-28 16:37:42 +03:00
|
|
|
this.lwm2mConfigFormGroup.get('client').reset(config, {emitEvent: false});
|
2021-04-29 18:25:38 +03:00
|
|
|
this.securityConfigClientUpdateValidators(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private securityConfigClientUpdateValidators = (mode: Lwm2mSecurityType): void => {
|
|
|
|
|
switch (mode) {
|
|
|
|
|
case Lwm2mSecurityType.NO_SEC:
|
2021-05-13 16:20:39 +03:00
|
|
|
this.setValidatorsNoSecX509();
|
2021-07-07 15:25:48 +03:00
|
|
|
this.lwm2mConfigFormGroup.get('client.cert').disable({emitEvent: false});
|
2021-05-13 16:20:39 +03:00
|
|
|
break;
|
2021-04-29 18:25:38 +03:00
|
|
|
case Lwm2mSecurityType.X509:
|
|
|
|
|
this.setValidatorsNoSecX509();
|
2021-07-07 15:25:48 +03:00
|
|
|
this.lwm2mConfigFormGroup.get('client.cert').enable({emitEvent: false});
|
2021-04-29 18:25:38 +03:00
|
|
|
break;
|
|
|
|
|
case Lwm2mSecurityType.PSK:
|
|
|
|
|
this.setValidatorsPskRpk(mode);
|
2021-07-07 15:25:48 +03:00
|
|
|
this.lwm2mConfigFormGroup.get('client.identity').enable({emitEvent: false});
|
2021-04-29 18:25:38 +03:00
|
|
|
break;
|
|
|
|
|
case Lwm2mSecurityType.RPK:
|
|
|
|
|
this.setValidatorsPskRpk(mode);
|
2021-07-07 15:25:48 +03:00
|
|
|
this.lwm2mConfigFormGroup.get('client.identity').disable({emitEvent: false});
|
2021-04-29 18:25:38 +03:00
|
|
|
break;
|
|
|
|
|
}
|
2021-05-13 16:20:39 +03:00
|
|
|
this.lwm2mConfigFormGroup.get('client.identity').updateValueAndValidity({emitEvent: false});
|
|
|
|
|
this.lwm2mConfigFormGroup.get('client.key').updateValueAndValidity({emitEvent: false});
|
2021-04-29 18:25:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private setValidatorsNoSecX509 = (): void => {
|
2021-05-13 16:20:39 +03:00
|
|
|
this.lwm2mConfigFormGroup.get('client.identity').clearValidators();
|
|
|
|
|
this.lwm2mConfigFormGroup.get('client.key').clearValidators();
|
|
|
|
|
this.lwm2mConfigFormGroup.get('client.identity').disable({emitEvent: false});
|
|
|
|
|
this.lwm2mConfigFormGroup.get('client.key').disable({emitEvent: false});
|
2021-04-29 18:25:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private setValidatorsPskRpk = (mode: Lwm2mSecurityType): void => {
|
|
|
|
|
if (mode === Lwm2mSecurityType.PSK) {
|
2021-05-13 16:20:39 +03:00
|
|
|
this.lwm2mConfigFormGroup.get('client.identity').setValidators([Validators.required]);
|
2021-04-29 18:25:38 +03:00
|
|
|
} else {
|
2021-05-13 16:20:39 +03:00
|
|
|
this.lwm2mConfigFormGroup.get('client.identity').clearValidators();
|
2021-04-29 18:25:38 +03:00
|
|
|
}
|
2021-09-23 13:24:48 +03:00
|
|
|
this.lwm2mConfigFormGroup.get('client.key').setValidators([Validators.required]);
|
2021-05-13 16:20:39 +03:00
|
|
|
this.lwm2mConfigFormGroup.get('client.key').enable({emitEvent: false});
|
|
|
|
|
this.lwm2mConfigFormGroup.get('client.cert').disable({emitEvent: false});
|
2021-04-29 18:25:38 +03:00
|
|
|
}
|
|
|
|
|
|
2023-02-02 15:55:06 +02:00
|
|
|
private initLwm2mConfigForm = (): UntypedFormGroup => {
|
2021-04-29 18:25:38 +03:00
|
|
|
const formGroup = this.fb.group({
|
2021-05-13 16:20:39 +03:00
|
|
|
client: this.fb.group({
|
|
|
|
|
endpoint: ['', Validators.required],
|
|
|
|
|
securityConfigClientMode: [Lwm2mSecurityType.NO_SEC],
|
|
|
|
|
identity: [{value: '', disabled: true}],
|
|
|
|
|
key: [{value: '', disabled: true}],
|
|
|
|
|
cert: [{value: '', disabled: true}]
|
|
|
|
|
}),
|
|
|
|
|
bootstrap: this.fb.group({
|
|
|
|
|
bootstrapServer: [getDefaultServerSecurityConfig()],
|
|
|
|
|
lwm2mServer: [getDefaultServerSecurityConfig()]
|
|
|
|
|
})
|
2021-04-29 18:25:38 +03:00
|
|
|
});
|
2021-05-13 16:20:39 +03:00
|
|
|
formGroup.get('client.securityConfigClientMode').valueChanges.pipe(
|
2021-04-29 18:25:38 +03:00
|
|
|
takeUntil(this.destroy$)
|
|
|
|
|
).subscribe((type) => {
|
|
|
|
|
this.securityConfigClientModeChanged(type);
|
|
|
|
|
});
|
2021-05-13 16:20:39 +03:00
|
|
|
formGroup.valueChanges.pipe(
|
2021-04-29 18:25:38 +03:00
|
|
|
takeUntil(this.destroy$)
|
2021-05-13 16:20:39 +03:00
|
|
|
).subscribe((value) => {
|
|
|
|
|
this.propagateChange(JSON.stringify(value));
|
2021-04-29 18:25:38 +03:00
|
|
|
});
|
|
|
|
|
return formGroup;
|
|
|
|
|
}
|
|
|
|
|
}
|