@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.
- package/index.js +105 -87
- package/package.json +53 -53
package/index.js
CHANGED
|
@@ -1,123 +1,141 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
makeRandomConfigObject({
|
|
12
|
+
id,
|
|
13
|
+
prefix,
|
|
14
|
+
...params,
|
|
15
|
+
});
|
|
17
16
|
|
|
18
17
|
export const randomSubject = ({ prefix = "sub_", ...params } = {}) =>
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
makeRandomConfigObject({
|
|
19
|
+
id,
|
|
20
|
+
prefix,
|
|
21
|
+
...params,
|
|
22
|
+
});
|
|
24
23
|
|
|
25
24
|
const defaults = {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
36
|
+
Object.assign(options, defaults, params);
|
|
38
37
|
};
|
|
39
38
|
export const getOptions = () => options;
|
|
40
39
|
|
|
41
40
|
export const exists = async (sub) => {
|
|
42
|
-
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
-
|
|
120
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
}
|