Commit e0ac59b94b53c0d3c887e8dc0f7f1317a6ff40a6
1 parent
ab18d741
UI: Device provisioning autogenerate device key and secret
Showing
2 changed files
with
22 additions
and
2 deletions
@@ -403,3 +403,15 @@ export function deepTrim<T>(obj: T): T { | @@ -403,3 +403,15 @@ export function deepTrim<T>(obj: T): T { | ||
403 | return acc; | 403 | return acc; |
404 | }, (Array.isArray(obj) ? [] : {}) as T); | 404 | }, (Array.isArray(obj) ? [] : {}) as T); |
405 | } | 405 | } |
406 | + | ||
407 | +export function generateSecret(length?: number): string { | ||
408 | + if (isUndefined(length) || length == null) { | ||
409 | + length = 1; | ||
410 | + } | ||
411 | + const l = length > 10 ? 10 : length; | ||
412 | + const str = Math.random().toString(36).substr(2, l); | ||
413 | + if (str.length >= length) { | ||
414 | + return str; | ||
415 | + } | ||
416 | + return str.concat(generateSecret(length - str.length)); | ||
417 | +} |
@@ -32,7 +32,7 @@ import { | @@ -32,7 +32,7 @@ import { | ||
32 | DeviceProvisionType, | 32 | DeviceProvisionType, |
33 | deviceProvisionTypeTranslationMap | 33 | deviceProvisionTypeTranslationMap |
34 | } from "@shared/models/device.models"; | 34 | } from "@shared/models/device.models"; |
35 | -import { isDefinedAndNotNull } from "@core/utils"; | 35 | +import { generateSecret, isDefinedAndNotNull } from '@core/utils'; |
36 | 36 | ||
37 | @Component({ | 37 | @Component({ |
38 | selector: 'tb-device-profile-provision-configuration', | 38 | selector: 'tb-device-profile-provision-configuration', |
@@ -85,10 +85,18 @@ export class DeviceProfileProvisionConfigurationComponent implements ControlValu | @@ -85,10 +85,18 @@ export class DeviceProfileProvisionConfigurationComponent implements ControlValu | ||
85 | this.provisionConfigurationFormGroup.get('type').valueChanges.subscribe((type) => { | 85 | this.provisionConfigurationFormGroup.get('type').valueChanges.subscribe((type) => { |
86 | if (type === DeviceProvisionType.DISABLED) { | 86 | if (type === DeviceProvisionType.DISABLED) { |
87 | this.provisionConfigurationFormGroup.get('provisionDeviceSecret').disable({emitEvent: false}); | 87 | this.provisionConfigurationFormGroup.get('provisionDeviceSecret').disable({emitEvent: false}); |
88 | - this.provisionConfigurationFormGroup.get('provisionDeviceSecret').patchValue(null,{emitEvent: false}); | 88 | + this.provisionConfigurationFormGroup.get('provisionDeviceSecret').patchValue(null, {emitEvent: false}); |
89 | this.provisionConfigurationFormGroup.get('provisionDeviceKey').disable({emitEvent: false}); | 89 | this.provisionConfigurationFormGroup.get('provisionDeviceKey').disable({emitEvent: false}); |
90 | this.provisionConfigurationFormGroup.get('provisionDeviceKey').patchValue(null); | 90 | this.provisionConfigurationFormGroup.get('provisionDeviceKey').patchValue(null); |
91 | } else { | 91 | } else { |
92 | + const provisionDeviceSecret: string = this.provisionConfigurationFormGroup.get('provisionDeviceSecret').value; | ||
93 | + if (!provisionDeviceSecret || !provisionDeviceSecret.length) { | ||
94 | + this.provisionConfigurationFormGroup.get('provisionDeviceSecret').patchValue(generateSecret(20), {emitEvent: false}); | ||
95 | + } | ||
96 | + const provisionDeviceKey: string = this.provisionConfigurationFormGroup.get('provisionDeviceKey').value; | ||
97 | + if (!provisionDeviceKey || !provisionDeviceKey.length) { | ||
98 | + this.provisionConfigurationFormGroup.get('provisionDeviceKey').patchValue(generateSecret(20), {emitEvent: false}); | ||
99 | + } | ||
92 | this.provisionConfigurationFormGroup.get('provisionDeviceSecret').enable({emitEvent: false}); | 100 | this.provisionConfigurationFormGroup.get('provisionDeviceSecret').enable({emitEvent: false}); |
93 | this.provisionConfigurationFormGroup.get('provisionDeviceKey').enable({emitEvent: false}); | 101 | this.provisionConfigurationFormGroup.get('provisionDeviceKey').enable({emitEvent: false}); |
94 | } | 102 | } |