Skip to content

Commit ebf1323

Browse files
committed
Minor
1 parent b1e69da commit ebf1323

File tree

1 file changed

+33
-45
lines changed

1 file changed

+33
-45
lines changed

src/cmd/device-protection.js

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
3131
// then the device is not pretected. For now though, let's assume the device is in normal mode and not in dfu mode.
3232

3333
return this._withDevice(async () => {
34-
let s;
35-
try {
36-
s = await this.device.getProtectionState();
37-
} catch (error) {
38-
if (error.message === 'Not supported') {
39-
throw new Error(`Device protection feature is not supported on this device`);
40-
}
41-
}
34+
const s = await this._getDeviceProtection();
4235

4336
let res;
4437
if (!s.protected && !s.overridden) {
@@ -57,16 +50,10 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
5750

5851
async disableProtection({ open } = {}) {
5952
return this._withDevice(async () => {
60-
let s;
61-
try {
62-
s = await this.device.getProtectionState();
63-
} catch (error) {
64-
if (error.message === 'Not supported') {
65-
throw new Error(`Device protection feature is not supported on this device${os.EOL}`);
66-
}
67-
}
53+
const s = await this._getDeviceProtection();
54+
6855
if (!s.protected && !s.overridden) {
69-
this.ui.stdout.write(`Device is not protected${os.EOL}`);
56+
this.ui.stdout.write(`Device is not protected`);
7057
return;
7158
}
7259

@@ -79,22 +66,22 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
7966
// console.log(`CLI -> Device:\n\tserver_nonce=${serverNonce.toString('base64')}`);
8067
r = await this.device.unprotectDevice({ action: 'prepare', serverNonce });
8168
if (!r.protected) {
82-
console.log('Device is not protected');
83-
return;
69+
this.ui.stdout.write('Device is not protected');
70+
return;
8471
}
8572
const { deviceNonce, deviceSignature, devicePublicKeyFingerprint } = r;
8673
// console.log(`Device -> CLI:\n\tdevice_signature=${deviceSignature.toString('base64')}`);
8774

8875
// Verify the device signature and get a server signature
8976
// console.log(`CLI -> Server:\n\tdevice_signature=${deviceSignature.toString('base64')}`);
9077
r = await this.api.unprotectDevice({
91-
deviceId: this.deviceId,
92-
action: 'confirm',
93-
serverNonce: serverNonce.toString('base64'),
94-
deviceNonce: deviceNonce.toString('base64'),
95-
deviceSignature: deviceSignature.toString('base64'),
96-
devicePublicKeyFingerprint: devicePublicKeyFingerprint.toString('base64'),
97-
auth: settings.access_token
78+
deviceId: this.deviceId,
79+
action: 'confirm',
80+
serverNonce: serverNonce.toString('base64'),
81+
deviceNonce: deviceNonce.toString('base64'),
82+
deviceSignature: deviceSignature.toString('base64'),
83+
devicePublicKeyFingerprint: devicePublicKeyFingerprint.toString('base64'),
84+
auth: settings.access_token
9885
});
9986

10087
const serverSignature = Buffer.from(r.server_signature, 'base64');
@@ -104,25 +91,22 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
10491
// Unprotect the device
10592
await this.device.unprotectDevice({ action: 'confirm', serverSignature, serverPublicKeyFingerprint });
10693

107-
s = await this.device.getProtectionState();
94+
s = await this._getDeviceProtection();
10895
if (!open) {
10996
this.ui.stdout.write(`Device protection temporarily disabled.${os.EOL}Device is put into service mode for 20 reboots or 24 hours.${os.EOL}`);
11097
return;
11198
}
11299

113-
if (open) {
114-
115-
const localBootloaderPath = await this._downloadBootloader();
100+
const localBootloaderPath = await this._downloadBootloader();
116101

117-
await this._flashBootloader(localBootloaderPath, 'disable');
102+
await this._flashBootloader(localBootloaderPath, 'disable');
118103

119-
this.ui.stdout.write(`Device protection disabled.${os.EOL}Device is open${os.EOL}`);
104+
this.ui.stdout.write(`Device protection disabled.${os.EOL}Device is open${os.EOL}`);
120105

121-
const success = await this._markAsDevelopmentDevice(true);
106+
const success = await this._markAsDevelopmentDevice(true);
122107

123-
if (!success) {
124-
this.ui.stdout.write(`Failed to mark device as development device. Protection will be automatically enabled after a power cycle${os.EOL}`);
125-
}
108+
if (!success) {
109+
this.ui.stdout.write(`Failed to mark device as development device. Protection will be automatically enabled after a power cycle${os.EOL}`);
126110
}
127111
});
128112
}
@@ -155,15 +139,8 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
155139
let protectedBinary = file;
156140

157141
return this._withDevice(async () => {
158-
let s;
159-
try {
160-
s = await this.device.getProtectionState();
161-
} catch (error) {
162-
if (error.message === 'Not supported') {
163-
throw new Error(`Device protection feature is not supported on this device`);
164-
}
165-
}
166-
142+
const s = await this._getDeviceProtection();
143+
167144
const attrs = await this.api.getDeviceAttributes(this.deviceId);
168145
let deviceProtectionActiveInProduct = false;
169146
if (attrs.platform_id !== attrs.product_id) {
@@ -209,6 +186,17 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
209186
});
210187
}
211188

189+
async _getDeviceProtection() {
190+
try {
191+
const s = await this.device.getProtectionState();
192+
return s;
193+
} catch (error) {
194+
if (error.message === 'Not supported') {
195+
throw new Error(`Device protection feature is not supported on this device${os.EOL}`);
196+
}
197+
}
198+
}
199+
212200
async _flashBootloader(path, action) {
213201
let msg;
214202
switch (action) {

0 commit comments

Comments
 (0)