kroxt 1.1.1 → 1.1.2

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/README.md +169 -82
  3. package/package.json +2 -1
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kroxt Auth
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 CHANGED
@@ -1,82 +1,169 @@
1
- # kroxt
2
-
3
- A framework-agnostic, modular authentication engine for modern TypeScript applications. Built for security, extensibility, and ease of use.
4
-
5
- ## Features
6
-
7
- - 🔐 **Secure Hashing**: Powered by `argon2` for industry-standard password security.
8
- - 🎟️ **Stateless Sessions**: Managed via `jose` with high-performance JWT signing and verification.
9
- - 🌍 **OAuth Ready**: Built-in support for GitHub and Google OAuth via `arctic`.
10
- - 🧩 **Database Agnostic**: Use Mongoose, Prisma, Drizzle, or even in-memory stores via the `AuthAdapter` pattern.
11
- - **Zod Schema Support**: Perfectly preserves and types your extended user metadata.
12
- - 🚀 **ESM First**: Native support for NodeNext module resolution.
13
-
14
- ## Installation
15
-
16
- ```bash
17
- npm install kroxt
18
- ```
19
-
20
- ## Quick Start
21
-
22
- ### 1. Initialize the Auth Engine
23
-
24
- ```typescript
25
- import { createAuth } from "kroxt";
26
- import { myDatabaseAdapter } from "./myAdapter.js";
27
-
28
- const auth = createAuth({
29
- adapter: myDatabaseAdapter,
30
- secret: process.env.AUTH_SECRET,
31
- session: {
32
- expires: "7d" // jose compatible duration
33
- }
34
- });
35
- ```
36
-
37
- ### 2. Sign Up a User
38
-
39
- ```typescript
40
- const { user, token } = await auth.signup({
41
- email: "user@example.com",
42
- firstName: "Tobi",
43
- role: "tenant",
44
- // ...any other extended fields supported by your adapter
45
- }, "strong-password-123");
46
- ```
47
-
48
- ### 3. Log In
49
-
50
- ```typescript
51
- const { user, token } = await auth.loginWithPassword("user@example.com", "password");
52
- ```
53
-
54
- ### 4. Verify a Session
55
-
56
- ```typescript
57
- const payload = await auth.verifyToken(token);
58
- // auth.verifyToken returns the signed payload { sub: string, role: string, ... }
59
- ```
60
-
61
- ## The Adapter Pattern
62
-
63
- Gatekeeper doesn't care which DB you use. You just need to implement the `AuthAdapter` interface:
64
-
65
- ```typescript
66
- import type { AuthAdapter, User } from "kroxt/adapter";
67
-
68
- export const myAdapter: AuthAdapter = {
69
- createUser: async (data) => { /* logic */ },
70
- findUserByEmail: async (email) => { /* logic */ },
71
- findUserById: async (id) => { /* logic */ },
72
- linkOAuthAccount: async (user, provider, provId) => { /* logic */ }
73
- };
74
- ```
75
-
76
- ## Reference Project
77
-
78
- Check out the `test-project` folder for a complete **Express + MongoDB** implementation using this library.
79
-
80
- ## License
81
-
82
- ISC
1
+ # kroxt
2
+
3
+ A framework-agnostic, modular authentication engine for modern TypeScript applications. Built for security, extensibility, and ease of use.
4
+
5
+ ## Features
6
+
7
+ - 🔐 **Secure Hashing**: Powered by `argon2` for industry-standard password security.
8
+ - 🎟️ **Dual-Token Sessions**: Native support for Access and Refresh tokens via `jose`.
9
+ - 🌍 **OAuth Ready**: Built-in support for GitHub and Google OAuth via `arctic`.
10
+ - 🧩 **Database Agnostic**: Use Mongoose, Prisma, Drizzle, or any store via the `AuthAdapter` pattern.
11
+ - 🌶️ **Password Peppering**: Server-side pepper support for enhanced hash protection.
12
+ - 🛡️ **Timing Attack Protection**: Built-in safeguards against side-channel analysis during login.
13
+ - ✅ **Zod Schema Support**: Perfectly preserves and types your user metadata.
14
+ - 🚀 **ESM First**: Native support for NodeNext module resolution.
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ npm install kroxt
20
+ ```
21
+
22
+ ---
23
+
24
+ ## Guide: Full Authentication Flow
25
+
26
+ This guide walks you through setting up Kroxt from scratch in your application.
27
+
28
+ ### Step 1: The Adapter Pattern
29
+
30
+ Kroxt doesn't care which database you use. You just need to implement the `AuthAdapter` interface.
31
+
32
+ In this example, we use a simple user structure: `name`, `email`, and `password`.
33
+ > [!NOTE]
34
+ > Kroxt's adapter can accept **any** additional fields your application requires (e.g., `role`, `avatar`, `preferences`) with no limits.
35
+
36
+ ```typescript
37
+ import type { AuthAdapter, User } from "kroxt/adapter";
38
+
39
+ export const myAdapter: AuthAdapter = {
40
+ createUser: async (data) => {
41
+ // Save to your DB: { name, email, passwordHash, ...anyOtherFields }
42
+ // return the created user including its unique id
43
+ },
44
+ findUserByEmail: async (email) => {
45
+ // Find user by email in your DB
46
+ },
47
+ findUserById: async (id) => {
48
+ // Find user by ID in your DB
49
+ },
50
+ linkOAuthAccount: async (user, provider, providerId) => {
51
+ // Link an OAuth provider to an existing user
52
+ }
53
+ };
54
+ ```
55
+
56
+ ### Step 2: Initialize the Auth Engine
57
+
58
+ Configure Kroxt with your adapter and security settings.
59
+
60
+ ```typescript
61
+ import { createAuth } from "kroxt";
62
+ import { myAdapter } from "./myAdapter.js";
63
+
64
+ export const auth = createAuth({
65
+ adapter: myAdapter,
66
+ secret: process.env.AUTH_SECRET, // High-entropy secret for JWT signing
67
+ pepper: process.env.AUTH_PEPPER, // Optional: Server-side pepper for password hashing
68
+ session: {
69
+ expires: "15m", // Access token duration
70
+ refreshExpires: "7d" // Refresh token duration
71
+ }
72
+ });
73
+ ```
74
+
75
+ ### Step 3: Implement Controllers & Routes
76
+
77
+ Use the engine in your application logic. Examples below use an Express-like structure.
78
+
79
+ #### Registration
80
+ ```typescript
81
+ app.post("/register", async (req, res) => {
82
+ const { name, email, password, ...extraFields } = req.body;
83
+
84
+ // Kroxt handles argon2 hashing (with pepper) and token generation
85
+ const { user, accessToken, refreshToken } = await auth.signup({
86
+ name,
87
+ email,
88
+ ...extraFields
89
+ }, password);
90
+
91
+ res.json({ user, accessToken, refreshToken });
92
+ });
93
+ ```
94
+
95
+ #### Login
96
+ ```typescript
97
+ app.post("/login", async (req, res) => {
98
+ const { email, password } = req.body;
99
+
100
+ // Kroxt verifies password (timing-attack safe) and returns tokens
101
+ const { user, accessToken, refreshToken } = await auth.loginWithPassword(email, password);
102
+
103
+ res.json({ user, accessToken, refreshToken });
104
+ });
105
+ ```
106
+
107
+ #### Token Refresh
108
+ Keep users logged in by rotating access tokens using a valid refresh token.
109
+ ```typescript
110
+ app.post("/refresh", async (req, res) => {
111
+ const { refreshToken } = req.body;
112
+
113
+ // Returns a fresh access token
114
+ const { accessToken } = await auth.refresh(refreshToken);
115
+
116
+ res.json({ accessToken });
117
+ });
118
+ ```
119
+
120
+ #### Protecting Routes (Middleware)
121
+ ```typescript
122
+ app.get("/me", async (req, res) => {
123
+ const token = req.headers.authorization?.split(" ")[1];
124
+
125
+ // Verify the JWT and get the payload { sub: string, role: string, ... }
126
+ const payload = await auth.verifyToken(token, "access");
127
+
128
+ if (!payload) return res.status(401).send("Unauthorized");
129
+
130
+ const user = await myAdapter.findUserById(payload.sub);
131
+ res.json(user);
132
+ });
133
+ ```
134
+
135
+ ---
136
+
137
+ ## Security Best Practices
138
+
139
+ ### 1. Password Peppering
140
+ Always use a `pepper` in production. It's a server-side secret added to passwords before hashing. If your database is leaked, the hashes cannot be cracked without this pepper.
141
+
142
+ ### 2. CSRF Protection
143
+ Kroxt provides helpers for the double-submit cookie pattern. Use these if you are storing tokens in cookies.
144
+
145
+ ```typescript
146
+ import { generateCsrfToken, verifyCsrf } from "kroxt/security";
147
+
148
+ const token = generateCsrfToken();
149
+ const isValid = verifyCsrf(tokenInRequest, tokenInCookie);
150
+ ```
151
+
152
+ ### 3. Secure Cookies
153
+ If using cookies, always set these flags:
154
+ - `httpOnly: true` (Prevents XSS)
155
+ - `secure: true` (Requires HTTPS)
156
+ - `sameSite: 'strict'` (Prevents CSRF)
157
+
158
+ ### 4. Rate Limiting
159
+ Implement rate limiting (e.g., `express-rate-limit`) on `/login` and `/register` to block brute-force attempts.
160
+
161
+ ---
162
+
163
+ ## Reference Project
164
+
165
+ Check out the `kroxt-example` folder or the [GitHub repository](https://github.com/adepoju-oluwatobi/kroxt-example) for a complete **Express + MongoDB** implementation using this library.
166
+
167
+ ## License
168
+
169
+ MIT
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "kroxt",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
+ "license": "MIT",
4
5
  "description": "A framework-agnostic modular auth engine",
5
6
  "type": "module",
6
7
  "main": "./dist-lib/index.js",