@plinthjs/auth 0.1.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/README.md +115 -0
- package/dist/index.cjs +885 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +698 -0
- package/dist/index.d.ts +698 -0
- package/dist/index.js +852 -0
- package/dist/index.js.map +1 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The Mason contributors
|
|
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/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# @plinthjs/auth
|
|
2
|
+
|
|
3
|
+
Mason's authentication layer, the `Illuminate\Auth` equivalent. The `Authenticatable` /
|
|
4
|
+
`UserProvider` / `Guard` contracts, an in-memory `ArrayUserProvider` over `GenericUser`s (passwords
|
|
5
|
+
verified through `@plinthjs/encryption`), a stateful `SessionGuard` (with `attemptWhen`, HTTP Basic,
|
|
6
|
+
remember-me and the logout variants), a stateless `TokenGuard`, an `AuthManager` for named guards,
|
|
7
|
+
password resets, signed email verification, the Laravel auth event classes, and HTTP middleware.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install @plinthjs/auth
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { Hasher } from '@plinthjs/encryption'
|
|
19
|
+
import { Session } from '@plinthjs/session'
|
|
20
|
+
import { ArrayUserProvider, GenericUser, SessionGuard } from '@plinthjs/auth'
|
|
21
|
+
|
|
22
|
+
const hasher = new Hasher()
|
|
23
|
+
const users = new ArrayUserProvider(
|
|
24
|
+
[new GenericUser({ id: 1, email: 'ada@example.com', password: hasher.make('secret') })],
|
|
25
|
+
hasher,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
const guard = new SessionGuard(users, new Session('session-id', {}))
|
|
29
|
+
|
|
30
|
+
await guard.attempt({ email: 'ada@example.com', password: 'secret' }) // true
|
|
31
|
+
await guard.check() // true
|
|
32
|
+
await guard.id() // 1
|
|
33
|
+
|
|
34
|
+
// Extra checks that must all pass before the user is logged in.
|
|
35
|
+
await guard.attemptWhen({ email: 'ada@example.com', password: 'secret' }, [
|
|
36
|
+
(user) => user.getAuthIdentifier() !== 2,
|
|
37
|
+
])
|
|
38
|
+
|
|
39
|
+
await guard.logout() // also cycles the remember token
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
`SessionGuard` also offers `once`, `loginUsingId`, `remember(user)` / `loginUsingRecaller(value)`,
|
|
43
|
+
`basic()` / `onceBasic()` (HTTP Basic, returning a 401 challenge response on failure),
|
|
44
|
+
`logoutCurrentDevice()` and `logoutOtherDevices(password)`.
|
|
45
|
+
|
|
46
|
+
## Token and request guards
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
import { Request } from '@plinthjs/http'
|
|
50
|
+
import { AuthManager, TokenGuard } from '@plinthjs/auth'
|
|
51
|
+
|
|
52
|
+
// Reads a bearer token, or the `api_token` query/input field.
|
|
53
|
+
const api = new TokenGuard(users, new Request({ headers: { authorization: 'Bearer tok-123' } }))
|
|
54
|
+
await api.user()
|
|
55
|
+
|
|
56
|
+
const auth = new AuthManager('web')
|
|
57
|
+
.extend('api', (request) => new TokenGuard(users, request!))
|
|
58
|
+
.viaRequest('custom', (request) =>
|
|
59
|
+
request.bearerToken() === 'tok-123' ? users.retrieveById(1) : null,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
auth.guard('custom', new Request({ headers: { authorization: 'Bearer tok-123' } })) // a RequestGuard
|
|
63
|
+
auth.guard('nope') // throws: guard not defined
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Middleware
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import { HttpKernel, Response, Router } from '@plinthjs/http'
|
|
70
|
+
import { ArraySessionStore, SessionManager, startSession } from '@plinthjs/session'
|
|
71
|
+
import { authenticateSession, authenticateToken, getUser, requirePassword } from '@plinthjs/auth'
|
|
72
|
+
|
|
73
|
+
const router = new Router()
|
|
74
|
+
router.get('/me', (req) => Response.json({ id: getUser(req)?.getAuthIdentifier() ?? null }))
|
|
75
|
+
|
|
76
|
+
const sessions = new SessionManager(new ArraySessionStore())
|
|
77
|
+
const kernel = new HttpKernel(router, {
|
|
78
|
+
middleware: [startSession(sessions), authenticateSession(users)],
|
|
79
|
+
})
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
- `authenticateSession(provider)` resolves the user from the session (throwing a 401
|
|
83
|
+
`AuthenticationError` otherwise) and logs out sessions whose stored password hash is stale.
|
|
84
|
+
- `authenticateToken(provider, inputKey?)` is the stateless API equivalent.
|
|
85
|
+
- `redirectIfAuthenticated(provider, '/home')` keeps signed-in users off guest pages.
|
|
86
|
+
- `requirePassword('/confirm-password', seconds)` demands a recent `confirmPassword(...)`.
|
|
87
|
+
|
|
88
|
+
## Password resets
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { ArrayTokenRepository, PasswordBroker, PasswordStatus } from '@plinthjs/auth'
|
|
92
|
+
|
|
93
|
+
const broker = new PasswordBroker(users, new ArrayTokenRepository({ hasher, expireMinutes: 60 }))
|
|
94
|
+
|
|
95
|
+
let token = ''
|
|
96
|
+
await broker.sendResetLink({ email: 'ada@example.com' }, (user, issued) => {
|
|
97
|
+
token = issued // email the link to the user here
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
const status = await broker.reset(
|
|
101
|
+
{ email: 'ada@example.com', token, password: 'new-secret' },
|
|
102
|
+
async (user, password) => saveHashedPassword(user, hasher.make(password)),
|
|
103
|
+
)
|
|
104
|
+
status === PasswordStatus.PasswordReset
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Email verification and events
|
|
108
|
+
|
|
109
|
+
`EmailVerifier({ secret, expireMinutes })` signs a `{ expires, signature }` payload for any user
|
|
110
|
+
implementing `MustVerifyEmail`; `verify(user, payload)` checks it and marks the email verified.
|
|
111
|
+
|
|
112
|
+
Guards, the broker and the verifier dispatch `Attempting`, `Login`, `Failed`, `Logout`,
|
|
113
|
+
`PasswordReset`, `Verified` and the other Laravel event classes through any object with a
|
|
114
|
+
`dispatch(event)` method (the `AuthEvents` seam), passed as the `events` option, or as the third
|
|
115
|
+
argument of the `PasswordBroker` constructor.
|