2020-09-29 10:33:29 +03:00
|
|
|
///
|
2023-01-31 10:43:56 +02:00
|
|
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
2020-09-29 10:33:29 +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-04-29 18:25:38 +03:00
|
|
|
import { Component, forwardRef, Input, OnDestroy, OnInit } from '@angular/core';
|
2020-09-29 10:33:29 +03:00
|
|
|
import {
|
|
|
|
|
ControlValueAccessor,
|
2023-02-02 15:55:06 +02:00
|
|
|
UntypedFormBuilder,
|
|
|
|
|
UntypedFormControl,
|
|
|
|
|
UntypedFormGroup,
|
2020-09-29 10:33:29 +03:00
|
|
|
NG_VALIDATORS,
|
|
|
|
|
NG_VALUE_ACCESSOR,
|
|
|
|
|
Validator,
|
|
|
|
|
Validators
|
|
|
|
|
} from '@angular/forms';
|
|
|
|
|
import {
|
|
|
|
|
credentialTypeNames,
|
2021-07-07 00:21:34 +03:00
|
|
|
credentialTypesByTransportType,
|
2020-09-29 10:33:29 +03:00
|
|
|
DeviceCredentials,
|
2021-07-06 17:50:20 +03:00
|
|
|
DeviceCredentialsType,
|
|
|
|
|
DeviceTransportType
|
2020-09-29 10:33:29 +03:00
|
|
|
} from '@shared/models/device.models';
|
2021-04-29 18:25:38 +03:00
|
|
|
import { Subject } from 'rxjs';
|
2021-05-13 16:20:39 +03:00
|
|
|
import { takeUntil } from 'rxjs/operators';
|
2023-05-03 14:59:41 +03:00
|
|
|
import { generateSecret, isDefinedAndNotNull } from '@core/utils';
|
2020-09-29 10:33:29 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-device-credentials',
|
|
|
|
|
templateUrl: './device-credentials.component.html',
|
|
|
|
|
providers: [
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => DeviceCredentialsComponent),
|
|
|
|
|
multi: true
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALIDATORS,
|
|
|
|
|
useExisting: forwardRef(() => DeviceCredentialsComponent),
|
|
|
|
|
multi: true,
|
|
|
|
|
}],
|
|
|
|
|
styleUrls: []
|
|
|
|
|
})
|
|
|
|
|
export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit, Validator, OnDestroy {
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
|
2021-07-07 00:21:34 +03:00
|
|
|
private deviceTransportTypeValue = DeviceTransportType.DEFAULT;
|
|
|
|
|
get deviceTransportType(): DeviceTransportType {
|
2021-07-07 15:25:48 +03:00
|
|
|
return this.deviceTransportTypeValue;
|
2021-07-07 00:21:34 +03:00
|
|
|
}
|
2021-07-06 17:50:20 +03:00
|
|
|
@Input()
|
2021-07-07 00:21:34 +03:00
|
|
|
set deviceTransportType(type: DeviceTransportType) {
|
|
|
|
|
if (type) {
|
|
|
|
|
this.deviceTransportTypeValue = type;
|
|
|
|
|
this.credentialsTypes = credentialTypesByTransportType.get(type);
|
|
|
|
|
const currentType = this.deviceCredentialsFormGroup.get('credentialsType').value;
|
|
|
|
|
if (!this.credentialsTypes.includes(currentType)) {
|
|
|
|
|
this.deviceCredentialsFormGroup.get('credentialsType').patchValue(this.credentialsTypes[0], {onlySelf: true});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-06 17:50:20 +03:00
|
|
|
|
2023-02-06 13:09:43 +02:00
|
|
|
private destroy$ = new Subject<void>();
|
2020-09-29 10:33:29 +03:00
|
|
|
|
2023-02-02 15:55:06 +02:00
|
|
|
deviceCredentialsFormGroup: UntypedFormGroup;
|
2020-09-29 10:33:29 +03:00
|
|
|
|
|
|
|
|
deviceCredentialsType = DeviceCredentialsType;
|
|
|
|
|
|
2021-07-07 00:21:34 +03:00
|
|
|
credentialsTypes = credentialTypesByTransportType.get(DeviceTransportType.DEFAULT);
|
2020-09-29 10:33:29 +03:00
|
|
|
|
|
|
|
|
credentialTypeNamesMap = credentialTypeNames;
|
|
|
|
|
|
|
|
|
|
private propagateChange = (v: any) => {};
|
|
|
|
|
|
2023-02-02 15:55:06 +02:00
|
|
|
constructor(public fb: UntypedFormBuilder) {
|
2020-09-29 10:33:29 +03:00
|
|
|
this.deviceCredentialsFormGroup = this.fb.group({
|
|
|
|
|
credentialsType: [DeviceCredentialsType.ACCESS_TOKEN],
|
|
|
|
|
credentialsId: [null],
|
2021-07-07 15:25:48 +03:00
|
|
|
credentialsValue: [null]
|
2020-09-29 10:33:29 +03:00
|
|
|
});
|
2021-04-29 18:25:38 +03:00
|
|
|
this.deviceCredentialsFormGroup.valueChanges.pipe(
|
|
|
|
|
takeUntil(this.destroy$)
|
|
|
|
|
).subscribe(() => {
|
|
|
|
|
this.updateView();
|
|
|
|
|
});
|
|
|
|
|
this.deviceCredentialsFormGroup.get('credentialsType').valueChanges.pipe(
|
|
|
|
|
takeUntil(this.destroy$)
|
2021-05-13 16:20:39 +03:00
|
|
|
).subscribe(() => {
|
|
|
|
|
this.credentialsTypeChanged();
|
2021-04-29 18:25:38 +03:00
|
|
|
});
|
2020-09-29 10:33:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
|
if (this.disabled) {
|
|
|
|
|
this.deviceCredentialsFormGroup.disable({emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
2021-04-29 18:25:38 +03:00
|
|
|
this.destroy$.next();
|
|
|
|
|
this.destroy$.complete();
|
2020-09-29 10:33:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(value: DeviceCredentials | null): void {
|
|
|
|
|
if (isDefinedAndNotNull(value)) {
|
2021-07-07 15:25:48 +03:00
|
|
|
const credentialsType = this.credentialsTypes.includes(value.credentialsType) ? value.credentialsType : this.credentialsTypes[0];
|
2020-09-29 10:33:29 +03:00
|
|
|
this.deviceCredentialsFormGroup.patchValue({
|
2021-07-07 15:25:48 +03:00
|
|
|
credentialsType,
|
2020-09-29 10:33:29 +03:00
|
|
|
credentialsId: value.credentialsId,
|
2021-07-07 15:25:48 +03:00
|
|
|
credentialsValue: value.credentialsValue
|
2020-09-29 10:33:29 +03:00
|
|
|
}, {emitEvent: false});
|
|
|
|
|
this.updateValidators();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateView() {
|
|
|
|
|
const deviceCredentialsValue = this.deviceCredentialsFormGroup.value;
|
|
|
|
|
this.propagateChange(deviceCredentialsValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
|
|
|
|
if (this.disabled) {
|
|
|
|
|
this.deviceCredentialsFormGroup.disable({emitEvent: false});
|
|
|
|
|
} else {
|
|
|
|
|
this.deviceCredentialsFormGroup.enable({emitEvent: false});
|
|
|
|
|
this.updateValidators();
|
2021-06-02 12:43:54 +03:00
|
|
|
this.deviceCredentialsFormGroup.updateValueAndValidity();
|
2020-09-29 10:33:29 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-02 15:55:06 +02:00
|
|
|
public validate(c: UntypedFormControl) {
|
2020-09-29 10:33:29 +03:00
|
|
|
return this.deviceCredentialsFormGroup.valid ? null : {
|
|
|
|
|
deviceCredentials: {
|
|
|
|
|
valid: false,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 16:20:39 +03:00
|
|
|
credentialsTypeChanged(): void {
|
2020-09-29 10:33:29 +03:00
|
|
|
this.deviceCredentialsFormGroup.patchValue({
|
|
|
|
|
credentialsId: null,
|
2021-07-07 15:25:48 +03:00
|
|
|
credentialsValue: null
|
2020-09-29 10:33:29 +03:00
|
|
|
});
|
|
|
|
|
this.updateValidators();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateValidators(): void {
|
2020-10-13 09:09:12 +03:00
|
|
|
const credentialsType = this.deviceCredentialsFormGroup.get('credentialsType').value as DeviceCredentialsType;
|
|
|
|
|
switch (credentialsType) {
|
2020-09-29 10:33:29 +03:00
|
|
|
case DeviceCredentialsType.ACCESS_TOKEN:
|
2021-07-29 07:55:01 +03:00
|
|
|
this.deviceCredentialsFormGroup.get('credentialsId').setValidators([Validators.required, Validators.pattern(/^.{1,32}$/)]);
|
2020-09-29 10:33:29 +03:00
|
|
|
this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity({emitEvent: false});
|
|
|
|
|
this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([]);
|
|
|
|
|
this.deviceCredentialsFormGroup.get('credentialsValue').updateValueAndValidity({emitEvent: false});
|
|
|
|
|
break;
|
2021-07-07 15:25:48 +03:00
|
|
|
default:
|
2021-05-13 16:20:39 +03:00
|
|
|
this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([Validators.required]);
|
2021-02-19 23:15:20 +02:00
|
|
|
this.deviceCredentialsFormGroup.get('credentialsValue').updateValueAndValidity({emitEvent: false});
|
|
|
|
|
this.deviceCredentialsFormGroup.get('credentialsId').setValidators([]);
|
|
|
|
|
this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity({emitEvent: false});
|
|
|
|
|
break;
|
2020-09-29 10:33:29 +03:00
|
|
|
}
|
|
|
|
|
}
|
2023-05-03 14:59:41 +03:00
|
|
|
|
|
|
|
|
public generate(formControlName: string) {
|
2023-05-03 15:59:31 +03:00
|
|
|
this.deviceCredentialsFormGroup.get(formControlName).patchValue(generateSecret(20));
|
2023-05-03 14:59:41 +03:00
|
|
|
}
|
2020-09-29 10:33:29 +03:00
|
|
|
}
|