@1auth/account 0.0.0-alpha.3 → 0.0.0-alpha.30

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.
Files changed (2) hide show
  1. package/index.js +25 -23
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -1,20 +1,26 @@
1
1
  import {
2
- subject,
2
+ randomId,
3
+ subject as randomSubject,
3
4
  makeSymetricKey,
4
5
  makeAsymmetricKeys,
5
- encrypt,
6
- decrypt
6
+ encryptFields,
7
+ decryptFields
7
8
  } from '@1auth/crypto'
8
9
 
9
10
  const options = {
10
11
  store: undefined,
11
12
  notify: undefined,
12
13
  table: 'accounts',
14
+ idGenerate: true,
15
+ idPrefix: 'account',
16
+ subPrefix: 'sub',
17
+ randomId: undefined,
18
+ randomSubject: undefined,
13
19
  encryptedKeys: []
14
20
  }
15
21
 
16
22
  export default (params) => {
17
- Object.assign(options, { id: subject }, params)
23
+ Object.assign(options, { randomId, randomSubject }, params)
18
24
  }
19
25
  export const getOptions = () => options
20
26
 
@@ -24,40 +30,37 @@ export const exists = async (sub) => {
24
30
 
25
31
  export const lookup = async (sub) => {
26
32
  const item = options.store.select(options.table, { sub })
27
- for (const key of options.encryptedKeys) {
28
- item[key] = await decrypt(item[key], item.encryptionKey, sub)
29
- }
33
+ decryptFields(item, item.encryptionKey, sub, options.encryptedKeys)
30
34
  delete item.encryptionKey
31
35
  delete item.privateKey
32
36
  return item
33
37
  }
34
38
 
35
39
  export const create = async (values = {}) => {
36
- const sub = await options.id.create()
40
+ const sub = await options.randomSubject.create(options.subPrefix)
37
41
 
38
- // const notifications = {}
39
- // const authorization = {}
40
- const now = nowInSeconds()
41
42
  const { encryptionKey, encryptedKey } = makeSymetricKey(sub)
42
43
  const { publicKey, privateKey } = await makeAsymmetricKeys(encryptionKey)
43
44
 
44
- for (const key of options.encryptedKeys) {
45
- values[key] &&= await encrypt(values[key], encryptionKey, sub)
46
- }
45
+ // TODO optimize: don't decrypt encryptionKey
46
+ encryptFields(values, encryptedKey, sub, options.encryptedKeys)
47
47
 
48
- await options.store.insert(options.table, {
48
+ const now = nowInSeconds()
49
+ const params = {
50
+ create: now, // allow use for migration import
49
51
  ...values,
50
52
  sub,
51
53
  encryptionKey: encryptedKey,
52
54
  publicKey,
53
55
  privateKey,
54
- create: now,
55
56
  update: now
56
- // notifications,
57
- // authorization
58
- })
57
+ }
58
+ if (options.idGenerate) {
59
+ params.id = await options.randomId.create(options.idPrefix)
60
+ }
61
+ await options.store.insert(options.table, params)
59
62
 
60
- // TODO update session, attach sub
63
+ // TODO update guest session, attach sub
61
64
  return sub
62
65
  }
63
66
 
@@ -67,9 +70,8 @@ export const update = async (sub, values = {}) => {
67
70
  sub
68
71
  })
69
72
 
70
- for (const key of options.encryptedKeys) {
71
- values[key] &&= await encrypt(values[key], encryptionKey, sub)
72
- }
73
+ encryptFields(values, encryptionKey, sub, options.encryptedKeys)
74
+
73
75
  await options.store.update(
74
76
  options.table,
75
77
  { sub },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1auth/account",
3
- "version": "0.0.0-alpha.3",
3
+ "version": "0.0.0-alpha.30",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "engines": {
@@ -48,8 +48,8 @@
48
48
  "url": "https://github.com/willfarrell/1auth/issues"
49
49
  },
50
50
  "homepage": "https://github.com/willfarrell/1auth",
51
- "gitHead": "a02b1e01f039718f213d79b91d04ed660a955c73",
51
+ "gitHead": "bba2971096bfd6bcc30e7613fae272896456ecfa",
52
52
  "dependencies": {
53
- "@1auth/crypto": "0.0.0-alpha.3"
53
+ "@1auth/crypto": "0.0.0-alpha.30"
54
54
  }
55
55
  }