@1auth/account 0.0.0-alpha.71 → 0.0.0-alpha.73

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 +105 -87
  2. package/package.json +53 -53
package/index.js CHANGED
@@ -1,123 +1,141 @@
1
1
  import {
2
- makeRandomConfigObject,
3
- symmetricGenerateEncryptionKey,
4
- makeAsymmetricKeys,
5
- symmetricEncryptFields,
6
- symmetricDecryptFields,
2
+ makeRandomConfigObject,
3
+ symmetricDecryptFields,
4
+ symmetricEncryptFields,
5
+ symmetricGenerateEncryptionKey,
7
6
  } from "@1auth/crypto";
8
7
 
9
8
  const id = "account";
10
9
 
11
10
  export const randomId = ({ prefix = "user_", ...params } = {}) =>
12
- makeRandomConfigObject({
13
- id,
14
- prefix,
15
- ...params,
16
- });
11
+ makeRandomConfigObject({
12
+ id,
13
+ prefix,
14
+ ...params,
15
+ });
17
16
 
18
17
  export const randomSubject = ({ prefix = "sub_", ...params } = {}) =>
19
- makeRandomConfigObject({
20
- id,
21
- prefix,
22
- ...params,
23
- });
18
+ makeRandomConfigObject({
19
+ id,
20
+ prefix,
21
+ ...params,
22
+ });
24
23
 
25
24
  const defaults = {
26
- id,
27
- store: undefined,
28
- notify: undefined,
29
- table: "accounts",
30
- idGenerate: true,
31
- randomId: randomId(),
32
- randomSubject: randomSubject(),
33
- encryptedFields: ["privateKey"], // TODO has encryption build-in
25
+ id,
26
+ store: undefined,
27
+ notify: undefined,
28
+ table: "accounts",
29
+ idGenerate: true,
30
+ randomId: randomId(),
31
+ randomSubject: randomSubject(),
32
+ encryptedFields: [],
34
33
  };
35
34
  const options = {};
36
35
  export default (params) => {
37
- Object.assign(options, defaults, params);
36
+ Object.assign(options, defaults, params);
38
37
  };
39
38
  export const getOptions = () => options;
40
39
 
41
40
  export const exists = async (sub) => {
42
- return options.store.exists(options.table, { sub });
41
+ if (!sub || typeof sub !== "string") {
42
+ throw new Error("404 Not Found", { cause: { sub } });
43
+ }
44
+ return options.store.exists(options.table, { sub });
43
45
  };
44
46
 
45
47
  export const lookup = async (sub) => {
46
- const account = await options.store.select(options.table, { sub });
47
- if (!account) return;
48
- const { encryptionKey: encryptedKey } = account;
49
- delete account.encryptionKey;
50
- delete account.privateKey;
51
- const decryptedAccount = symmetricDecryptFields(
52
- account,
53
- { encryptedKey, sub },
54
- options.encryptedFields,
55
- );
56
- return decryptedAccount;
48
+ if (!sub || typeof sub !== "string") {
49
+ throw new Error("404 Not Found", { cause: { sub } });
50
+ }
51
+ const account = await options.store.select(options.table, { sub });
52
+ if (!account) {
53
+ throw new Error("404 Not Found", { cause: { sub } });
54
+ }
55
+ const { encryptionKey: encryptedKey } = account;
56
+ account.encryptionKey = undefined;
57
+ const decryptedAccount = symmetricDecryptFields(
58
+ account,
59
+ { encryptedKey, sub },
60
+ options.encryptedFields,
61
+ );
62
+ return decryptedAccount;
57
63
  };
58
64
 
59
65
  export const create = async (values = {}) => {
60
- const sub = await options.randomSubject.create(options.subPrefix);
61
- const asymmetricKeys = await makeAsymmetricKeys();
62
-
63
- const { encryptionKey, encryptedKey } = symmetricGenerateEncryptionKey(sub);
64
- const encryptedValues = symmetricEncryptFields(
65
- { ...values, ...asymmetricKeys },
66
- { encryptionKey, sub },
67
- options.encryptedFields,
68
- );
69
-
70
- const now = nowInSeconds();
71
- const params = {
72
- create: now, // allow use for migration import
73
- ...encryptedValues,
74
- sub,
75
- encryptionKey: encryptedKey,
76
- update: now,
77
- };
78
- if (options.idGenerate) {
79
- params.id = await options.randomId.create(options.idPrefix);
80
- }
81
- await options.store.insert(options.table, params);
82
-
83
- // TODO update guest session, attach sub
84
- return sub;
66
+ const sub = await options.randomSubject.create(options.subPrefix);
67
+
68
+ const { encryptionKey, encryptedKey } = symmetricGenerateEncryptionKey(sub);
69
+ const encryptedValues = symmetricEncryptFields(
70
+ values,
71
+ { encryptionKey, sub },
72
+ options.encryptedFields,
73
+ );
74
+
75
+ const now = nowInSeconds();
76
+ const params = {
77
+ create: now, // allow use for migration import
78
+ ...encryptedValues,
79
+ sub,
80
+ encryptionKey: encryptedKey,
81
+ update: now,
82
+ };
83
+ if (options.idGenerate) {
84
+ params.id = await options.randomId.create(options.idPrefix);
85
+ }
86
+ await options.store.insert(options.table, params);
87
+
88
+ // TODO update guest session, attach sub
89
+ return sub;
85
90
  };
86
91
 
87
92
  // for in the clear user metadata
88
93
  export const update = async (sub, values = {}) => {
89
- const { encryptionKey: encryptedKey } = await options.store.select(
90
- options.table,
91
- {
92
- sub,
93
- },
94
- ["encryptionKey"],
95
- );
96
-
97
- values = symmetricEncryptFields(
98
- values,
99
- { encryptedKey, sub },
100
- options.encryptedFields,
101
- );
102
-
103
- await options.store.update(
104
- options.table,
105
- { sub },
106
- { ...values, update: nowInSeconds() },
107
- );
94
+ if (!sub || typeof sub !== "string") {
95
+ throw new Error("404 Not Found", { cause: { sub } });
96
+ }
97
+ const account = await options.store.select(
98
+ options.table,
99
+ {
100
+ sub,
101
+ },
102
+ ["encryptionKey"],
103
+ );
104
+ if (!account) {
105
+ throw new Error("404 Not Found", { cause: { sub } });
106
+ }
107
+ const { encryptionKey: encryptedKey } = account;
108
+
109
+ const encryptedValues = symmetricEncryptFields(
110
+ values,
111
+ { encryptedKey, sub },
112
+ options.encryptedFields,
113
+ );
114
+
115
+ await options.store.update(
116
+ options.table,
117
+ { sub },
118
+ { ...encryptedValues, update: nowInSeconds() },
119
+ );
108
120
  };
109
121
 
110
122
  export const expire = async (sub) => {
111
- await options.store.update(
112
- options.table,
113
- { sub },
114
- { expire: nowInSeconds() },
115
- );
123
+ if (!sub || typeof sub !== "string") {
124
+ throw new Error("401 Unauthorized", { cause: { sub } });
125
+ }
126
+ await options.store.update(
127
+ options.table,
128
+ { sub },
129
+ { expire: nowInSeconds() },
130
+ );
116
131
  };
117
132
 
118
133
  export const remove = async (sub) => {
119
- // Should trigger removal of credentials and messengers
120
- await options.store.remove(options.table, { sub });
134
+ if (!sub || typeof sub !== "string") {
135
+ throw new Error("404 Not Found", { cause: { sub } });
136
+ }
137
+ // Should trigger removal of credentials and messengers
138
+ await options.store.remove(options.table, { sub });
121
139
  };
122
140
 
123
141
  /* export const expire = async (sub) => {
package/package.json CHANGED
@@ -1,55 +1,55 @@
1
1
  {
2
- "name": "@1auth/account",
3
- "version": "0.0.0-alpha.71",
4
- "description": "",
5
- "type": "module",
6
- "engines": {
7
- "node": ">=20"
8
- },
9
- "engineStrict": true,
10
- "publishConfig": {
11
- "access": "public"
12
- },
13
- "main": "./index.js",
14
- "module": "./index.js",
15
- "exports": {
16
- ".": {
17
- "import": {
18
- "types": "./index.d.ts",
19
- "default": "./index.js"
20
- }
21
- }
22
- },
23
- "types": "index.d.ts",
24
- "files": [
25
- "index.js",
26
- "index.d.ts"
27
- ],
28
- "scripts": {
29
- "test": "npm run test:unit",
30
- "test:unit": "node --test"
31
- },
32
- "license": "MIT",
33
- "funding": {
34
- "type": "github",
35
- "url": "https://github.com/sponsors/willfarrell"
36
- },
37
- "keywords": [],
38
- "author": {
39
- "name": "1auth contributors",
40
- "url": "https://github.com/willfarrell/1auth/graphs/contributors"
41
- },
42
- "repository": {
43
- "type": "git",
44
- "url": "git+https://github.com/willfarrell/1auth.git",
45
- "directory": "packages/account"
46
- },
47
- "bugs": {
48
- "url": "https://github.com/willfarrell/1auth/issues"
49
- },
50
- "homepage": "https://github.com/willfarrell/1auth",
51
- "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431",
52
- "dependencies": {
53
- "@1auth/crypto": "0.0.0-alpha.71"
54
- }
2
+ "name": "@1auth/account",
3
+ "version": "0.0.0-alpha.73",
4
+ "description": "",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=22"
8
+ },
9
+ "engineStrict": true,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "main": "./index.js",
14
+ "module": "./index.js",
15
+ "exports": {
16
+ ".": {
17
+ "import": {
18
+ "types": "./index.d.ts",
19
+ "default": "./index.js"
20
+ }
21
+ }
22
+ },
23
+ "types": "index.d.ts",
24
+ "files": [
25
+ "index.js",
26
+ "index.d.ts"
27
+ ],
28
+ "scripts": {
29
+ "test": "npm run test:unit",
30
+ "test:unit": "node --test"
31
+ },
32
+ "license": "MIT",
33
+ "funding": {
34
+ "type": "github",
35
+ "url": "https://github.com/sponsors/willfarrell"
36
+ },
37
+ "keywords": [],
38
+ "author": {
39
+ "name": "1auth contributors",
40
+ "url": "https://github.com/willfarrell/1auth/graphs/contributors"
41
+ },
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+https://github.com/willfarrell/1auth.git",
45
+ "directory": "packages/account"
46
+ },
47
+ "bugs": {
48
+ "url": "https://github.com/willfarrell/1auth/issues"
49
+ },
50
+ "homepage": "https://github.com/willfarrell/1auth",
51
+ "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431",
52
+ "dependencies": {
53
+ "@1auth/crypto": "0.0.0-alpha.73"
54
+ }
55
55
  }