@1auth/account 0.0.0-alpha.68 → 0.0.0-alpha.69
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 +141 -0
- package/package.json +2 -2
package/index.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import {
|
|
2
|
+
makeRandomConfigObject,
|
|
3
|
+
symmetricGenerateEncryptionKey,
|
|
4
|
+
makeAsymmetricKeys,
|
|
5
|
+
symmetricEncryptFields,
|
|
6
|
+
symmetricDecryptFields,
|
|
7
|
+
} from "@1auth/crypto";
|
|
8
|
+
|
|
9
|
+
const id = "account";
|
|
10
|
+
|
|
11
|
+
export const randomId = ({ prefix = "user_", ...params } = {}) =>
|
|
12
|
+
makeRandomConfigObject({
|
|
13
|
+
id,
|
|
14
|
+
prefix,
|
|
15
|
+
...params,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export const randomSubject = ({ prefix = "sub_", ...params } = {}) =>
|
|
19
|
+
makeRandomConfigObject({
|
|
20
|
+
id,
|
|
21
|
+
prefix,
|
|
22
|
+
...params,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
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
|
|
34
|
+
};
|
|
35
|
+
const options = {};
|
|
36
|
+
export default (params) => {
|
|
37
|
+
Object.assign(options, defaults, params);
|
|
38
|
+
};
|
|
39
|
+
export const getOptions = () => options;
|
|
40
|
+
|
|
41
|
+
export const exists = async (sub) => {
|
|
42
|
+
return options.store.exists(options.table, { sub });
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
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;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
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;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// for in the clear user metadata
|
|
88
|
+
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
|
+
);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export const expire = async (sub) => {
|
|
111
|
+
await options.store.update(
|
|
112
|
+
options.table,
|
|
113
|
+
{ sub },
|
|
114
|
+
{ expire: nowInSeconds() },
|
|
115
|
+
);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export const remove = async (sub) => {
|
|
119
|
+
// Should trigger removal of credentials and messengers
|
|
120
|
+
await options.store.remove(options.table, { sub });
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/* export const expire = async (sub) => {
|
|
124
|
+
const expire = nowInSeconds() + 90 * 24 * 60 * 60
|
|
125
|
+
await options.store.update(options.table, { sub }, { expire })
|
|
126
|
+
await options.notify.trigger('account-expire', sub)
|
|
127
|
+
// TODO clear sessions
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export const recover = async (sub) => {
|
|
131
|
+
await options.store.update(options.table, { sub }, { expire: null })
|
|
132
|
+
await options.notify.trigger('account-recover', sub)
|
|
133
|
+
} */
|
|
134
|
+
|
|
135
|
+
// TODO manage onboard state
|
|
136
|
+
|
|
137
|
+
// TODO save notification settings
|
|
138
|
+
|
|
139
|
+
// TODO authorize management?
|
|
140
|
+
|
|
141
|
+
const nowInSeconds = () => Math.floor(Date.now() / 1000);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@1auth/account",
|
|
3
|
-
"version": "0.0.0-alpha.
|
|
3
|
+
"version": "0.0.0-alpha.69",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -50,6 +50,6 @@
|
|
|
50
50
|
"homepage": "https://github.com/willfarrell/1auth",
|
|
51
51
|
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431",
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@1auth/crypto": "0.0.0-alpha.
|
|
53
|
+
"@1auth/crypto": "0.0.0-alpha.69"
|
|
54
54
|
}
|
|
55
55
|
}
|