2021-04-29 18:25:38 +03:00
|
|
|
///
|
2024-01-09 10:46:16 +02:00
|
|
|
/// Copyright © 2016-2024 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.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
import { Component, forwardRef, OnDestroy } from '@angular/core';
|
|
|
|
|
import {
|
|
|
|
|
ControlValueAccessor,
|
2023-02-02 15:55:06 +02:00
|
|
|
UntypedFormBuilder,
|
|
|
|
|
UntypedFormGroup,
|
2021-04-29 18:25:38 +03:00
|
|
|
NG_VALIDATORS,
|
|
|
|
|
NG_VALUE_ACCESSOR,
|
|
|
|
|
ValidationErrors,
|
|
|
|
|
Validator,
|
|
|
|
|
Validators
|
|
|
|
|
} from '@angular/forms';
|
|
|
|
|
import {
|
2021-11-08 11:31:50 +02:00
|
|
|
Lwm2mClientSecretKeyTooltipTranslationsMap,
|
|
|
|
|
Lwm2mPublicKeyOrIdTooltipTranslationsMap,
|
2021-04-29 18:25:38 +03:00
|
|
|
Lwm2mSecurityType,
|
|
|
|
|
Lwm2mSecurityTypeTranslationMap,
|
|
|
|
|
ServerSecurityConfig
|
|
|
|
|
} from '@shared/models/lwm2m-security-config.models';
|
|
|
|
|
import { takeUntil } from 'rxjs/operators';
|
|
|
|
|
import { Subject } from 'rxjs';
|
|
|
|
|
|
|
|
|
|
@Component({
|
2021-07-12 10:45:58 +03:00
|
|
|
selector: 'tb-device-credentials-lwm2m-server',
|
|
|
|
|
templateUrl: './device-credentials-lwm2m-server.component.html',
|
2021-05-28 16:39:30 +03:00
|
|
|
styleUrls: [],
|
2021-04-29 18:25:38 +03:00
|
|
|
providers: [
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
2021-07-12 10:45:58 +03:00
|
|
|
useExisting: forwardRef(() => DeviceCredentialsLwm2mServerComponent),
|
2021-04-29 18:25:38 +03:00
|
|
|
multi: true
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALIDATORS,
|
2021-07-12 10:45:58 +03:00
|
|
|
useExisting: forwardRef(() => DeviceCredentialsLwm2mServerComponent),
|
2021-04-29 18:25:38 +03:00
|
|
|
multi: true
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
|
2021-07-12 10:45:58 +03:00
|
|
|
export class DeviceCredentialsLwm2mServerComponent implements OnDestroy, ControlValueAccessor, Validator {
|
2021-04-29 18:25:38 +03:00
|
|
|
|
2023-02-02 15:55:06 +02:00
|
|
|
serverFormGroup: UntypedFormGroup;
|
2021-04-29 18:25:38 +03:00
|
|
|
securityConfigLwM2MType = Lwm2mSecurityType;
|
|
|
|
|
securityConfigLwM2MTypes = Object.values(Lwm2mSecurityType);
|
|
|
|
|
lwm2mSecurityTypeTranslationMap = Lwm2mSecurityTypeTranslationMap;
|
2021-11-08 11:31:50 +02:00
|
|
|
publicKeyOrIdTooltipNamesMap = Lwm2mPublicKeyOrIdTooltipTranslationsMap;
|
|
|
|
|
clientSecretKeyTooltipNamesMap = Lwm2mClientSecretKeyTooltipTranslationsMap;
|
2021-04-29 18:25:38 +03:00
|
|
|
|
2023-02-06 13:09:43 +02:00
|
|
|
private destroy$ = new Subject<void>();
|
2021-04-29 18:25:38 +03:00
|
|
|
private propagateChange = (v: any) => {};
|
|
|
|
|
|
2023-02-02 15:55:06 +02:00
|
|
|
constructor(private fb: UntypedFormBuilder) {
|
2021-04-29 18:25:38 +03:00
|
|
|
this.serverFormGroup = this.fb.group({
|
|
|
|
|
securityMode: [Lwm2mSecurityType.NO_SEC],
|
|
|
|
|
clientPublicKeyOrId: [''],
|
|
|
|
|
clientSecretKey: ['']
|
|
|
|
|
});
|
|
|
|
|
this.serverFormGroup.get('securityMode').valueChanges.pipe(
|
|
|
|
|
takeUntil(this.destroy$)
|
|
|
|
|
).subscribe((securityMode) => {
|
|
|
|
|
this.updateValidate(securityMode);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.serverFormGroup.valueChanges.pipe(
|
|
|
|
|
takeUntil(this.destroy$)
|
|
|
|
|
).subscribe((value) => {
|
|
|
|
|
this.propagateChange(value);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(value: any): void {
|
|
|
|
|
if (value) {
|
|
|
|
|
this.updateValueFields(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
if (isDisabled) {
|
|
|
|
|
this.serverFormGroup.disable({emitEvent: false});
|
|
|
|
|
} else {
|
|
|
|
|
this.serverFormGroup.enable({emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validate(control): ValidationErrors | null {
|
|
|
|
|
return this.serverFormGroup.valid ? null : {
|
|
|
|
|
securityConfig: {valid: false}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
|
this.destroy$.next();
|
|
|
|
|
this.destroy$.complete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private updateValueFields(serverData: ServerSecurityConfig): void {
|
|
|
|
|
this.serverFormGroup.patchValue(serverData, {emitEvent: false});
|
|
|
|
|
this.updateValidate(serverData.securityMode, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private updateValidate(securityMode: Lwm2mSecurityType, initValue = false): void {
|
|
|
|
|
switch (securityMode) {
|
|
|
|
|
case Lwm2mSecurityType.NO_SEC:
|
|
|
|
|
this.serverFormGroup.get('clientPublicKeyOrId').clearValidators();
|
|
|
|
|
this.serverFormGroup.get('clientSecretKey').clearValidators();
|
2021-05-13 16:20:39 +03:00
|
|
|
this.serverFormGroup.get('clientPublicKeyOrId').disable({emitEvent: false});
|
|
|
|
|
this.serverFormGroup.get('clientSecretKey').disable();
|
2021-04-29 18:25:38 +03:00
|
|
|
break;
|
|
|
|
|
case Lwm2mSecurityType.PSK:
|
|
|
|
|
case Lwm2mSecurityType.RPK:
|
|
|
|
|
case Lwm2mSecurityType.X509:
|
2021-09-23 13:24:48 +03:00
|
|
|
this.setValidatorsSecurity();
|
2021-04-29 18:25:38 +03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
this.serverFormGroup.get('clientPublicKeyOrId').updateValueAndValidity({emitEvent: false});
|
|
|
|
|
this.serverFormGroup.get('clientSecretKey').updateValueAndValidity({emitEvent: !initValue});
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-23 13:24:48 +03:00
|
|
|
private setValidatorsSecurity = (): void => {
|
|
|
|
|
const clientSecretKeyValidators = [Validators.required];
|
2021-08-09 11:48:08 +03:00
|
|
|
const clientPublicKeyOrIdValidators = [Validators.required];
|
2021-04-29 18:25:38 +03:00
|
|
|
|
2021-08-09 11:48:08 +03:00
|
|
|
this.serverFormGroup.get('clientPublicKeyOrId').setValidators(clientPublicKeyOrIdValidators);
|
|
|
|
|
this.serverFormGroup.get('clientSecretKey').setValidators(clientSecretKeyValidators);
|
2021-05-13 16:20:39 +03:00
|
|
|
|
|
|
|
|
this.serverFormGroup.get('clientPublicKeyOrId').enable({emitEvent: false});
|
|
|
|
|
this.serverFormGroup.get('clientSecretKey').enable();
|
2023-02-06 13:09:43 +02:00
|
|
|
};
|
2021-04-29 18:25:38 +03:00
|
|
|
}
|