hb-nb-tools 1.1.1 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/NbClient.js +10 -14
- package/lib/NbListener.js +4 -0
- package/package.json +4 -6
package/lib/NbClient.js
CHANGED
|
@@ -6,13 +6,8 @@
|
|
|
6
6
|
'use strict'
|
|
7
7
|
|
|
8
8
|
const crypto = require('crypto')
|
|
9
|
-
const
|
|
10
|
-
const {
|
|
11
|
-
let sodiumPlus
|
|
12
|
-
try {
|
|
13
|
-
sodiumPlus = require('sodium-plus')
|
|
14
|
-
} catch (error) {}
|
|
15
|
-
const { CryptographyKey, SodiumPlus } = sodiumPlus || {}
|
|
9
|
+
const { HttpClient, JsonFormatter, OptionParser } = require('hb-lib-tools')
|
|
10
|
+
const { secretbox, randomBytes } = require('tweetnacl')
|
|
16
11
|
|
|
17
12
|
class NbClient extends HttpClient {
|
|
18
13
|
static get DeviceTypes () {
|
|
@@ -106,7 +101,7 @@ class NbClient extends HttpClient {
|
|
|
106
101
|
|
|
107
102
|
constructor (params = {}) {
|
|
108
103
|
const _params = {
|
|
109
|
-
encryption:
|
|
104
|
+
encryption: 'encryptedToken',
|
|
110
105
|
port: 8080,
|
|
111
106
|
timeout: 5
|
|
112
107
|
}
|
|
@@ -177,6 +172,7 @@ class NbClient extends HttpClient {
|
|
|
177
172
|
}
|
|
178
173
|
|
|
179
174
|
async callbackAdd (url) {
|
|
175
|
+
url = OptionParser.toString('url', url, true)
|
|
180
176
|
return this._get('/callback/add', { url: encodeURIComponent(url) })
|
|
181
177
|
}
|
|
182
178
|
|
|
@@ -198,15 +194,15 @@ class NbClient extends HttpClient {
|
|
|
198
194
|
const date = new Date().toISOString()
|
|
199
195
|
const ts = date.slice(0, 19) + 'Z'
|
|
200
196
|
if (this._params.encryption === 'encryptedToken') {
|
|
201
|
-
if (this.
|
|
197
|
+
if (this._key == null) {
|
|
202
198
|
const hash = crypto.createHash('sha256')
|
|
203
199
|
hash.update(this._params.token)
|
|
204
|
-
this._key =
|
|
205
|
-
this._sodium = await SodiumPlus.auto()
|
|
200
|
+
this._key = hash.digest()
|
|
206
201
|
}
|
|
207
|
-
const rnr =
|
|
208
|
-
const nonce =
|
|
209
|
-
const
|
|
202
|
+
const rnr = crypto.randomInt(10000)
|
|
203
|
+
const nonce = Buffer.from(randomBytes(secretbox.nonceLength))
|
|
204
|
+
const message = Buffer.from([ts, rnr].join(','), 'utf-8')
|
|
205
|
+
const ctoken = Buffer.from(secretbox(message, nonce, this._key))
|
|
210
206
|
suffix = separator + 'ctoken=' + ctoken.toString('hex') + '&nonce=' + nonce.toString('hex')
|
|
211
207
|
} else if (this._params.encryption === 'hashedToken') { // deprecated
|
|
212
208
|
const hash = crypto.createHash('sha256')
|
package/lib/NbListener.js
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
|
|
8
8
|
const events = require('events')
|
|
9
9
|
const http = require('http')
|
|
10
|
+
const NbClient = require('./NbClient')
|
|
11
|
+
const { OptionParser } = require('hb-lib-tools')
|
|
10
12
|
|
|
11
13
|
class NbListener extends events.EventEmitter {
|
|
12
14
|
constructor (port = 0) {
|
|
@@ -84,6 +86,7 @@ class NbListener extends events.EventEmitter {
|
|
|
84
86
|
}
|
|
85
87
|
|
|
86
88
|
async addClient (nbClient) {
|
|
89
|
+
OptionParser.toInstance('nbClient', nbClient, NbClient)
|
|
87
90
|
this._clients[nbClient.id] = nbClient
|
|
88
91
|
this._clientsByName[nbClient.name] = nbClient
|
|
89
92
|
await this._listen()
|
|
@@ -93,6 +96,7 @@ class NbListener extends events.EventEmitter {
|
|
|
93
96
|
}
|
|
94
97
|
|
|
95
98
|
async removeClient (nbClient) {
|
|
99
|
+
OptionParser.toInstance('nbClient', nbClient, NbClient)
|
|
96
100
|
this.removeAllListeners(nbClient.id)
|
|
97
101
|
delete this._clientsByName[nbClient.name]
|
|
98
102
|
delete this._clients[nbClient.id]
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Homebridge NB Tools",
|
|
4
4
|
"author": "Erik Baauw",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
|
-
"version": "1.1.
|
|
6
|
+
"version": "1.1.3",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"nuki",
|
|
9
9
|
"smart-lock",
|
|
@@ -14,13 +14,11 @@
|
|
|
14
14
|
"nb": "cli/nb.js"
|
|
15
15
|
},
|
|
16
16
|
"engines": {
|
|
17
|
-
"node": "^18.16.
|
|
17
|
+
"node": "^18.16.1"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"hb-lib-tools": "~1.0.
|
|
21
|
-
|
|
22
|
-
"optionalDependencies": {
|
|
23
|
-
"sodium-plus": "~0.9.0"
|
|
20
|
+
"hb-lib-tools": "~1.0.10",
|
|
21
|
+
"tweetnacl": "~1.0.3"
|
|
24
22
|
},
|
|
25
23
|
"scripts": {
|
|
26
24
|
"prepare": "standard",
|