@mblaney/holster-browser 2.1.0 → 2.2.0
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/README.md +20 -0
- package/package.json +8 -2
- package/src/components/__tests__/Settings.test.jsx +1 -1
- package/src/hooks/__tests__/useAccountSync.test.js +45 -0
- package/src/hooks/__tests__/useDataKey.test.js +44 -0
- package/src/hooks/useAccountSync.js +5 -2
- package/src/hooks/useDataKey.js +34 -0
- package/src/index.js +1 -0
package/README.md
CHANGED
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
EditCache,
|
|
25
25
|
SearchAppBar,
|
|
26
26
|
useAccountSync,
|
|
27
|
+
useDataKey,
|
|
27
28
|
} from "@mblaney/holster-browser"
|
|
28
29
|
```
|
|
29
30
|
|
|
@@ -133,3 +134,22 @@ const accountsReady = useAccountSync(holster, user, host, code)
|
|
|
133
134
|
```
|
|
134
135
|
|
|
135
136
|
Returns a boolean that becomes `true` once the accounts list has been received for the first time.
|
|
137
|
+
|
|
138
|
+
#### useDataKey
|
|
139
|
+
|
|
140
|
+
A hook that returns the data key holster-router shares with the account when its `dataKeys` option is set, or `null` until it's available. Use it to encrypt the account's own data so only the account and the host can read it. The host re-shares the key after a password reset, so the data stays readable with the account's new keys.
|
|
141
|
+
|
|
142
|
+
The key is in SEA's pair format, so it can be passed straight to `SEA.encrypt` and `SEA.decrypt`:
|
|
143
|
+
|
|
144
|
+
```jsx
|
|
145
|
+
const dataKey = useDataKey(holster, user, host, code)
|
|
146
|
+
|
|
147
|
+
const enc = await holster.SEA.encrypt({url}, dataKey)
|
|
148
|
+
const data = await holster.SEA.decrypt(enc, dataKey)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
To use a value as a key without revealing it, `SEA.work` with one iteration and the data key gives the same id for the value each time. One iteration of PBKDF2 is an HMAC keyed with the data key, so the id can't be matched to the value without it:
|
|
152
|
+
|
|
153
|
+
```jsx
|
|
154
|
+
const {epriv: id} = await holster.SEA.work(dataKey.epriv, url, 1)
|
|
155
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mblaney/holster-browser",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "React components for holster applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -29,14 +29,20 @@
|
|
|
29
29
|
"react": ">=18.0.0"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
|
-
"test": "vitest --run"
|
|
32
|
+
"test": "vitest --run",
|
|
33
|
+
"lint": "eslint ."
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
36
|
+
"@eslint/js": "^9.39.5",
|
|
35
37
|
"@mblaney/holster": "^2.3.0",
|
|
36
38
|
"@testing-library/jest-dom": "^5.17.0",
|
|
37
39
|
"@testing-library/react": "^14.0.0",
|
|
38
40
|
"@testing-library/user-event": "^13.5.0",
|
|
39
41
|
"@vitejs/plugin-react": "^4.3.1",
|
|
42
|
+
"eslint": "^9.39.5",
|
|
43
|
+
"eslint-plugin-react": "^7.37.5",
|
|
44
|
+
"eslint-plugin-react-hooks": "^7.1.1",
|
|
45
|
+
"globals": "^17.14.0",
|
|
40
46
|
"jsdom": "^22.0.0",
|
|
41
47
|
"mock-socket": "^9.3.1",
|
|
42
48
|
"prettier": "^3.4.2",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {describe, it, expect, beforeEach
|
|
1
|
+
import {describe, it, expect, beforeEach} from "vitest"
|
|
2
2
|
import {render, screen, waitFor, act} from "@testing-library/react"
|
|
3
3
|
import userEvent from "@testing-library/user-event"
|
|
4
4
|
import {createTestUser} from "../../__tests__/setup.js"
|
|
@@ -54,4 +54,49 @@ describe("useAccountSync", () => {
|
|
|
54
54
|
const shared = await user.get("shared").next(contactCode)
|
|
55
55
|
expect(await holster.SEA.decrypt(shared.someKey, oldSecret)).toBe(null)
|
|
56
56
|
})
|
|
57
|
+
|
|
58
|
+
it("re-shares data when a contact's key changes while listening", async () => {
|
|
59
|
+
const {holster, user} = await createTestUser()
|
|
60
|
+
const host = user.is.pub
|
|
61
|
+
const code = "my-code"
|
|
62
|
+
const contactCode = "bob-code"
|
|
63
|
+
|
|
64
|
+
const oldAccount = await holster.SEA.pair()
|
|
65
|
+
const newAccount = await holster.SEA.pair()
|
|
66
|
+
await publishEpub(holster, oldAccount)
|
|
67
|
+
await publishEpub(holster, newAccount)
|
|
68
|
+
|
|
69
|
+
// The host's record and this user's contact start out matching.
|
|
70
|
+
await user
|
|
71
|
+
.get([host, "accounts"])
|
|
72
|
+
.next(contactCode)
|
|
73
|
+
.put({pub: oldAccount.pub})
|
|
74
|
+
await user
|
|
75
|
+
.get("public")
|
|
76
|
+
.next("contacts")
|
|
77
|
+
.next(contactCode)
|
|
78
|
+
.put({pub: oldAccount.pub})
|
|
79
|
+
|
|
80
|
+
const oldSecret = await holster.SEA.secret(oldAccount, user.is)
|
|
81
|
+
const oldEnc = await holster.SEA.encrypt("secret data", oldSecret)
|
|
82
|
+
await user.get("shared").next(contactCode).put({someKey: oldEnc})
|
|
83
|
+
|
|
84
|
+
const {result} = renderHook(() => useAccountSync(holster, user, host, code))
|
|
85
|
+
await waitFor(() => expect(result.current).toBe(true))
|
|
86
|
+
|
|
87
|
+
// The contact migrates to a new key in place, after the hook has
|
|
88
|
+
// already seen their account.
|
|
89
|
+
await user
|
|
90
|
+
.get([host, "accounts"])
|
|
91
|
+
.next(contactCode)
|
|
92
|
+
.put({pub: newAccount.pub})
|
|
93
|
+
|
|
94
|
+
const newSecret = await holster.SEA.secret(newAccount, user.is)
|
|
95
|
+
await waitFor(async () => {
|
|
96
|
+
const shared = await user.get("shared").next(contactCode)
|
|
97
|
+
expect(await holster.SEA.decrypt(shared.someKey, newSecret)).toBe(
|
|
98
|
+
"secret data",
|
|
99
|
+
)
|
|
100
|
+
})
|
|
101
|
+
})
|
|
57
102
|
})
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {describe, it, expect} from "vitest"
|
|
2
|
+
import {renderHook, waitFor} from "@testing-library/react"
|
|
3
|
+
import {createTestUser} from "../../__tests__/setup.js"
|
|
4
|
+
import {useDataKey} from "../useDataKey.js"
|
|
5
|
+
|
|
6
|
+
describe("useDataKey", () => {
|
|
7
|
+
it("returns the shared data key in a form SEA can use", async () => {
|
|
8
|
+
// The test user is its own host here, so the shared secret is between
|
|
9
|
+
// the user's keys and themselves.
|
|
10
|
+
const {holster, user} = await createTestUser()
|
|
11
|
+
const host = user.is.pub
|
|
12
|
+
const code = "my-code"
|
|
13
|
+
|
|
14
|
+
const secret = await holster.SEA.secret(user.is, user.is)
|
|
15
|
+
const enc = await holster.SEA.encrypt("the-data-key", secret)
|
|
16
|
+
await user.get("shared").next("data_key").next(code).put(enc)
|
|
17
|
+
|
|
18
|
+
const {result} = renderHook(() => useDataKey(holster, user, host, code))
|
|
19
|
+
await waitFor(() => expect(result.current).toEqual({epriv: "the-data-key"}))
|
|
20
|
+
|
|
21
|
+
const dataKey = result.current
|
|
22
|
+
const data = await holster.SEA.encrypt({url: "https://a.example"}, dataKey)
|
|
23
|
+
expect(await holster.SEA.decrypt(data, dataKey)).toEqual({
|
|
24
|
+
url: "https://a.example",
|
|
25
|
+
})
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it("updates when the host re-shares the key", async () => {
|
|
29
|
+
const {holster, user} = await createTestUser()
|
|
30
|
+
const host = user.is.pub
|
|
31
|
+
const code = "my-code"
|
|
32
|
+
|
|
33
|
+
const secret = await holster.SEA.secret(user.is, user.is)
|
|
34
|
+
const first = await holster.SEA.encrypt("first-key", secret)
|
|
35
|
+
await user.get("shared").next("data_key").next(code).put(first)
|
|
36
|
+
|
|
37
|
+
const {result} = renderHook(() => useDataKey(holster, user, host, code))
|
|
38
|
+
await waitFor(() => expect(result.current).toEqual({epriv: "first-key"}))
|
|
39
|
+
|
|
40
|
+
const second = await holster.SEA.encrypt("second-key", secret)
|
|
41
|
+
await user.get("shared").next("data_key").next(code).put(second)
|
|
42
|
+
await waitFor(() => expect(result.current).toEqual({epriv: "second-key"}))
|
|
43
|
+
})
|
|
44
|
+
})
|
|
@@ -90,12 +90,15 @@ export function useAccountSync(holster, user, host, code) {
|
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
|
|
93
|
+
// Nested so this also runs when a password reset changes the pub stored
|
|
94
|
+
// for an existing code while the app is open. Without it that's only seen
|
|
95
|
+
// on the next load.
|
|
96
|
+
user.get([host, "accounts"]).on(updateAccounts, true, {nested: true})
|
|
94
97
|
|
|
95
98
|
return () => {
|
|
96
99
|
user.get([host, "accounts"]).off(updateAccounts)
|
|
97
100
|
}
|
|
98
|
-
}, [host, code])
|
|
101
|
+
}, [holster, user, host, code])
|
|
99
102
|
|
|
100
103
|
return accountsReady
|
|
101
104
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {useEffect, useState} from "react"
|
|
2
|
+
|
|
3
|
+
// Returns the data key holster-router shares with this account when its
|
|
4
|
+
// dataKeys option is set, or null until it's available. It's in SEA's pair
|
|
5
|
+
// format, so it can be passed straight to SEA.encrypt and SEA.decrypt. The
|
|
6
|
+
// host can also read it, and re-shares it when a password reset changes the
|
|
7
|
+
// account's keys, so data encrypted with it stays readable.
|
|
8
|
+
export function useDataKey(holster, user, host, code) {
|
|
9
|
+
const [dataKey, setDataKey] = useState(null)
|
|
10
|
+
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
if (!host || !code || !user.is) return
|
|
13
|
+
|
|
14
|
+
let secret = null
|
|
15
|
+
const update = async enc => {
|
|
16
|
+
if (!enc) return
|
|
17
|
+
|
|
18
|
+
if (!secret) {
|
|
19
|
+
const epub = await user.get([host, "epub"])
|
|
20
|
+
secret = await holster.SEA.secret({epub}, user.is)
|
|
21
|
+
}
|
|
22
|
+
const key = await holster.SEA.decrypt(enc, secret)
|
|
23
|
+
if (key) setDataKey({epriv: key})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
user.get([host, "shared"]).next("data_key").next(code).on(update, true)
|
|
27
|
+
|
|
28
|
+
return () => {
|
|
29
|
+
user.get([host, "shared"]).next("data_key").next(code).off(update)
|
|
30
|
+
}
|
|
31
|
+
}, [holster, user, host, code])
|
|
32
|
+
|
|
33
|
+
return dataKey
|
|
34
|
+
}
|
package/src/index.js
CHANGED
|
@@ -10,4 +10,5 @@ export {default as Settings} from "./components/Settings.jsx"
|
|
|
10
10
|
export {default as UpdatePassword} from "./components/UpdatePassword.jsx"
|
|
11
11
|
export {default as ValidateEmail} from "./components/ValidateEmail.jsx"
|
|
12
12
|
export {useAccountSync} from "./hooks/useAccountSync.js"
|
|
13
|
+
export {useDataKey} from "./hooks/useDataKey.js"
|
|
13
14
|
export {baseStyleOverrides} from "./baseStyleOverrides.js"
|