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 | 403 | return acc; |
404 | 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 | 32 | DeviceProvisionType, |
33 | 33 | deviceProvisionTypeTranslationMap |
34 | 34 | } from "@shared/models/device.models"; |
35 | -import { isDefinedAndNotNull } from "@core/utils"; | |
35 | +import { generateSecret, isDefinedAndNotNull } from '@core/utils'; | |
36 | 36 | |
37 | 37 | @Component({ |
38 | 38 | selector: 'tb-device-profile-provision-configuration', |
... | ... | @@ -85,10 +85,18 @@ export class DeviceProfileProvisionConfigurationComponent implements ControlValu |
85 | 85 | this.provisionConfigurationFormGroup.get('type').valueChanges.subscribe((type) => { |
86 | 86 | if (type === DeviceProvisionType.DISABLED) { |
87 | 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 | 89 | this.provisionConfigurationFormGroup.get('provisionDeviceKey').disable({emitEvent: false}); |
90 | 90 | this.provisionConfigurationFormGroup.get('provisionDeviceKey').patchValue(null); |
91 | 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 | 100 | this.provisionConfigurationFormGroup.get('provisionDeviceSecret').enable({emitEvent: false}); |
93 | 101 | this.provisionConfigurationFormGroup.get('provisionDeviceKey').enable({emitEvent: false}); |
94 | 102 | } | ... | ... |