@1auth/account 0.0.0-alpha.0
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/LICENSE +21 -0
- package/index.js +98 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 will Farrell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import {
|
|
2
|
+
subject,
|
|
3
|
+
makeSymetricKey,
|
|
4
|
+
makeAsymmetricKeys,
|
|
5
|
+
encrypt,
|
|
6
|
+
decrypt
|
|
7
|
+
} from '@1auth/crypto'
|
|
8
|
+
|
|
9
|
+
export const options = {
|
|
10
|
+
store: undefined,
|
|
11
|
+
notify: undefined,
|
|
12
|
+
table: 'accounts',
|
|
13
|
+
encryptedKeys: []
|
|
14
|
+
}
|
|
15
|
+
export default (params) => {
|
|
16
|
+
Object.assign(options, { id: subject }, params)
|
|
17
|
+
}
|
|
18
|
+
export const exists = async (sub) => {
|
|
19
|
+
return options.store.exists(options.table, { sub })
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const lookup = async (sub) => {
|
|
23
|
+
const item = options.store.select(options.table, { sub })
|
|
24
|
+
for (const key of options.encryptedKeys) {
|
|
25
|
+
item[key] = await decrypt(item[key], item.encryptionKey, sub)
|
|
26
|
+
}
|
|
27
|
+
delete item.encryptionKey
|
|
28
|
+
delete item.privateKey
|
|
29
|
+
return item
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const create = async (values = {}) => {
|
|
33
|
+
const sub = await options.id.create()
|
|
34
|
+
|
|
35
|
+
// const notifications = {}
|
|
36
|
+
// const authorization = {}
|
|
37
|
+
const now = nowInSeconds()
|
|
38
|
+
const { encryptionKey, encryptedKey } = makeSymetricKey(sub)
|
|
39
|
+
const { publicKey, privateKey } = await makeAsymmetricKeys(encryptionKey)
|
|
40
|
+
|
|
41
|
+
for (const key of options.encryptedKeys) {
|
|
42
|
+
values[key] &&= await encrypt(values[key], encryptionKey, sub)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
await options.store.insert(options.table, {
|
|
46
|
+
...values,
|
|
47
|
+
sub,
|
|
48
|
+
encryptionKey: encryptedKey,
|
|
49
|
+
publicKey,
|
|
50
|
+
privateKey,
|
|
51
|
+
create: now,
|
|
52
|
+
update: now
|
|
53
|
+
// notifications,
|
|
54
|
+
// authorization
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
// TODO update session, attach sub
|
|
58
|
+
return sub
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// for in the clear user metadata
|
|
62
|
+
export const update = async (sub, values = {}) => {
|
|
63
|
+
const { encryptionKey } = await options.store.select(options.table, { sub })
|
|
64
|
+
|
|
65
|
+
for (const key of options.encryptedKeys) {
|
|
66
|
+
values[key] &&= await encrypt(values[key], encryptionKey, sub)
|
|
67
|
+
}
|
|
68
|
+
await options.store.update(
|
|
69
|
+
options.table,
|
|
70
|
+
{ sub },
|
|
71
|
+
{ ...values, update: nowInSeconds() }
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const remove = async (sub) => {
|
|
76
|
+
await options.store.remove(options.table, { sub }) // Should trigger removal of credentials and messengers
|
|
77
|
+
await options.notify.trigger('account-remove', sub)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/* export const expire = async (sub) => {
|
|
81
|
+
const expire = nowInSeconds() + 90 * 24 * 60 * 60
|
|
82
|
+
await options.store.update(options.table, { sub }, { expire })
|
|
83
|
+
await options.notify.trigger('account-expire', sub)
|
|
84
|
+
// TODO clear sessions
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export const recover = async (sub) => {
|
|
88
|
+
await options.store.update(options.table, { sub }, { expire: null })
|
|
89
|
+
await options.notify.trigger('account-recover', sub)
|
|
90
|
+
} */
|
|
91
|
+
|
|
92
|
+
// TODO manage onboard state
|
|
93
|
+
|
|
94
|
+
// TODO save notification settings
|
|
95
|
+
|
|
96
|
+
// TODO authorize management?
|
|
97
|
+
|
|
98
|
+
const nowInSeconds = () => Math.floor(Date.now() / 1000)
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@1auth/account",
|
|
3
|
+
"version": "0.0.0-alpha.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=18"
|
|
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": "ava"
|
|
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": "github:willfarrell/1auth",
|
|
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": "dcd3cb3dcacdebbe21625f092187e7cf02473f68",
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@1auth/crypto": "0.0.0-alpha.0"
|
|
54
|
+
}
|
|
55
|
+
}
|