fixed security

This commit is contained in:
mpetrov 2024-06-27 17:16:13 +03:00
parent 4dd87d5fbb
commit baaca20017
3 changed files with 49 additions and 20 deletions

View File

@ -26,13 +26,15 @@ import { Subject } from 'rxjs';
import { import {
ControlValueAccessor, ControlValueAccessor,
FormBuilder, FormBuilder,
NG_VALIDATORS,
NG_VALUE_ACCESSOR, NG_VALUE_ACCESSOR,
UntypedFormGroup, UntypedFormGroup,
ValidationErrors,
Validators Validators
} from '@angular/forms'; } from '@angular/forms';
import { import {
BrokerSecurityType, SecurityType,
BrokerSecurityTypeTranslationsMap, SecurityTypeTranslationsMap,
ModeType, ModeType,
noLeadTrailSpacesRegex noLeadTrailSpacesRegex
} from '@home/components/widget/lib/gateway/gateway-widget.models'; } from '@home/components/widget/lib/gateway/gateway-widget.models';
@ -52,6 +54,11 @@ import { CommonModule } from '@angular/common';
useExisting: forwardRef(() => SecurityConfigComponent), useExisting: forwardRef(() => SecurityConfigComponent),
multi: true multi: true
}, },
{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => SecurityConfigComponent),
multi: true
}
], ],
standalone: true, standalone: true,
imports:[ imports:[
@ -67,23 +74,25 @@ export class SecurityConfigComponent implements ControlValueAccessor, OnInit, On
@coerceBoolean() @coerceBoolean()
extendCertificatesModel = false; extendCertificatesModel = false;
BrokerSecurityType = BrokerSecurityType; BrokerSecurityType = SecurityType;
securityTypes = Object.values(BrokerSecurityType); securityTypes = Object.values(SecurityType);
modeTypes = Object.values(ModeType); modeTypes = Object.values(ModeType);
SecurityTypeTranslationsMap = BrokerSecurityTypeTranslationsMap; SecurityTypeTranslationsMap = SecurityTypeTranslationsMap;
securityFormGroup: UntypedFormGroup; securityFormGroup: UntypedFormGroup;
private destroy$ = new Subject<void>(); private destroy$ = new Subject<void>();
private propagateChange = (v: any) => {};
constructor(private fb: FormBuilder) {} constructor(private fb: FormBuilder) {}
ngOnInit(): void { ngOnInit(): void {
this.securityFormGroup = this.fb.group({ this.securityFormGroup = this.fb.group({
type: [BrokerSecurityType.ANONYMOUS, []], type: [SecurityType.ANONYMOUS, []],
username: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], username: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]],
password: ['', [Validators.pattern(noLeadTrailSpacesRegex)]], password: ['', [Validators.pattern(noLeadTrailSpacesRegex)]],
pathToCACert: ['', [Validators.pattern(noLeadTrailSpacesRegex)]], pathToCACert: ['', [Validators.pattern(noLeadTrailSpacesRegex)]],
@ -93,11 +102,12 @@ export class SecurityConfigComponent implements ControlValueAccessor, OnInit, On
if (this.extendCertificatesModel) { if (this.extendCertificatesModel) {
this.securityFormGroup.addControl('mode', this.fb.control(ModeType.NONE, [])); this.securityFormGroup.addControl('mode', this.fb.control(ModeType.NONE, []));
} }
this.securityFormGroup.valueChanges.pipe(
takeUntil(this.destroy$)
).subscribe((value) => this.updateView(value));
this.securityFormGroup.get('type').valueChanges.pipe( this.securityFormGroup.get('type').valueChanges.pipe(
takeUntil(this.destroy$) takeUntil(this.destroy$)
).subscribe((type) => { ).subscribe((type) => this.updateValidators(type));
this.updateValidators(type);
});
} }
ngOnDestroy(): void { ngOnDestroy(): void {
@ -105,13 +115,31 @@ export class SecurityConfigComponent implements ControlValueAccessor, OnInit, On
this.destroy$.complete(); this.destroy$.complete();
} }
registerOnChange(fn: any): void {} writeValue(deviceInfo: any) {
if (!deviceInfo.type) {
deviceInfo.type = SecurityType.ANONYMOUS;
}
this.securityFormGroup.reset(deviceInfo);
this.updateView(deviceInfo);
}
validate(): ValidationErrors | null {
return this.securityFormGroup.valid ? null : {
securityForm: { valid: false }
};
}
updateView(value: any) {
this.propagateChange(value);
}
registerOnChange(fn: any): void {
this.propagateChange = fn;
}
registerOnTouched(fn: any): void {} registerOnTouched(fn: any): void {}
writeValue(obj: any): void {} private updateValidators(type: SecurityType): void {
private updateValidators(type): void {
if (type) { if (type) {
this.securityFormGroup.get('username').disable({emitEvent: false}); this.securityFormGroup.get('username').disable({emitEvent: false});
this.securityFormGroup.get('password').disable({emitEvent: false}); this.securityFormGroup.get('password').disable({emitEvent: false});
@ -119,10 +147,10 @@ export class SecurityConfigComponent implements ControlValueAccessor, OnInit, On
this.securityFormGroup.get('pathToPrivateKey').disable({emitEvent: false}); this.securityFormGroup.get('pathToPrivateKey').disable({emitEvent: false});
this.securityFormGroup.get('pathToClientCert').disable({emitEvent: false}); this.securityFormGroup.get('pathToClientCert').disable({emitEvent: false});
this.securityFormGroup.get('mode')?.disable({emitEvent: false}); this.securityFormGroup.get('mode')?.disable({emitEvent: false});
if (type === BrokerSecurityType.BASIC) { if (type === SecurityType.BASIC) {
this.securityFormGroup.get('username').enable({emitEvent: false}); this.securityFormGroup.get('username').enable({emitEvent: false});
this.securityFormGroup.get('password').enable({emitEvent: false}); this.securityFormGroup.get('password').enable({emitEvent: false});
} else if (type === BrokerSecurityType.CERTIFICATES) { } else if (type === SecurityType.CERTIFICATES) {
this.securityFormGroup.get('pathToCACert').enable({emitEvent: false}); this.securityFormGroup.get('pathToCACert').enable({emitEvent: false});
this.securityFormGroup.get('pathToPrivateKey').enable({emitEvent: false}); this.securityFormGroup.get('pathToPrivateKey').enable({emitEvent: false});
this.securityFormGroup.get('pathToClientCert').enable({emitEvent: false}); this.securityFormGroup.get('pathToClientCert').enable({emitEvent: false});

View File

@ -84,6 +84,7 @@ export class ServerConfigComponent implements OnInit, ControlValueAccessor, OnDe
ngOnInit(): void { ngOnInit(): void {
this.addSelfControl(); this.addSelfControl();
} }
ngOnDestroy(): void { ngOnDestroy(): void {

View File

@ -474,7 +474,7 @@ export enum ConnectorConfigurationModes {
ADVANCED = 'advanced' ADVANCED = 'advanced'
} }
export enum BrokerSecurityType { export enum SecurityType {
ANONYMOUS = 'anonymous', ANONYMOUS = 'anonymous',
BASIC = 'basic', BASIC = 'basic',
CERTIFICATES = 'certificates' CERTIFICATES = 'certificates'
@ -486,11 +486,11 @@ export enum ModeType {
SIGNANDENCRYPT = 'SignAndEncrypt' SIGNANDENCRYPT = 'SignAndEncrypt'
} }
export const BrokerSecurityTypeTranslationsMap = new Map<BrokerSecurityType, string>( export const SecurityTypeTranslationsMap = new Map<SecurityType, string>(
[ [
[BrokerSecurityType.ANONYMOUS, 'gateway.broker.security-types.anonymous'], [SecurityType.ANONYMOUS, 'gateway.broker.security-types.anonymous'],
[BrokerSecurityType.BASIC, 'gateway.broker.security-types.basic'], [SecurityType.BASIC, 'gateway.broker.security-types.basic'],
[BrokerSecurityType.CERTIFICATES, 'gateway.broker.security-types.certificates'] [SecurityType.CERTIFICATES, 'gateway.broker.security-types.certificates']
] ]
); );