@focura/auth-core 1.0.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 +138 -0
- package/dist/index.d.ts +593 -0
- package/dist/index.js +1362 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Focura
|
|
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,138 @@
|
|
|
1
|
+
# @focura/auth-core
|
|
2
|
+
|
|
3
|
+
Production-ready authentication core for Express.js backends.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Dual-token RS256 JWT** architecture (15min access / 7d refresh)
|
|
8
|
+
- **Token exchange** — HMAC-SHA256 signed proof between services
|
|
9
|
+
- **Refresh token rotation** — Atomic Lua script, distributed lock
|
|
10
|
+
- **Session binding** — Device fingerprint + IP validation
|
|
11
|
+
- **Session management** — Max concurrent sessions, eviction
|
|
12
|
+
- **Account lockout** — Configurable failure threshold
|
|
13
|
+
- **2FA (TOTP)** — Generate and verify time-based one-time passwords
|
|
14
|
+
- **Audit logging** — 50+ event types with severity levels
|
|
15
|
+
- **CSRF protection** — Redis-backed tokens
|
|
16
|
+
- **Rate limiting** — Sliding window, configurable per-route
|
|
17
|
+
- **Cache layer** — Auth result + user profile caching
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @focura/auth-core express ioredis
|
|
23
|
+
npm install -D @types/express
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { MiddlewareFactory, AccountLockout, SessionManager } from "@focura/auth-core";
|
|
30
|
+
import Redis from "ioredis";
|
|
31
|
+
|
|
32
|
+
const redis = new Redis(process.env.REDIS_URL!);
|
|
33
|
+
|
|
34
|
+
const auth = new MiddlewareFactory({
|
|
35
|
+
redis,
|
|
36
|
+
userStore: {
|
|
37
|
+
findById: (id) => prisma.user.findUnique({ where: { id } }),
|
|
38
|
+
findByEmail: (email) => prisma.user.findUnique({ where: { email } }),
|
|
39
|
+
update: (id, data) => prisma.user.update({ where: { id }, data }),
|
|
40
|
+
updateEmailVerified: (id, date) => prisma.user.update({ where: { id }, data: { emailVerified: date } }),
|
|
41
|
+
},
|
|
42
|
+
hmacSecret: process.env.NEXTAUTH_SECRET!,
|
|
43
|
+
jwt: {
|
|
44
|
+
privateKey: fs.readFileSync("keys/private.pem", "utf8"),
|
|
45
|
+
publicKey: fs.readFileSync("keys/public.pem", "utf8"),
|
|
46
|
+
},
|
|
47
|
+
cache: {
|
|
48
|
+
get: (key) => redis.get(key).then(JSON.parse),
|
|
49
|
+
set: (key, val, ttl) => redis.setex(key, ttl!, JSON.stringify(val)),
|
|
50
|
+
delete: (key) => redis.del(key),
|
|
51
|
+
},
|
|
52
|
+
auditLogger: {
|
|
53
|
+
log: (event, data) => prisma.auditLog.create({ data: { event, ...data } }),
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Auth routes
|
|
58
|
+
app.post("/api/v1/auth/exchange", auth.createExchangeHandler());
|
|
59
|
+
app.post("/api/v1/auth/refresh", auth.createRefreshHandler());
|
|
60
|
+
app.post("/api/v1/auth/logout", auth.createLogoutHandler());
|
|
61
|
+
|
|
62
|
+
// Protected routes
|
|
63
|
+
app.get("/api/v1/profile", auth.createAuthenticateMiddleware(), (req, res) => {
|
|
64
|
+
res.json({ user: req.user });
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
app.get("/api/v1/admin", auth.createAuthenticateMiddleware(), auth.createAuthorizeMiddleware("ADMIN"), (req, res) => {
|
|
68
|
+
res.json({ admin: true });
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Adapters
|
|
73
|
+
|
|
74
|
+
### RedisAdapter
|
|
75
|
+
|
|
76
|
+
Works with ioredis or any compatible client:
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import Redis from "ioredis";
|
|
80
|
+
const redis = new Redis(process.env.REDIS_URL);
|
|
81
|
+
|
|
82
|
+
// Pass directly — ioredis satisfies the RedisAdapter interface
|
|
83
|
+
const auth = new MiddlewareFactory({ redis, ... });
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### UserStore
|
|
87
|
+
|
|
88
|
+
Implement this to connect your database:
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
const userStore = {
|
|
92
|
+
findById: async (id) => db.user.findUnique({ where: { id } }),
|
|
93
|
+
findByEmail: async (email) => db.user.findUnique({ where: { email } }),
|
|
94
|
+
update: async (id, data) => db.user.update({ where: { id }, data }),
|
|
95
|
+
updateEmailVerified: async (id, date) => db.user.update({ where: { id }, data: { emailVerified: date } }),
|
|
96
|
+
};
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### CacheAdapter
|
|
100
|
+
|
|
101
|
+
Optional but recommended for performance:
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
const cache = {
|
|
105
|
+
get: async (key) => { const v = await redis.get(key); return v ? JSON.parse(v) : null; },
|
|
106
|
+
set: async (key, val, ttl) => { await redis.setex(key, ttl!, JSON.stringify(val)); },
|
|
107
|
+
delete: async (key) => { await redis.del(key); },
|
|
108
|
+
};
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Configuration
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
interface AuthCoreConfig {
|
|
115
|
+
redis: RedisAdapter; // Required
|
|
116
|
+
userStore: UserStore; // Required
|
|
117
|
+
hmacSecret: string; // Required — shared secret for token exchange
|
|
118
|
+
jwt: TokenConfig; // Required — RSA key pair
|
|
119
|
+
cache?: CacheAdapter; // Optional — enables auth result caching
|
|
120
|
+
auditLogger?: AuditLogger; // Optional — persists audit events
|
|
121
|
+
observability?: ObservabilitySink; // Optional — Sentry/Datadog integration
|
|
122
|
+
errors?: ErrorFactory; // Optional — custom error classes
|
|
123
|
+
keyPrefix?: string; // Default: "focura:"
|
|
124
|
+
lockout?: LockoutConfig; // Default: 10 failures / 15min lock / 1hr window
|
|
125
|
+
session?: SessionConfig; // Default: 7d inactivity / 7d absolute / 5 max concurrent
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Generated RSA Keys
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:2048
|
|
133
|
+
openssl rsa -in private.pem -pubout -out public.pem
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## License
|
|
137
|
+
|
|
138
|
+
MIT
|