Commit d2469420adc40e1fefd0f880889fce7edf2621fc
1 parent
e0591bc6
Lwm2m: front add validator json for security config device
Showing
4 changed files
with
40 additions
and
30 deletions
... | ... | @@ -41,7 +41,7 @@ import { |
41 | 41 | DeviceCredentialsDialogLwm2mData, |
42 | 42 | END_POINT, |
43 | 43 | getDefaultSecurityConfig, |
44 | - JSON_ALL_CONFIG | |
44 | + JSON_ALL_CONFIG, SecurityConfigModels, validateSecurityConfig | |
45 | 45 | } from '@home/pages/device/lwm2m/security-config.models'; |
46 | 46 | import {TranslateService} from '@ngx-translate/core'; |
47 | 47 | import {MatDialog} from '@angular/material/dialog'; |
... | ... | @@ -285,15 +285,6 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit, |
285 | 285 | } |
286 | 286 | |
287 | 287 | private jsonValidator(control: FormControl) { |
288 | - try { | |
289 | - JSON.parse(control.value); | |
290 | - return null | |
291 | - } catch (e) { | |
292 | - return { | |
293 | - jsonError: { | |
294 | - parsedJson: "error" | |
295 | - } | |
296 | - } | |
297 | - } | |
288 | + return validateSecurityConfig(control.value) ? null: {jsonError: {parsedJson: "error"}}; | |
298 | 289 | } |
299 | 290 | } | ... | ... |
... | ... | @@ -395,21 +395,4 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro |
395 | 395 | } |
396 | 396 | }); |
397 | 397 | } |
398 | - | |
399 | - isPathInJson(path: string): boolean { | |
400 | - let isPath = this.findPathInJson(path, ATTRIBUTE); | |
401 | - if (!isPath) { | |
402 | - isPath = this.findPathInJson(path, TELEMETRY); | |
403 | - } | |
404 | - return !!isPath; | |
405 | - } | |
406 | - | |
407 | - private findPathInJson = (path: string, side: string): string => { | |
408 | - if (this.configurationValue.observeAttr) { | |
409 | - if (this.configurationValue.observeAttr[side]) { | |
410 | - return this.configurationValue.bootstrap[side].find( | |
411 | - pathJs => pathJs === path); | |
412 | - } | |
413 | - } | |
414 | - } | |
415 | 398 | } | ... | ... |
... | ... | @@ -27,7 +27,7 @@ export const DEFAULT_ID_SERVER = 123; |
27 | 27 | export const DEFAULT_ID_BOOTSTRAP = 111; |
28 | 28 | export const DEFAULT_HOST_NAME = 'localhost'; |
29 | 29 | export const DEFAULT_PORT_SERVER_NO_SEC = 5685; |
30 | -export const DEFAULT_PORT_BOOTSTRAP_NO_SEC = 5686; | |
30 | +export const DEFAULT_PORT_BOOTSTRAP_NO_SEC = 5687; | |
31 | 31 | export const DEFAULT_CLIENT_HOLD_OFF_TIME = 1; |
32 | 32 | export const DEFAULT_LIFE_TIME = 300; |
33 | 33 | export const DEFAULT_MIN_PERIOD = 1; | ... | ... |
... | ... | @@ -20,7 +20,6 @@ export const DEFAULT_END_POINT = 'default_client_lwm2m_end_point_no_sec'; |
20 | 20 | export const BOOTSTRAP_SERVERS = 'servers'; |
21 | 21 | export const BOOTSTRAP_SERVER = 'bootstrapServer'; |
22 | 22 | export const LWM2M_SERVER = 'lwm2mServer'; |
23 | -export const JSON_OBSERVE = 'jsonObserve'; | |
24 | 23 | export const LEN_MAX_PSK = 64; |
25 | 24 | export const LEN_MAX_PRIVATE_KEY = 134; |
26 | 25 | export const LEN_MAX_PUBLIC_KEY_RPK = 182; |
... | ... | @@ -148,4 +147,41 @@ export function getDefaultSecurityConfig(): SecurityConfigModels { |
148 | 147 | return securityConfigModels; |
149 | 148 | } |
150 | 149 | |
150 | +const isSecurityConfigModels = (p: any): p is SecurityConfigModels => | |
151 | + p.hasOwnProperty('client') && | |
152 | + isClientSecurityConfigType(p['client']) && | |
153 | + p.hasOwnProperty('bootstrap') && | |
154 | + isBootstrapSecurityConfig(p['bootstrap']); | |
155 | + | |
156 | +const isClientSecurityConfigType = (p: any): p is ClientSecurityConfigType => | |
157 | + p.hasOwnProperty('securityConfigClientMode') && | |
158 | + p.hasOwnProperty('endpoint') && | |
159 | + p.hasOwnProperty('identity') && | |
160 | + p.hasOwnProperty('key') && | |
161 | + p.hasOwnProperty('x509'); | |
162 | + | |
163 | +const isBootstrapSecurityConfig = (p: any): p is BootstrapSecurityConfig => | |
164 | + p.hasOwnProperty('bootstrapServer') && | |
165 | + isServerSecurityConfig(p['bootstrapServer']) && | |
166 | + p.hasOwnProperty('lwm2mServer') && | |
167 | + isServerSecurityConfig(p['lwm2mServer']); | |
168 | + | |
169 | +const isServerSecurityConfig = (p: any): p is ServerSecurityConfig => | |
170 | + p.hasOwnProperty('securityMode') && | |
171 | + p.hasOwnProperty('clientPublicKeyOrId') && | |
172 | + p.hasOwnProperty('clientSecretKey'); | |
173 | + | |
174 | +export function validateSecurityConfig(config: string): boolean { | |
175 | + try { | |
176 | + const securityConfig= JSON.parse(config); | |
177 | + if (isSecurityConfigModels(securityConfig)) { | |
178 | + return true; | |
179 | + } else { | |
180 | + return false; | |
181 | + } | |
182 | + } catch (e) { | |
183 | + return false; | |
184 | + } | |
185 | +} | |
186 | + | |
151 | 187 | ... | ... |