|
|
|
@ -2,6 +2,7 @@ |
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
import { Component, forwardRef, Input, OnInit } from '@angular/core'; |
|
|
|
import { |
|
|
|
AbstractControl, |
|
|
|
ControlValueAccessor, |
|
|
|
NG_VALIDATORS, |
|
|
|
NG_VALUE_ACCESSOR, |
|
|
|
@ -11,7 +12,7 @@ import { |
|
|
|
Validator, |
|
|
|
Validators |
|
|
|
} from '@angular/forms'; |
|
|
|
import { baseUrl, isDefinedAndNotNull } from '@core/utils'; |
|
|
|
import { baseUrl, isDefinedAndNotNull, isNotEmptyStr } from '@core/utils'; |
|
|
|
import { takeUntil } from 'rxjs/operators'; |
|
|
|
import { |
|
|
|
IntegrationCredentialType, |
|
|
|
@ -25,10 +26,15 @@ import { AppState } from '@core/core.state'; |
|
|
|
import { TranslateService } from '@ngx-translate/core'; |
|
|
|
import { IntegrationForm } from '@home/components/integration/configuration/integration-form'; |
|
|
|
|
|
|
|
interface KpnIntegrationFormValue extends Omit<KpnIntegration, 'enableSecurity' | 'headersFilter'> { |
|
|
|
securityHeader: string; |
|
|
|
additionalHeadersFilter: {[key: string]: string}; |
|
|
|
} |
|
|
|
|
|
|
|
@Component({ |
|
|
|
selector: 'tb-kpn-integration-form', |
|
|
|
templateUrl: './kpn-integration-form.component.html', |
|
|
|
styleUrls: ['./kpn-integration-form.component.scss'], |
|
|
|
styleUrls: [], |
|
|
|
providers: [{ |
|
|
|
provide: NG_VALUE_ACCESSOR, |
|
|
|
useExisting: forwardRef(() => KpnIntegrationFormComponent), |
|
|
|
@ -47,6 +53,10 @@ export class KpnIntegrationFormComponent extends IntegrationForm implements Cont |
|
|
|
|
|
|
|
IntegrationCredentialType = IntegrationCredentialType; |
|
|
|
|
|
|
|
httpsDestinationSettingsExpanded = false; |
|
|
|
|
|
|
|
noSecurity = true; |
|
|
|
|
|
|
|
@Input() |
|
|
|
routingKey: string; |
|
|
|
|
|
|
|
@ -63,6 +73,7 @@ export class KpnIntegrationFormComponent extends IntegrationForm implements Cont |
|
|
|
} |
|
|
|
|
|
|
|
private integrationType = IntegrationType.KPN; |
|
|
|
private securityHeaderName = 'X-Things-Secret'; |
|
|
|
private isSetDownlinkValue = false; |
|
|
|
|
|
|
|
private propagateChangePending = false; |
|
|
|
@ -82,13 +93,13 @@ export class KpnIntegrationFormComponent extends IntegrationForm implements Cont |
|
|
|
} |
|
|
|
this.kpnIntegrationConfigForm = this.fb.group({ |
|
|
|
baseUrl: [baseUrl(), baseURLValidators], |
|
|
|
destinationSharedSecret: [""], |
|
|
|
httpEndpoint: [{ |
|
|
|
value: integrationEndPointUrl(this.integrationType, baseUrl(), this.routingKey), |
|
|
|
disabled: true |
|
|
|
}], |
|
|
|
enableSecurity: [false], |
|
|
|
headersFilter: [{}], |
|
|
|
securityHeader: [''], |
|
|
|
destinationSharedSecret: [''], |
|
|
|
additionalHeadersFilter: [{}, (control: AbstractControl) => this.additionalHeadersFilterValidator(control)], |
|
|
|
apiId: [], |
|
|
|
apiKey: [] |
|
|
|
}); |
|
|
|
@ -101,7 +112,9 @@ export class KpnIntegrationFormComponent extends IntegrationForm implements Cont |
|
|
|
this.kpnIntegrationConfigForm.valueChanges.pipe( |
|
|
|
takeUntil(this.destroy$) |
|
|
|
).subscribe(() => { |
|
|
|
this.updateModels(this.kpnIntegrationConfigForm.getRawValue()); |
|
|
|
const value = this.kpnIntegrationConfigForm.getRawValue(); |
|
|
|
this.updateNoSecurity(value); |
|
|
|
this.updateModels(value); |
|
|
|
}); |
|
|
|
this.kpnIntegrationConfigForm.removeControl('replaceNoContentToOk', {emitEvent: false}); |
|
|
|
this.updateCredentialsValidators(); |
|
|
|
@ -109,7 +122,11 @@ export class KpnIntegrationFormComponent extends IntegrationForm implements Cont |
|
|
|
|
|
|
|
writeValue(value: KpnIntegration) { |
|
|
|
if (isDefinedAndNotNull(value)) { |
|
|
|
this.kpnIntegrationConfigForm.reset(value, {emitEvent: false}); |
|
|
|
const formValue = this.configToFormValue(value); |
|
|
|
this.httpsDestinationSettingsExpanded = isNotEmptyStr(formValue.destinationSharedSecret) || |
|
|
|
Object.keys(formValue.additionalHeadersFilter).length > 0; |
|
|
|
this.kpnIntegrationConfigForm.reset(formValue, {emitEvent: false}); |
|
|
|
this.updateNoSecurity(this.kpnIntegrationConfigForm.getRawValue()); |
|
|
|
} else { |
|
|
|
this.propagateChangePending = true; |
|
|
|
} |
|
|
|
@ -159,8 +176,41 @@ export class KpnIntegrationFormComponent extends IntegrationForm implements Cont |
|
|
|
})); |
|
|
|
} |
|
|
|
|
|
|
|
private updateModels(value) { |
|
|
|
this.propagateChange(value); |
|
|
|
private updateNoSecurity({securityHeader, destinationSharedSecret, additionalHeadersFilter}: KpnIntegrationFormValue) { |
|
|
|
this.noSecurity = !isNotEmptyStr(securityHeader) && !isNotEmptyStr(destinationSharedSecret) && |
|
|
|
!Object.keys(additionalHeadersFilter).length; |
|
|
|
} |
|
|
|
|
|
|
|
private updateModels(value: KpnIntegrationFormValue) { |
|
|
|
this.propagateChange(this.formValueToConfig(value)); |
|
|
|
} |
|
|
|
|
|
|
|
private configToFormValue({enableSecurity, headersFilter, ...config}: KpnIntegration): KpnIntegrationFormValue { |
|
|
|
const additionalHeadersFilter = enableSecurity ? {...headersFilter} : {}; |
|
|
|
const securityHeaderKey = Object.keys(additionalHeadersFilter).find(key => this.isSecurityHeader(key)); |
|
|
|
let securityHeader = ''; |
|
|
|
if (securityHeaderKey) { |
|
|
|
securityHeader = additionalHeadersFilter[securityHeaderKey]; |
|
|
|
delete additionalHeadersFilter[securityHeaderKey]; |
|
|
|
} |
|
|
|
return {...config, securityHeader, additionalHeadersFilter}; |
|
|
|
} |
|
|
|
|
|
|
|
private formValueToConfig({securityHeader, additionalHeadersFilter, ...config}: KpnIntegrationFormValue): KpnIntegration { |
|
|
|
const headersFilter = {...additionalHeadersFilter}; |
|
|
|
if (isNotEmptyStr(securityHeader)) { |
|
|
|
headersFilter[this.securityHeaderName] = securityHeader.trim(); |
|
|
|
} |
|
|
|
const enableSecurity = Object.keys(headersFilter).length > 0; |
|
|
|
return {...config, enableSecurity, headersFilter}; |
|
|
|
} |
|
|
|
|
|
|
|
private additionalHeadersFilterValidator(control: AbstractControl): ValidationErrors | null { |
|
|
|
return Object.keys(control.value ?? {}).some(key => this.isSecurityHeader(key)) ? {securityHeaderDuplicate: true} : null; |
|
|
|
} |
|
|
|
|
|
|
|
private isSecurityHeader(key: string): boolean { |
|
|
|
return key.toLowerCase() === this.securityHeaderName.toLowerCase(); |
|
|
|
} |
|
|
|
|
|
|
|
private updateCredentialsValidators() { |
|
|
|
|