1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.AccessoryInfo = exports.PermissionTypes = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const assert_1 = tslib_1.__importDefault(require("assert"));
|
6 | const crypto_1 = tslib_1.__importDefault(require("crypto"));
|
7 | const tweetnacl_1 = tslib_1.__importDefault(require("tweetnacl"));
|
8 | const util_1 = tslib_1.__importDefault(require("util"));
|
9 | const eventedhttp_1 = require("../util/eventedhttp");
|
10 | const HAPStorage_1 = require("./HAPStorage");
|
11 | function getVersion() {
|
12 |
|
13 | const packageJson = require("../../../package.json");
|
14 | return packageJson.version;
|
15 | }
|
16 |
|
17 |
|
18 |
|
19 | var PermissionTypes;
|
20 | (function (PermissionTypes) {
|
21 |
|
22 | PermissionTypes[PermissionTypes["USER"] = 0] = "USER";
|
23 | PermissionTypes[PermissionTypes["ADMIN"] = 1] = "ADMIN";
|
24 | })(PermissionTypes || (exports.PermissionTypes = PermissionTypes = {}));
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | class AccessoryInfo {
|
31 | static deviceIdPattern = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
|
32 | username;
|
33 | displayName;
|
34 | model;
|
35 | category;
|
36 | pincode;
|
37 | signSk;
|
38 | signPk;
|
39 | pairedClients;
|
40 | pairedAdminClients;
|
41 | configVersion = 1;
|
42 | configHash;
|
43 | setupID;
|
44 | lastFirmwareVersion = "";
|
45 | constructor(username) {
|
46 | this.username = username;
|
47 | this.displayName = "";
|
48 | this.model = "";
|
49 | this.category = 1 ;
|
50 | this.pincode = "";
|
51 | this.signSk = Buffer.alloc(0);
|
52 | this.signPk = Buffer.alloc(0);
|
53 | this.pairedClients = {};
|
54 | this.pairedAdminClients = 0;
|
55 | this.configHash = "";
|
56 | this.setupID = "";
|
57 | }
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | addPairedClient(username, publicKey, permission) {
|
65 | this.pairedClients[username] = {
|
66 | username: username,
|
67 | publicKey: publicKey,
|
68 | permission: permission,
|
69 | };
|
70 | if (permission === 1 ) {
|
71 | this.pairedAdminClients++;
|
72 | }
|
73 | }
|
74 | updatePermission(username, permission) {
|
75 | const pairingInformation = this.pairedClients[username];
|
76 | if (pairingInformation) {
|
77 | const oldPermission = pairingInformation.permission;
|
78 | pairingInformation.permission = permission;
|
79 | if (oldPermission === 1 && permission !== 1 ) {
|
80 | this.pairedAdminClients--;
|
81 | }
|
82 | else if (oldPermission !== 1 && permission === 1 ) {
|
83 | this.pairedAdminClients++;
|
84 | }
|
85 | }
|
86 | }
|
87 | listPairings() {
|
88 | const array = [];
|
89 | for (const pairingInformation of Object.values(this.pairedClients)) {
|
90 | array.push(pairingInformation);
|
91 | }
|
92 | return array;
|
93 | }
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 | removePairedClient(connection, username) {
|
100 | this._removePairedClient0(connection, username);
|
101 | if (this.pairedAdminClients === 0) {
|
102 | for (const username0 of Object.keys(this.pairedClients)) {
|
103 | this._removePairedClient0(connection, username0);
|
104 | }
|
105 | }
|
106 | }
|
107 | _removePairedClient0(connection, username) {
|
108 | if (this.pairedClients[username] && this.pairedClients[username].permission === 1 ) {
|
109 | this.pairedAdminClients--;
|
110 | }
|
111 | delete this.pairedClients[username];
|
112 | eventedhttp_1.EventedHTTPServer.destroyExistingConnectionsAfterUnpair(connection, username);
|
113 | }
|
114 | |
115 |
|
116 |
|
117 |
|
118 | isPaired(username) {
|
119 | return !!this.pairedClients[username];
|
120 | }
|
121 | hasAdminPermissions(username) {
|
122 | if (!username) {
|
123 | return false;
|
124 | }
|
125 | const pairingInformation = this.pairedClients[username];
|
126 | return !!pairingInformation && pairingInformation.permission === 1 ;
|
127 | }
|
128 |
|
129 | getClientPublicKey(username) {
|
130 | const pairingInformation = this.pairedClients[username];
|
131 | if (pairingInformation) {
|
132 | return pairingInformation.publicKey;
|
133 | }
|
134 | else {
|
135 | return undefined;
|
136 | }
|
137 | }
|
138 |
|
139 | paired = () => {
|
140 | return Object.keys(this.pairedClients).length > 0;
|
141 | };
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | checkForCurrentConfigurationNumberIncrement(configuration, checkFirmwareIncrement) {
|
151 | const shasum = crypto_1.default.createHash("sha1");
|
152 | shasum.update(JSON.stringify(configuration));
|
153 | const configHash = shasum.digest("hex");
|
154 | let changed = false;
|
155 | if (configHash !== this.configHash) {
|
156 | this.configVersion++;
|
157 | this.configHash = configHash;
|
158 | this.ensureConfigVersionBounds();
|
159 | changed = true;
|
160 | }
|
161 | if (checkFirmwareIncrement) {
|
162 | const version = getVersion();
|
163 | if (this.lastFirmwareVersion !== version) {
|
164 |
|
165 |
|
166 | this.lastFirmwareVersion = version;
|
167 | changed = true;
|
168 | }
|
169 | }
|
170 | if (changed) {
|
171 | this.save();
|
172 | }
|
173 | return changed;
|
174 | }
|
175 | getConfigVersion() {
|
176 | return this.configVersion;
|
177 | }
|
178 | ensureConfigVersionBounds() {
|
179 |
|
180 | this.configVersion = this.configVersion % (0xFFFF + 1);
|
181 | if (this.configVersion === 0) {
|
182 | this.configVersion = 1;
|
183 | }
|
184 | }
|
185 | save() {
|
186 | const saved = {
|
187 | displayName: this.displayName,
|
188 | category: this.category,
|
189 | pincode: this.pincode,
|
190 | signSk: this.signSk.toString("hex"),
|
191 | signPk: this.signPk.toString("hex"),
|
192 | pairedClients: {},
|
193 |
|
194 |
|
195 |
|
196 | pairedClientsPermission: {},
|
197 | configVersion: this.configVersion,
|
198 | configHash: this.configHash,
|
199 | setupID: this.setupID,
|
200 | lastFirmwareVersion: this.lastFirmwareVersion,
|
201 | };
|
202 | for (const [username, pairingInformation] of Object.entries(this.pairedClients)) {
|
203 |
|
204 | saved.pairedClients[username] = pairingInformation.publicKey.toString("hex");
|
205 |
|
206 | saved.pairedClientsPermission[username] = pairingInformation.permission;
|
207 | }
|
208 | const key = AccessoryInfo.persistKey(this.username);
|
209 | HAPStorage_1.HAPStorage.storage().setItemSync(key, saved);
|
210 | }
|
211 |
|
212 | static persistKey(username) {
|
213 | return util_1.default.format("AccessoryInfo.%s.json", username.replace(/:/g, "").toUpperCase());
|
214 | }
|
215 | static create(username) {
|
216 | AccessoryInfo.assertValidUsername(username);
|
217 | const accessoryInfo = new AccessoryInfo(username);
|
218 | accessoryInfo.lastFirmwareVersion = getVersion();
|
219 |
|
220 | const keyPair = tweetnacl_1.default.sign.keyPair();
|
221 | accessoryInfo.signSk = Buffer.from(keyPair.secretKey);
|
222 | accessoryInfo.signPk = Buffer.from(keyPair.publicKey);
|
223 | return accessoryInfo;
|
224 | }
|
225 | static load(username) {
|
226 | AccessoryInfo.assertValidUsername(username);
|
227 | const key = AccessoryInfo.persistKey(username);
|
228 | const saved = HAPStorage_1.HAPStorage.storage().getItem(key);
|
229 | if (saved) {
|
230 | const info = new AccessoryInfo(username);
|
231 | info.displayName = saved.displayName || "";
|
232 | info.category = saved.category || "";
|
233 | info.pincode = saved.pincode || "";
|
234 | info.signSk = Buffer.from(saved.signSk || "", "hex");
|
235 | info.signPk = Buffer.from(saved.signPk || "", "hex");
|
236 | info.pairedClients = {};
|
237 | for (const username of Object.keys(saved.pairedClients || {})) {
|
238 | const publicKey = saved.pairedClients[username];
|
239 | let permission = saved.pairedClientsPermission ? saved.pairedClientsPermission[username] : undefined;
|
240 | if (permission === undefined) {
|
241 | permission = 1 ;
|
242 | }
|
243 | info.pairedClients[username] = {
|
244 | username: username,
|
245 | publicKey: Buffer.from(publicKey, "hex"),
|
246 | permission: permission,
|
247 | };
|
248 | if (permission === 1 ) {
|
249 | info.pairedAdminClients++;
|
250 | }
|
251 | }
|
252 | info.configVersion = saved.configVersion || 1;
|
253 | info.configHash = saved.configHash || "";
|
254 | info.setupID = saved.setupID || "";
|
255 | info.lastFirmwareVersion = saved.lastFirmwareVersion || getVersion();
|
256 | info.ensureConfigVersionBounds();
|
257 | return info;
|
258 | }
|
259 | else {
|
260 | return null;
|
261 | }
|
262 | }
|
263 | static remove(username) {
|
264 | const key = AccessoryInfo.persistKey(username);
|
265 | HAPStorage_1.HAPStorage.storage().removeItemSync(key);
|
266 | }
|
267 | static assertValidUsername(username) {
|
268 | assert_1.default.ok(AccessoryInfo.deviceIdPattern.test(username), "The supplied username (" + username + ") is not valid " +
|
269 | "(expected a format like 'XX:XX:XX:XX:XX:XX' with XX being a valid hexadecimal string). " +
|
270 | "Note that, if you had this accessory already paired with the invalid username, you will need to repair " +
|
271 | "the accessory and reconfigure your services in the Home app. " +
|
272 | "Using an invalid username will lead to unexpected behaviour.");
|
273 | }
|
274 | }
|
275 | exports.AccessoryInfo = AccessoryInfo;
|
276 |
|
\ | No newline at end of file |