mbkauthe 5.0.0 → 5.0.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.
@@ -0,0 +1,105 @@
1
+ # Authentication and Sessions
2
+
3
+ [Back to API index](../api.md) | [Back to docs index](../../README.md) | [Back to project README](../../../README.md)
4
+
5
+ ## Authentication
6
+
7
+ MBKAuthe supports two authentication methods:
8
+
9
+ 1. **Session-based Authentication** - Cookie-based sessions for web applications
10
+ 2. **Token-based Authentication** - Persistent API keys for server-to-server communication
11
+
12
+ ### Token-based Authentication
13
+
14
+ For API clients and external services, use a Bearer token in the `Authorization` header.
15
+
16
+ **Header Format:**
17
+ ```
18
+ Authorization: Bearer <your_api_token>
19
+ ```
20
+ *Token format: `mbk_` followed by 64 hexadecimal characters.*
21
+
22
+ **Behavior:**
23
+ - **Stateless:** Validates against the `ApiTokens` table on every request.
24
+ - **Expiration:** Tokens can have an optional expiration date.
25
+ - **Permissions:** API tokens inherit the permissions of the user who created them.
26
+ - **Scopes:** Tokens have a scope (`read-only` or `write`) that controls which HTTP methods are allowed:
27
+ - `read-only`: Only GET, HEAD, and OPTIONS requests (safe, read-only operations)
28
+ - `write`: All HTTP methods (GET, POST, PUT, DELETE, PATCH, etc.)
29
+ - **Usage Tracking:** The system updates the `LastUsed` timestamp on every successful request.
30
+
31
+ **Errors:**
32
+ - `401 Unauthorized` (Code 1005: `INVALID_AUTH_TOKEN`): Token is malformed or not found.
33
+ - `401 Unauthorized` (Code 1006: `API_TOKEN_EXPIRED`): Token exists but has passed its expiration date.
34
+ - `403 Forbidden` (Code 1007: `TOKEN_SCOPE_INSUFFICIENT`): Token scope doesn't allow this HTTP method.
35
+
36
+ **Example Usage:**
37
+
38
+ **1. Backend Implementation (Express):**
39
+
40
+ Even when using API tokens, the `validateSession`/`sessVal` middleware hydrates `req.session.user` for consistency, allowing you to use the same route logic for both browser and API clients.
41
+
42
+ ```javascript
43
+ import { sessVal } from 'mbkauthe';
44
+
45
+ app.get('/api/protected-resource', sessVal, (req, res) => {
46
+ // Access user info populated from the token
47
+ const user = req.session.user; // { id, username, role, ... }
48
+
49
+ res.json({
50
+ message: `Hello ${user.username}`,
51
+ role: user.role
52
+ });
53
+ });
54
+ ```
55
+
56
+ **2. Client Request Examples:**
57
+
58
+ *cURL:*
59
+ ```bash
60
+ curl -X GET https://api.yourdomain.com/api/protected-resource \
61
+ -H "Authorization: Bearer mbk_7f83a92b1dc..."
62
+ ```
63
+
64
+ *JavaScript (Fetch):*
65
+ ```javascript
66
+ const response = await fetch('https://api.yourdomain.com/api/protected-resource', {
67
+ headers: {
68
+ 'Authorization': 'Bearer mbk_7f83a92b1dc...'
69
+ }
70
+ });
71
+ const data = await response.json();
72
+ ```
73
+
74
+ **Output:**
75
+ ```json
76
+ {
77
+ "message": "Hello john.doe",
78
+ "role": "NormalUser"
79
+ }
80
+ ```
81
+
82
+ ---
83
+
84
+ ## Session Management
85
+
86
+ ### Session Cookie
87
+
88
+ When a user logs in, MBKAuthe creates a session and sets the following cookies:
89
+
90
+ | Cookie Name | Description | HttpOnly | Secure | SameSite |
91
+ |------------|-------------|----------|--------|----------|
92
+ | `mbkauthe.sid` | Session identifier | ✓ | Auto* | lax |
93
+ | `sessionId` | Encrypted session token (AES-256-GCM). This cookie is encrypted and treated as an opaque value by clients; do not attempt to parse or rely on the raw cookie contents. Use server endpoints (e.g., `GET /mbkauthe/api/checkSession`, `POST /mbkauthe/api/checkSession` (body) or `POST /mbkauthe/api/verifySession`) to validate or query session information. | ✓ | Auto* | lax |
94
+ | `username` | Username | ✗ | Auto* | lax |
95
+
96
+ \* `secure` flag is automatically set to `true` in production when `IS_DEPLOYED=true`
97
+
98
+ ### Session Lifetime
99
+
100
+ - Default: 2 days (configurable via `COOKIE_EXPIRE_TIME`)
101
+ - Application sessions are stored in the `Sessions` table in PostgreSQL
102
+ - Sessions persist across subdomains in production
103
+
104
+ ---
105
+