@1auth/authn 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.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/index.js +166 -0
  3. 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,166 @@
1
+ import { setTimeout } from 'node:timers/promises'
2
+ import { randomId, makeSymetricKey } from '@1auth/crypto'
3
+
4
+ export const options = {
5
+ store: undefined,
6
+ notify: undefined,
7
+ table: 'credentials',
8
+ authenticationDuration: 500, // min duration authentication should take (ms)
9
+ usernameExists: [] // hooks to allow what to be used as a username
10
+ }
11
+ export default (params) => {
12
+ Object.assign(options, params)
13
+ }
14
+
15
+ export const create = async (
16
+ credentialType,
17
+ { id, sub, value, ...rest },
18
+ parentOptions
19
+ ) => {
20
+ const now = nowInSeconds()
21
+ id ??= await randomId.create()
22
+ const type = parentOptions.id + '-' + parentOptions[credentialType].type
23
+ const otp = parentOptions[credentialType].otp
24
+ const expire = parentOptions[credentialType].expire
25
+ ? now + parentOptions[credentialType].expire
26
+ : null
27
+ const { encryptedKey } = makeSymetricKey(sub)
28
+
29
+ const encryptedData = await parentOptions[credentialType].encode(
30
+ value,
31
+ encryptedKey,
32
+ sub
33
+ )
34
+ return parentOptions.store.insert(options.table, {
35
+ expire,
36
+ ...rest,
37
+ id,
38
+ sub,
39
+ type,
40
+ otp,
41
+ encryptionKey: encryptedKey,
42
+ value: encryptedData,
43
+ create: now,
44
+ update: now
45
+ })
46
+ }
47
+
48
+ export const update = async (
49
+ credentialType,
50
+ { id, sub, encryptionKey, value, ...rest },
51
+ parentOptions
52
+ ) => {
53
+ const now = nowInSeconds()
54
+ const encryptedData = await parentOptions[credentialType].encode(
55
+ value,
56
+ encryptionKey,
57
+ sub
58
+ )
59
+ return parentOptions.store.update(
60
+ options.table,
61
+ { id, sub },
62
+ {
63
+ ...rest,
64
+ value: encryptedData,
65
+ update: now
66
+ }
67
+ )
68
+ }
69
+
70
+ export const subject = async (username) => {
71
+ return Promise.all(
72
+ options.usernameExists.map((exists) => exists(username))
73
+ ).then((identities) => {
74
+ return identities.filter((lookup) => lookup)?.[0]
75
+ })
76
+ }
77
+
78
+ export const authenticate = async (username, secret, parentOptions) => {
79
+ const timeout = setTimeout(() => {}, options.authenticationDuration)
80
+ const type = parentOptions.id + '-' + parentOptions.secret.type
81
+
82
+ const sub = await subject(username)
83
+
84
+ const credentials = await parentOptions.store.selectList(options.table, {
85
+ sub,
86
+ type
87
+ }) // TODO and verify is not null
88
+ let valid, id, encryptionKey
89
+ for (const credential of credentials) {
90
+ let { value, encryptionKey: encryptedKey, ...rest } = credential
91
+ value = await parentOptions.secret.decode(value, encryptedKey, sub)
92
+ valid = await parentOptions.secret.verify(secret, value, rest)
93
+ if (valid) {
94
+ id ??= credential.id
95
+ encryptionKey ??= encryptedKey
96
+ break
97
+ }
98
+ }
99
+
100
+ if (valid && parentOptions.secret.otp) {
101
+ await parentOptions.store.remove(options.table, { id, sub })
102
+ }
103
+ await timeout
104
+ if (!valid) throw new Error('401 Unauthorized')
105
+ return { sub, id, encryptionKey, ...valid }
106
+ }
107
+
108
+ export const verifySecret = async (sub, id, parentOptions) => {
109
+ // const type = parentOptions.id + '-' + parentOptions.secret.type
110
+ await parentOptions.store.update(
111
+ options.table,
112
+ { id, sub },
113
+ { verify: nowInSeconds() }
114
+ )
115
+ }
116
+
117
+ export const verify = async (credentialType, sub, token, parentOptions) => {
118
+ const timeout = setTimeout(() => {}, options.authenticationDuration)
119
+ const type = parentOptions.id + '-' + parentOptions[credentialType].type
120
+ let id
121
+ const credentials = await parentOptions.store.selectList(options.table, {
122
+ sub,
123
+ type
124
+ })
125
+ // TODO re-confirm when needed
126
+ // .then((rows) => {
127
+ // if (rows.length) {
128
+ // return rows
129
+ // }
130
+ //
131
+ // return parentOptions.store.select(options.table, { id: sub, type })
132
+ // })
133
+
134
+ let valid
135
+ for (const credential of credentials) {
136
+ let { value, encryptionKey, ...rest } = credential
137
+ value = await parentOptions[credentialType].decode(
138
+ value,
139
+ encryptionKey,
140
+ sub
141
+ )
142
+ valid = await parentOptions[credentialType].verify(token, value, rest)
143
+ if (valid) {
144
+ id = credential.id
145
+ break
146
+ }
147
+ }
148
+ if (valid && parentOptions[credentialType].otp) {
149
+ await parentOptions.store.remove(options.table, { id, sub })
150
+ }
151
+ if (!valid) throw new Error('401 Unauthorized')
152
+ await timeout
153
+ return { sub, id, ...valid }
154
+ }
155
+
156
+ export const expire = async (sub, id, parentOptions = options) => {
157
+ await parentOptions.store.remove(options.table, { id, sub })
158
+ }
159
+
160
+ // TODO manage onboard state
161
+
162
+ // TODO save notification settings
163
+
164
+ // TODO authorize management?
165
+
166
+ const nowInSeconds = () => Math.floor(Date.now() / 1000)
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@1auth/authn",
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/authn"
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
+ }