Browse Source

lwm2m: front fix bug create device lwm2mConfig default

pull/4313/head
nickAS21 6 years ago
parent
commit
0710d272a1
  1. 8
      ui-ngx/src/app/modules/home/components/device/device-credentials.component.html
  2. 17
      ui-ngx/src/app/modules/home/components/device/device-credentials.component.ts
  3. 2
      ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-profile-transport-configuration.component.html
  4. 49
      ui-ngx/src/app/modules/home/pages/device/lwm2m/security-config.component.ts
  5. 68
      ui-ngx/src/app/modules/home/pages/device/lwm2m/security-config.models.ts
  6. 4
      ui-ngx/src/assets/locale/locale.constant-en_US.json

8
ui-ngx/src/app/modules/home/components/device/device-credentials.component.html

@ -84,16 +84,18 @@
</mat-form-field> </mat-form-field>
<mat-form-field class="mat-block"> <mat-form-field class="mat-block">
<mat-label translate>device.lwm2m-value</mat-label> <mat-label translate>device.lwm2m-value</mat-label>
<textarea matInput formControlName="credentialsValue" rows="10" required></textarea> <textarea matInput formControlName="credentialsValue" rows="10" required
[matTooltip]="lwm2mCredentialsValueTip(deviceCredentialsFormGroup.get('credentialsValue').hasError('jsonError'))"
matTooltipPosition="above"
></textarea>
<mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsValue').hasError('required')"> <mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsValue').hasError('required')">
{{ 'device.lwm2m-value-required' | translate }} {{ 'device.lwm2m-value-required' | translate }}
</mat-error> </mat-error>
<mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsValue').hasError('jsonError')"> <mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsValue').hasError('jsonError')">
{{ 'device.lwm2m-value-json-error' | translate }} {{ 'device.lwm2m-value-format-error' | translate }}
</mat-error> </mat-error>
<div mat-dialog-actions fxLayoutAlign="center center"> <div mat-dialog-actions fxLayoutAlign="center center">
<button mat-raised-button color="primary" <button mat-raised-button color="primary"
matTooltip="{{'device.lwm2m-value-edit-tip' | translate }}"
(click)="openSecurityInfoLwM2mDialog($event)" (click)="openSecurityInfoLwM2mDialog($event)"
> >
{{'device.lwm2m-value-edit' | translate }} {{'device.lwm2m-value-edit' | translate }}

17
ui-ngx/src/app/modules/home/components/device/device-credentials.component.ts

@ -37,11 +37,13 @@ import {Subscription} from 'rxjs';
import {distinctUntilChanged} from 'rxjs/operators'; import {distinctUntilChanged} from 'rxjs/operators';
import {SecurityConfigComponent} from '@home/pages/device/lwm2m/security-config.component'; import {SecurityConfigComponent} from '@home/pages/device/lwm2m/security-config.component';
import { import {
ClientSecurityConfig,
DEFAULT_END_POINT, DEFAULT_END_POINT,
DeviceCredentialsDialogLwm2mData, DeviceCredentialsDialogLwm2mData,
END_POINT, END_POINT,
getDefaultSecurityConfig, getDefaultSecurityConfig,
JSON_ALL_CONFIG, SecurityConfigModels, validateSecurityConfig JSON_ALL_CONFIG,
validateSecurityConfig
} from '@home/pages/device/lwm2m/security-config.models'; } from '@home/pages/device/lwm2m/security-config.models';
import {TranslateService} from '@ngx-translate/core'; import {TranslateService} from '@ngx-translate/core';
import {MatDialog} from '@angular/material/dialog'; import {MatDialog} from '@angular/material/dialog';
@ -129,6 +131,8 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit,
let credentialsValue = null; let credentialsValue = null;
if (value.credentialsType === DeviceCredentialsType.MQTT_BASIC) { if (value.credentialsType === DeviceCredentialsType.MQTT_BASIC) {
credentialsBasic = JSON.parse(value.credentialsValue) as DeviceCredentialMQTTBasic; credentialsBasic = JSON.parse(value.credentialsValue) as DeviceCredentialMQTTBasic;
} else if (value.credentialsType === DeviceCredentialsType.LWM2M_CREDENTIALS) {
credentialsValue = JSON.parse(JSON.stringify(value.credentialsValue)) as ClientSecurityConfig;
} else { } else {
credentialsValue = value.credentialsValue; credentialsValue = value.credentialsValue;
} }
@ -178,7 +182,7 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit,
credentialsTypeChanged(): void { credentialsTypeChanged(): void {
this.deviceCredentialsFormGroup.patchValue({ this.deviceCredentialsFormGroup.patchValue({
credentialsId: null, credentialsId: null,
credentialsValue: null, credentialsValue: JSON.stringify(getDefaultSecurityConfig(), null, 2),
credentialsBasic: {clientId: '', userName: '', password: ''} credentialsBasic: {clientId: '', userName: '', password: ''}
}); });
this.updateValidators(); this.updateValidators();
@ -203,7 +207,7 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit,
this.deviceCredentialsFormGroup.get('credentialsBasic').disable({emitEvent: false}); this.deviceCredentialsFormGroup.get('credentialsBasic').disable({emitEvent: false});
break; break;
case DeviceCredentialsType.LWM2M_CREDENTIALS: case DeviceCredentialsType.LWM2M_CREDENTIALS:
this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([Validators.required, this.jsonValidator]); this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([Validators.required, this.lwm2mConfigJsonValidator]);
this.deviceCredentialsFormGroup.get('credentialsValue').updateValueAndValidity({emitEvent: false}); this.deviceCredentialsFormGroup.get('credentialsValue').updateValueAndValidity({emitEvent: false});
this.deviceCredentialsFormGroup.get('credentialsId').setValidators([]); this.deviceCredentialsFormGroup.get('credentialsId').setValidators([]);
this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity({emitEvent: false}); this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity({emitEvent: false});
@ -284,7 +288,12 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit,
return Object.keys(response).length === 0 || JSON.stringify(response) === '[{}]'; return Object.keys(response).length === 0 || JSON.stringify(response) === '[{}]';
} }
private jsonValidator(control: FormControl) { private lwm2mConfigJsonValidator(control: FormControl) {
return validateSecurityConfig(control.value) ? null: {jsonError: {parsedJson: "error"}}; return validateSecurityConfig(control.value) ? null: {jsonError: {parsedJson: "error"}};
} }
lwm2mCredentialsValueTip (flag: boolean): string {
let jsonConfigDef = JSON.stringify(getDefaultSecurityConfig(), null, 2);
return !flag ? "" : 'Example (mode=\"NoSec\"):\n\r ' + jsonConfigDef;
}
} }

2
ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-profile-transport-configuration.component.html

@ -20,7 +20,7 @@
<mat-tab label="{{ 'device-profile.lwm2m.model-tab' | translate }}"> <mat-tab label="{{ 'device-profile.lwm2m.model-tab' | translate }}">
<ng-template matTabContent> <ng-template matTabContent>
<section [formGroup]="lwm2mDeviceProfileFormGroup"> <section [formGroup]="lwm2mDeviceProfileFormGroup">
<div class="mat-padding" style="padding-bottom: 0px"> <div *ngIf="false" class="mat-padding" style="padding-bottom: 0px">
<mat-form-field class="mat-block"> <mat-form-field class="mat-block">
<mat-label>{{ 'device-profile.lwm2m.client-only-observe-after-connect-label' | translate }}</mat-label> <mat-label>{{ 'device-profile.lwm2m.client-only-observe-after-connect-label' | translate }}</mat-label>
<mat-select formControlName="clientOnlyObserveAfterConnect" <mat-select formControlName="clientOnlyObserveAfterConnect"

49
ui-ngx/src/app/modules/home/pages/device/lwm2m/security-config.component.ts

@ -15,23 +15,20 @@
/// ///
import { Component, Inject, OnInit } from '@angular/core'; import {Component, Inject, OnInit} from '@angular/core';
import { DialogComponent } from '@shared/components/dialog.component'; import {DialogComponent} from '@shared/components/dialog.component';
import { Store } from '@ngrx/store'; import {Store} from '@ngrx/store';
import { AppState } from '@core/core.state'; import {AppState} from '@core/core.state';
import { Router } from '@angular/router'; import {Router} from '@angular/router';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import { TranslateService } from '@ngx-translate/core'; import {TranslateService} from '@ngx-translate/core';
import { import {
BOOTSTRAP_SERVER, BOOTSTRAP_SERVER,
BOOTSTRAP_SERVERS, BOOTSTRAP_SERVERS,
ClientSecurityConfigNoSEC, ClientSecurityConfig,
ClientSecurityConfigPSK,
ClientSecurityConfigRPK,
ClientSecurityConfigX509,
DeviceCredentialsDialogLwm2mData, DeviceCredentialsDialogLwm2mData,
getDefaultClientSecurityConfigType, getClientSecurityConfig,
JSON_ALL_CONFIG, JSON_ALL_CONFIG,
KEY_REGEXP_HEX_DEC, KEY_REGEXP_HEX_DEC,
LEN_MAX_PSK, LEN_MAX_PSK,
@ -41,9 +38,9 @@ import {
SECURITY_CONFIG_MODE_NAMES, SECURITY_CONFIG_MODE_NAMES,
SecurityConfigModels SecurityConfigModels
} from './security-config.models'; } from './security-config.models';
import { WINDOW } from '@core/services/window.service'; import {WINDOW} from '@core/services/window.service';
import { MatTabChangeEvent } from '@angular/material/tabs'; import {MatTabChangeEvent} from '@angular/material/tabs';
import { MatTab } from '@angular/material/tabs/tab'; import {MatTab} from '@angular/material/tabs/tab';
@Component({ @Component({
selector: 'tb-security-config-lwm2m', selector: 'tb-security-config-lwm2m',
@ -115,20 +112,20 @@ export class SecurityConfigComponent extends DialogComponent<SecurityConfigCompo
case SECURITY_CONFIG_MODE.NO_SEC: case SECURITY_CONFIG_MODE.NO_SEC:
break; break;
case SECURITY_CONFIG_MODE.PSK: case SECURITY_CONFIG_MODE.PSK:
const clientSecurityConfigPSK = jsonAllConfig.client as ClientSecurityConfigPSK; const clientSecurityConfigPSK = jsonAllConfig.client as ClientSecurityConfig;
this.lwm2mConfigFormGroup.patchValue({ this.lwm2mConfigFormGroup.patchValue({
identityPSK: clientSecurityConfigPSK.identity, identityPSK: clientSecurityConfigPSK.identity,
clientKey: clientSecurityConfigPSK.key, clientKey: clientSecurityConfigPSK.key,
}, {emitEvent: false}); }, {emitEvent: false});
break; break;
case SECURITY_CONFIG_MODE.RPK: case SECURITY_CONFIG_MODE.RPK:
const clientSecurityConfigRPK = jsonAllConfig.client as ClientSecurityConfigRPK; const clientSecurityConfigRPK = jsonAllConfig.client as ClientSecurityConfig;
this.lwm2mConfigFormGroup.patchValue({ this.lwm2mConfigFormGroup.patchValue({
clientKey: clientSecurityConfigRPK.key, clientKey: clientSecurityConfigRPK.key,
}, {emitEvent: false}); }, {emitEvent: false});
break; break;
case SECURITY_CONFIG_MODE.X509: case SECURITY_CONFIG_MODE.X509:
const clientSecurityConfigX509 = jsonAllConfig.client as ClientSecurityConfigX509; const clientSecurityConfigX509 = jsonAllConfig.client as ClientSecurityConfig;
this.lwm2mConfigFormGroup.patchValue({ this.lwm2mConfigFormGroup.patchValue({
clientCertificate: clientSecurityConfigX509.x509 clientCertificate: clientSecurityConfigX509.x509
}, {emitEvent: false}); }, {emitEvent: false});
@ -140,7 +137,7 @@ export class SecurityConfigComponent extends DialogComponent<SecurityConfigCompo
securityConfigClientModeChanged = (mode: SECURITY_CONFIG_MODE): void => { securityConfigClientModeChanged = (mode: SECURITY_CONFIG_MODE): void => {
switch (mode) { switch (mode) {
case SECURITY_CONFIG_MODE.NO_SEC: case SECURITY_CONFIG_MODE.NO_SEC:
const clientSecurityConfigNoSEC = getDefaultClientSecurityConfigType(mode) as ClientSecurityConfigNoSEC; const clientSecurityConfigNoSEC = getClientSecurityConfig(mode) as ClientSecurityConfig;
this.jsonAllConfig.client = clientSecurityConfigNoSEC; this.jsonAllConfig.client = clientSecurityConfigNoSEC;
this.lwm2mConfigFormGroup.patchValue({ this.lwm2mConfigFormGroup.patchValue({
jsonAllConfig: this.jsonAllConfig, jsonAllConfig: this.jsonAllConfig,
@ -148,8 +145,8 @@ export class SecurityConfigComponent extends DialogComponent<SecurityConfigCompo
}, {emitEvent: false}); }, {emitEvent: false});
break; break;
case SECURITY_CONFIG_MODE.PSK: case SECURITY_CONFIG_MODE.PSK:
const clientSecurityConfigPSK = getDefaultClientSecurityConfigType(mode, this.lwm2mConfigFormGroup.get('endPoint') const clientSecurityConfigPSK = getClientSecurityConfig(mode, this.lwm2mConfigFormGroup.get('endPoint')
.value) as ClientSecurityConfigPSK; .value) as ClientSecurityConfig;
clientSecurityConfigPSK.identity = this.data.endPoint; clientSecurityConfigPSK.identity = this.data.endPoint;
clientSecurityConfigPSK.key = this.lwm2mConfigFormGroup.get('clientKey').value; clientSecurityConfigPSK.key = this.lwm2mConfigFormGroup.get('clientKey').value;
this.jsonAllConfig.client = clientSecurityConfigPSK; this.jsonAllConfig.client = clientSecurityConfigPSK;
@ -159,7 +156,7 @@ export class SecurityConfigComponent extends DialogComponent<SecurityConfigCompo
}, {emitEvent: false}); }, {emitEvent: false});
break; break;
case SECURITY_CONFIG_MODE.RPK: case SECURITY_CONFIG_MODE.RPK:
const clientSecurityConfigRPK = getDefaultClientSecurityConfigType(mode) as ClientSecurityConfigRPK; const clientSecurityConfigRPK = getClientSecurityConfig(mode) as ClientSecurityConfig;
clientSecurityConfigRPK.key = this.lwm2mConfigFormGroup.get('clientKey').value; clientSecurityConfigRPK.key = this.lwm2mConfigFormGroup.get('clientKey').value;
this.jsonAllConfig.client = clientSecurityConfigRPK; this.jsonAllConfig.client = clientSecurityConfigRPK;
this.lwm2mConfigFormGroup.patchValue({ this.lwm2mConfigFormGroup.patchValue({
@ -167,7 +164,7 @@ export class SecurityConfigComponent extends DialogComponent<SecurityConfigCompo
}, {emitEvent: false}); }, {emitEvent: false});
break; break;
case SECURITY_CONFIG_MODE.X509: case SECURITY_CONFIG_MODE.X509:
this.jsonAllConfig.client = getDefaultClientSecurityConfigType(mode) as ClientSecurityConfigX509; this.jsonAllConfig.client = getClientSecurityConfig(mode) as ClientSecurityConfig;
this.lwm2mConfigFormGroup.patchValue({ this.lwm2mConfigFormGroup.patchValue({
clientCertificate: true clientCertificate: true
}, {emitEvent: false}); }, {emitEvent: false});
@ -236,11 +233,11 @@ export class SecurityConfigComponent extends DialogComponent<SecurityConfigCompo
if (this.lwm2mConfigFormGroup !== null) { if (this.lwm2mConfigFormGroup !== null) {
if (!this.lwm2mConfigFormGroup.get('endPoint').pristine && this.lwm2mConfigFormGroup.get('endPoint').valid) { if (!this.lwm2mConfigFormGroup.get('endPoint').pristine && this.lwm2mConfigFormGroup.get('endPoint').valid) {
this.data.endPoint = this.lwm2mConfigFormGroup.get('endPoint').value; this.data.endPoint = this.lwm2mConfigFormGroup.get('endPoint').value;
// Client mode == PSK /** Client mode == PSK */
if (this.lwm2mConfigFormGroup.get('securityConfigClientMode').value === SECURITY_CONFIG_MODE.PSK) { if (this.lwm2mConfigFormGroup.get('securityConfigClientMode').value === SECURITY_CONFIG_MODE.PSK) {
const endPoint = 'endpoint'; const endPoint = 'endpoint';
this.jsonAllConfig.client[endPoint] = this.data.endPoint; this.jsonAllConfig.client[endPoint] = this.data.endPoint;
this.jsonAllConfig.client[endPoint].markAsPristine({ this.lwm2mConfigFormGroup.get('endPoint').markAsPristine({
onlySelf: true onlySelf: true
}); });
this.upDateJsonAllConfig(); this.upDateJsonAllConfig();

68
ui-ngx/src/app/modules/home/pages/device/lwm2m/security-config.models.ts

@ -48,33 +48,14 @@ export const SECURITY_CONFIG_MODE_NAMES = new Map<SECURITY_CONFIG_MODE, string>(
] ]
); );
export type ClientSecurityConfigType = export interface ClientSecurityConfig {
ClientSecurityConfigPSK
| ClientSecurityConfigRPK
| ClientSecurityConfigX509
| ClientSecurityConfigNoSEC;
export interface ClientSecurityConfigPSK {
securityConfigClientMode: string; securityConfigClientMode: string;
endpoint: string; endpoint: string;
identity: string; identity: string;
key: string; key: string;
}
export interface ClientSecurityConfigRPK {
securityConfigClientMode: string;
key: string;
}
export interface ClientSecurityConfigX509 {
securityConfigClientMode: string;
x509: boolean; x509: boolean;
} }
export interface ClientSecurityConfigNoSEC {
securityConfigClientMode: string;
}
export interface ServerSecurityConfig { export interface ServerSecurityConfig {
securityMode: string; securityMode: string;
clientPublicKeyOrId?: string; clientPublicKeyOrId?: string;
@ -87,43 +68,36 @@ interface BootstrapSecurityConfig {
} }
export interface SecurityConfigModels { export interface SecurityConfigModels {
client: ClientSecurityConfigType; client: ClientSecurityConfig;
bootstrap: BootstrapSecurityConfig; bootstrap: BootstrapSecurityConfig;
} }
export function getDefaultClientSecurityConfigType(securityConfigMode: SECURITY_CONFIG_MODE, endPoint?: string): ClientSecurityConfigType { export function getClientSecurityConfig(securityConfigMode: SECURITY_CONFIG_MODE, endPoint?: string): ClientSecurityConfig {
let security: ClientSecurityConfigType; let security = getDefaultClientSecurityConfig();
security.securityConfigClientMode = securityConfigMode.toString();
switch (securityConfigMode) { switch (securityConfigMode) {
case SECURITY_CONFIG_MODE.PSK: case SECURITY_CONFIG_MODE.PSK:
security = { security.endpoint = endPoint;
securityConfigClientMode: '', security.identity = endPoint;
endpoint: endPoint,
identity: endPoint,
key: ''
};
break;
case SECURITY_CONFIG_MODE.RPK:
security = {
securityConfigClientMode: '',
key: ''
};
break; break;
case SECURITY_CONFIG_MODE.X509: case SECURITY_CONFIG_MODE.X509:
security = { security.x509 = true;
securityConfigClientMode: '',
x509: true
};
break;
case SECURITY_CONFIG_MODE.NO_SEC:
security = {
securityConfigClientMode: ''
};
break; break;
} }
security.securityConfigClientMode = securityConfigMode.toString();
return security; return security;
} }
export function getDefaultClientSecurityConfig(): ClientSecurityConfig {
return {
securityConfigClientMode: SECURITY_CONFIG_MODE.NO_SEC.toString(),
endpoint: '',
identity: '',
key: '',
x509: false
};
}
export function getDefaultServerSecurityConfig(): ServerSecurityConfig { export function getDefaultServerSecurityConfig(): ServerSecurityConfig {
return { return {
securityMode: SECURITY_CONFIG_MODE.NO_SEC.toString(), securityMode: SECURITY_CONFIG_MODE.NO_SEC.toString(),
@ -141,7 +115,7 @@ function getDefaultBootstrapSecurityConfig(): BootstrapSecurityConfig {
export function getDefaultSecurityConfig(): SecurityConfigModels { export function getDefaultSecurityConfig(): SecurityConfigModels {
const securityConfigModels = { const securityConfigModels = {
client: getDefaultClientSecurityConfigType(SECURITY_CONFIG_MODE.NO_SEC), client: getClientSecurityConfig(SECURITY_CONFIG_MODE.NO_SEC),
bootstrap: getDefaultBootstrapSecurityConfig() bootstrap: getDefaultBootstrapSecurityConfig()
}; };
return securityConfigModels; return securityConfigModels;
@ -153,7 +127,7 @@ const isSecurityConfigModels = (p: any): p is SecurityConfigModels =>
p.hasOwnProperty('bootstrap') && p.hasOwnProperty('bootstrap') &&
isBootstrapSecurityConfig(p['bootstrap']); isBootstrapSecurityConfig(p['bootstrap']);
const isClientSecurityConfigType = (p: any): p is ClientSecurityConfigType => const isClientSecurityConfigType = (p: any): p is ClientSecurityConfig =>
p.hasOwnProperty('securityConfigClientMode') && p.hasOwnProperty('securityConfigClientMode') &&
p.hasOwnProperty('endpoint') && p.hasOwnProperty('endpoint') &&
p.hasOwnProperty('identity') && p.hasOwnProperty('identity') &&

4
ui-ngx/src/assets/locale/locale.constant-en_US.json

@ -888,11 +888,11 @@
"lwm2m-key-required": "LwM2M Security config key is required.", "lwm2m-key-required": "LwM2M Security config key is required.",
"lwm2m-value": "LwM2M Security config", "lwm2m-value": "LwM2M Security config",
"lwm2m-value-required": "LwM2M Security config value is required.", "lwm2m-value-required": "LwM2M Security config value is required.",
"lwm2m-value-json-error": "LwM2M Security config value is not json format.", "lwm2m-value-format-error": "Security config value must be in LwM2M Security config format.",
"lwm2m-endpoint": "Client endpoint/identity", "lwm2m-endpoint": "Client endpoint/identity",
"lwm2m-security-info": "Security Config Info", "lwm2m-security-info": "Security Config Info",
"lwm2m-value-edit": "Edit Security config", "lwm2m-value-edit": "Edit Security config",
"lwm2m-value-edit-tip": "Edit security config json editor", "lwm2m-credentials-value-tip": "Edit security config json editor",
"lwm2m-security-config": { "lwm2m-security-config": {
"identity": "Client Identity", "identity": "Client Identity",
"client-key": "Client Key", "client-key": "Client Key",

Loading…
Cancel
Save