@1auth/account 0.0.0-alpha.32 → 0.0.0-alpha.34

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 (3) hide show
  1. package/README.md +50 -0
  2. package/index.js +61 -28
  3. package/package.json +5 -5
package/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # @1auth/account
2
+
3
+
4
+
5
+
6
+ ## Getting started
7
+
8
+ **auth.js**
9
+ ```javascript
10
+
11
+ import * as notify from "@1auth/notify-console";
12
+ import * as store from "@1auth/store-memory";
13
+ import crypto from "@1auth/crypto";
14
+ import account from "@1auth/account";
15
+
16
+ export {
17
+ exists as accountExists,
18
+ create as accountCreate,
19
+ lookup as accountLookup,
20
+ update as accountUpdate,
21
+ remove as accountRemove
22
+ } from '@1auth/account'
23
+
24
+ // Initialize
25
+ crypto({ symetricEncryptionKey: '_32B_APPLICATION_ENCRYPTION_KEY_' });
26
+ account({ store, notify });
27
+ ```
28
+
29
+ ```javascript
30
+ import {accountUsernameCreate} from './auth.js'
31
+ const sub = await accountCreate()
32
+ ```
33
+
34
+ ## Options
35
+
36
+ - `notify` [Required]: Notify module
37
+ - `store` [Required]: Store module
38
+ - `encryptedFields`: Fields that should be encrypted. Defaults to `["privateKey"]`
39
+ - `table`: database table name. Defaults to `accounts`
40
+
41
+ ## Database table
42
+
43
+ - `id`
44
+ - `sub`
45
+ - `encryptionKey`
46
+ - `publicKey`
47
+ - `privateKey`
48
+ - `create`
49
+ - `update`
50
+ - Any additional columns
package/index.js CHANGED
@@ -1,26 +1,37 @@
1
1
  import {
2
+ entropyToCharacterLength,
3
+ charactersAlphaNumeric,
4
+ randomAlphaNumeric,
2
5
  randomId,
3
- subject as randomSubject,
4
6
  makeSymetricKey,
5
7
  makeAsymmetricKeys,
6
- encryptFields,
7
- decryptFields
8
+ symetricEncryptFields,
9
+ symetricDecryptFields
8
10
  } from '@1auth/crypto'
9
11
 
10
- const options = {
12
+ const id = 'account'
13
+ const randomSubject = {
14
+ type: 'id',
15
+ minLength: entropyToCharacterLength(64, charactersAlphaNumeric.length),
16
+ create: async (prefix) =>
17
+ (prefix ? prefix + '_' : '') + randomAlphaNumeric(randomSubject.minLength)
18
+ }
19
+
20
+ const defaults = {
21
+ id,
11
22
  store: undefined,
12
23
  notify: undefined,
13
24
  table: 'accounts',
14
25
  idGenerate: true,
15
- idPrefix: 'account',
26
+ idPrefix: 'user',
16
27
  subPrefix: 'sub',
17
- randomId: undefined,
18
- randomSubject: undefined,
19
- encryptedKeys: []
28
+ randomId,
29
+ randomSubject,
30
+ encryptedFields: ['privateKey'] // TODO has encryption build-in
20
31
  }
21
-
32
+ const options = {}
22
33
  export default (params) => {
23
- Object.assign(options, { randomId, randomSubject }, params)
34
+ Object.assign(options, defaults, params)
24
35
  }
25
36
  export const getOptions = () => options
26
37
 
@@ -29,30 +40,36 @@ export const exists = async (sub) => {
29
40
  }
30
41
 
31
42
  export const lookup = async (sub) => {
32
- const item = options.store.select(options.table, { sub })
33
- decryptFields(item, item.encryptionKey, sub, options.encryptedKeys)
34
- delete item.encryptionKey
35
- delete item.privateKey
36
- return item
43
+ const account = await options.store.select(options.table, { sub })
44
+ if (!account) return
45
+ const { encryptionKey: encryptedKey } = account
46
+ delete account.encryptionKey
47
+ delete account.privateKey
48
+ const decryptedAccount = symetricDecryptFields(
49
+ account,
50
+ { encryptedKey, sub },
51
+ options.encryptedFields
52
+ )
53
+ return decryptedAccount
37
54
  }
38
55
 
39
56
  export const create = async (values = {}) => {
40
57
  const sub = await options.randomSubject.create(options.subPrefix)
58
+ const asymmetricKeys = await makeAsymmetricKeys()
41
59
 
42
60
  const { encryptionKey, encryptedKey } = makeSymetricKey(sub)
43
- const { publicKey, privateKey } = await makeAsymmetricKeys(encryptionKey)
44
-
45
- // TODO optimize: don't decrypt encryptionKey
46
- encryptFields(values, encryptedKey, sub, options.encryptedKeys)
61
+ const encryptedValues = symetricEncryptFields(
62
+ { ...values, ...asymmetricKeys },
63
+ { encryptionKey, sub },
64
+ options.encryptedFields
65
+ )
47
66
 
48
67
  const now = nowInSeconds()
49
68
  const params = {
50
69
  create: now, // allow use for migration import
51
- ...values,
70
+ ...encryptedValues,
52
71
  sub,
53
72
  encryptionKey: encryptedKey,
54
- publicKey,
55
- privateKey,
56
73
  update: now
57
74
  }
58
75
  if (options.idGenerate) {
@@ -66,11 +83,19 @@ export const create = async (values = {}) => {
66
83
 
67
84
  // for in the clear user metadata
68
85
  export const update = async (sub, values = {}) => {
69
- const { encryptionKey } = await options.store.select(options.table, {
70
- sub
71
- })
86
+ const { encryptionKey: encryptedKey } = await options.store.select(
87
+ options.table,
88
+ {
89
+ sub
90
+ },
91
+ ['encryptionKey']
92
+ )
72
93
 
73
- encryptFields(values, encryptionKey, sub, options.encryptedKeys)
94
+ values = symetricEncryptFields(
95
+ values,
96
+ { encryptedKey, sub },
97
+ options.encryptedFields
98
+ )
74
99
 
75
100
  await options.store.update(
76
101
  options.table,
@@ -79,9 +104,17 @@ export const update = async (sub, values = {}) => {
79
104
  )
80
105
  }
81
106
 
107
+ export const expire = async (sub) => {
108
+ await options.store.update(
109
+ options.table,
110
+ { sub },
111
+ { expire: nowInSeconds() }
112
+ )
113
+ }
114
+
82
115
  export const remove = async (sub) => {
83
- await options.store.remove(options.table, { sub }) // Should trigger removal of credentials and messengers
84
- await options.notify.trigger('account-remove', sub)
116
+ // Should trigger removal of credentials and messengers
117
+ await options.store.remove(options.table, { sub })
85
118
  }
86
119
 
87
120
  /* export const expire = async (sub) => {
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@1auth/account",
3
- "version": "0.0.0-alpha.32",
3
+ "version": "0.0.0-alpha.34",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "engines": {
7
- "node": ">=16"
7
+ "node": ">=20"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
@@ -27,7 +27,7 @@
27
27
  ],
28
28
  "scripts": {
29
29
  "test": "npm run test:unit",
30
- "test:unit": "ava"
30
+ "test:unit": "node --test"
31
31
  },
32
32
  "license": "MIT",
33
33
  "funding": {
@@ -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": "3750bef3d7e376c48f7d680e5f2181ee809213b9",
51
+ "gitHead": "c88105a99efd7f3de80795736d6194e52ef465b4",
52
52
  "dependencies": {
53
- "@1auth/crypto": "0.0.0-alpha.32"
53
+ "@1auth/crypto": "0.0.0-alpha.34"
54
54
  }
55
55
  }