@mblaney/holster-browser 2.0.3 → 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 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.0.3",
3
+ "version": "2.2.0",
4
4
  "description": "React components for holster applications",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -23,20 +23,26 @@
23
23
  "peerDependencies": {
24
24
  "@emotion/react": ">=11.0.0",
25
25
  "@emotion/styled": ">=11.0.0",
26
- "@mblaney/holster": ">=2.1.0",
26
+ "@mblaney/holster": ">=2.3.0",
27
27
  "@mui/icons-material": ">=5.0.0",
28
28
  "@mui/material": ">=5.0.0",
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": {
35
- "@mblaney/holster": "^2.1.0",
36
+ "@eslint/js": "^9.39.5",
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",
@@ -26,15 +26,11 @@ const Login = ({user, host}) => {
26
26
  if (!user.is) return
27
27
 
28
28
  setMessage("Credentials verified, looking up account...")
29
- const code = await new Promise(res => {
30
- user.get([host, "map"]).next("account:" + user.is.pub, res)
31
- })
29
+ const code = await user.get([host, "map"]).next("account:" + user.is.pub)
32
30
  if (!code) return
33
31
 
34
32
  setMessage("Account found, verifying details...")
35
- const account = await new Promise(res => {
36
- user.get([host, "accounts"]).next(code, res)
37
- })
33
+ const account = await user.get([host, "accounts"]).next(code)
38
34
  if (!account) return
39
35
 
40
36
  if (account.pub !== user.is.pub) {
@@ -20,18 +20,14 @@ async function createHostedAccount({
20
20
  await new Promise(resolve => user.auth(username, password, resolve))
21
21
  const host = user.is.pub
22
22
 
23
- await new Promise(resolve => {
24
- user
25
- .get([host, "map"])
26
- .next("account:" + host)
27
- .put(accountCode, resolve)
28
- })
29
- await new Promise(resolve => {
30
- user
31
- .get([host, "accounts"])
32
- .next(accountCode)
33
- .put({pub: host, name: accountName}, resolve)
34
- })
23
+ await user
24
+ .get([host, "map"])
25
+ .next("account:" + host)
26
+ .put(accountCode)
27
+ await user
28
+ .get([host, "accounts"])
29
+ .next(accountCode)
30
+ .put({pub: host, name: accountName})
35
31
 
36
32
  user.leave()
37
33
  return {holster, user, host, username, password, accountCode, accountName}
@@ -155,18 +151,14 @@ describe("Login Component", () => {
155
151
  const host = user.is.pub
156
152
  const stalePub = "some-other-pub.some-other-epub"
157
153
 
158
- await new Promise(resolve => {
159
- user
160
- .get([host, "map"])
161
- .next("account:" + host)
162
- .put("acc1", resolve)
163
- })
164
- await new Promise(resolve => {
165
- user
166
- .get([host, "accounts"])
167
- .next("acc1")
168
- .put({pub: stalePub, name: "Alice"}, resolve)
169
- })
154
+ await user
155
+ .get([host, "map"])
156
+ .next("account:" + host)
157
+ .put("acc1")
158
+ await user
159
+ .get([host, "accounts"])
160
+ .next("acc1")
161
+ .put({pub: stalePub, name: "Alice"})
170
162
  user.leave()
171
163
 
172
164
  render(<Login user={user} host={host} />)
@@ -12,13 +12,11 @@ import LoginCodes from "../LoginCodes.jsx"
12
12
  async function shareCode(user, code, key, value) {
13
13
  const secret = await user.SEA.secret({epub: user.is.epub}, user.is)
14
14
  const enc = value === null ? null : await user.SEA.encrypt(value, secret)
15
- await new Promise(resolve => {
16
- user
17
- .get([user.is.pub, "shared"])
18
- .next("login_codes")
19
- .next(code)
20
- .put({[key]: enc}, resolve)
21
- })
15
+ await user
16
+ .get([user.is.pub, "shared"])
17
+ .next("login_codes")
18
+ .next(code)
19
+ .put({[key]: enc})
22
20
  }
23
21
 
24
22
  describe("LoginCodes Component", () => {
@@ -1,4 +1,4 @@
1
- import {describe, it, expect, beforeEach, vi} from "vitest"
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"
@@ -109,9 +109,7 @@ describe("UpdatePassword Component", () => {
109
109
  )
110
110
 
111
111
  await waitFor(async () => {
112
- const publicData = await new Promise(resolve => {
113
- user.get("public", resolve)
114
- })
112
+ const publicData = await user.get("public")
115
113
  expect(publicData?.favoriteColor).toBe("blue")
116
114
  })
117
115
 
@@ -21,47 +21,82 @@ describe("useAccountSync", () => {
21
21
 
22
22
  // The host's own record of this contact's account, already migrated to
23
23
  // the new key.
24
- await new Promise(resolve => {
25
- user
26
- .get([host, "accounts"])
27
- .next(contactCode)
28
- .put({pub: newAccount.pub}, resolve)
29
- })
24
+ await user
25
+ .get([host, "accounts"])
26
+ .next(contactCode)
27
+ .put({pub: newAccount.pub})
30
28
 
31
29
  // This user's own previously-recorded (now stale) copy of the contact.
32
- await new Promise(resolve => {
33
- user
34
- .get("public")
35
- .next("contacts")
36
- .next(contactCode)
37
- .put({pub: oldAccount.pub}, resolve)
38
- })
30
+ await user
31
+ .get("public")
32
+ .next("contacts")
33
+ .next(contactCode)
34
+ .put({pub: oldAccount.pub})
39
35
 
40
36
  // Something previously shared with the contact, encrypted for their old
41
37
  // key.
42
38
  const oldSecret = await holster.SEA.secret(oldAccount, user.is)
43
39
  const oldEnc = await holster.SEA.encrypt("secret data", oldSecret)
44
- await new Promise(resolve => {
45
- user.get("shared").next(contactCode).put({someKey: oldEnc}, resolve)
46
- })
40
+ await user.get("shared").next(contactCode).put({someKey: oldEnc})
47
41
 
48
42
  renderHook(() => useAccountSync(holster, user, host, code))
49
43
 
50
44
  const newSecret = await holster.SEA.secret(newAccount, user.is)
51
45
 
52
46
  await waitFor(async () => {
53
- const shared = await new Promise(resolve => {
54
- user.get("shared").next(contactCode, resolve)
55
- })
47
+ const shared = await user.get("shared").next(contactCode)
56
48
  expect(shared?.someKey).toBeTruthy()
57
49
  expect(await holster.SEA.decrypt(shared.someKey, newSecret)).toBe(
58
50
  "secret data",
59
51
  )
60
52
  })
61
53
 
62
- const shared = await new Promise(resolve => {
63
- user.get("shared").next(contactCode, resolve)
64
- })
54
+ const shared = await user.get("shared").next(contactCode)
65
55
  expect(await holster.SEA.decrypt(shared.someKey, oldSecret)).toBe(null)
66
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
+ })
67
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
+ })
@@ -12,9 +12,7 @@ export function useAccountSync(holster, user, host, code) {
12
12
  setAccountsReady(true)
13
13
 
14
14
  // Check accounts against the users list of contacts.
15
- const c = await new Promise(res => {
16
- user.get("public").next("contacts", res)
17
- })
15
+ const c = await user.get("public").next("contacts")
18
16
  if (!c) return
19
17
 
20
18
  for (const [accountCode, account] of Object.entries(accounts)) {
@@ -39,34 +37,26 @@ export function useAccountSync(holster, user, host, code) {
39
37
  // If the public key has changed for this contact then store their
40
38
  // new account details and re-share encrypted data with them to
41
39
  // help restore their account.
42
- const err = await new Promise(res => {
43
- user
44
- .get("public")
45
- .next("contacts")
46
- .next(contactCode)
47
- .put(check, res)
48
- })
49
- if (err) console.error(err)
40
+ await user
41
+ .get("public")
42
+ .next("contacts")
43
+ .next(contactCode)
44
+ .put(check)
45
+ .catch(err => console.error(err))
50
46
 
51
- const oldEpub = await new Promise(res => {
52
- user.get([contact.pub, "epub"], res)
53
- })
47
+ const oldEpub = await user.get([contact.pub, "epub"])
54
48
  if (!oldEpub) {
55
49
  console.error("User not found for old public key")
56
50
  break
57
51
  }
58
52
 
59
- const epub = await new Promise(res => {
60
- user.get([check.pub, "epub"], res)
61
- })
53
+ const epub = await user.get([check.pub, "epub"])
62
54
  if (!epub) {
63
55
  console.error("User not found for new public key")
64
56
  break
65
57
  }
66
58
 
67
- const shared = await new Promise(res => {
68
- user.get("shared").next(contactCode, res)
69
- })
59
+ const shared = await user.get("shared").next(contactCode)
70
60
  if (!shared) break
71
61
 
72
62
  const oldSecret = await holster.SEA.secret({epub: oldEpub}, user.is)
@@ -80,10 +70,11 @@ export function useAccountSync(holster, user, host, code) {
80
70
  update[key] = enc
81
71
  }
82
72
  if (Object.keys(update).length !== 0) {
83
- const err = await new Promise(res => {
84
- user.get("shared").next(contactCode).put(update, res)
85
- })
86
- if (err) console.error(err)
73
+ await user
74
+ .get("shared")
75
+ .next(contactCode)
76
+ .put(update)
77
+ .catch(err => console.error(err))
87
78
  }
88
79
  }
89
80
  // Add the new contact if we referred them.
@@ -99,12 +90,15 @@ export function useAccountSync(holster, user, host, code) {
99
90
  }
100
91
  }
101
92
 
102
- user.get([host, "accounts"]).on(updateAccounts, true)
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})
103
97
 
104
98
  return () => {
105
99
  user.get([host, "accounts"]).off(updateAccounts)
106
100
  }
107
- }, [host, code])
101
+ }, [holster, user, host, code])
108
102
 
109
103
  return accountsReady
110
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"