@xivdyetools/auth 1.0.0 → 1.0.1
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/README.md +158 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# @xivdyetools/auth
|
|
2
|
+
|
|
3
|
+
Shared authentication utilities for the xivdyetools ecosystem. Provides secure JWT verification, HMAC signing, timing-safe comparison, and Discord signature verification.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @xivdyetools/auth
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **JWT Verification** - HMAC-SHA256 JWT verification with algorithm validation
|
|
14
|
+
- **HMAC Signing** - Create and verify HMAC-SHA256 signatures
|
|
15
|
+
- **Timing-Safe Comparison** - Constant-time string comparison to prevent timing attacks
|
|
16
|
+
- **Discord Verification** - Ed25519 signature verification for Discord interactions
|
|
17
|
+
- **Tree-Shakeable** - Subpath exports for minimal bundle size
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### JWT Verification
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { verifyJWT, decodeJWT, isJWTExpired } from '@xivdyetools/auth';
|
|
25
|
+
|
|
26
|
+
// Verify JWT with signature and expiration checking
|
|
27
|
+
const payload = await verifyJWT(token, process.env.JWT_SECRET);
|
|
28
|
+
if (!payload) {
|
|
29
|
+
// Invalid signature, expired, or wrong algorithm
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Decode without verification (debugging only)
|
|
33
|
+
const decoded = decodeJWT(token);
|
|
34
|
+
|
|
35
|
+
// Check if JWT is expired
|
|
36
|
+
if (isJWTExpired(payload)) {
|
|
37
|
+
// Token has expired
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### HMAC Signing
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { hmacSign, hmacVerify, verifyBotSignature } from '@xivdyetools/auth';
|
|
45
|
+
|
|
46
|
+
// Sign data with HMAC-SHA256 (base64url output)
|
|
47
|
+
const signature = await hmacSign(data, secret);
|
|
48
|
+
|
|
49
|
+
// Verify signature
|
|
50
|
+
const isValid = await hmacVerify(data, signature, secret);
|
|
51
|
+
|
|
52
|
+
// Verify bot request signature (with timestamp validation)
|
|
53
|
+
const isValidBot = await verifyBotSignature(
|
|
54
|
+
signature, // X-Request-Signature header
|
|
55
|
+
timestamp, // X-Request-Timestamp header
|
|
56
|
+
userDiscordId,
|
|
57
|
+
userName,
|
|
58
|
+
secret,
|
|
59
|
+
{ maxAgeMs: 5 * 60 * 1000 } // Optional: 5 minute max age
|
|
60
|
+
);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Timing-Safe Comparison
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { timingSafeEqual } from '@xivdyetools/auth';
|
|
67
|
+
|
|
68
|
+
// Constant-time string comparison (prevents timing attacks)
|
|
69
|
+
const isEqual = await timingSafeEqual(userInput, expectedValue);
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Discord Signature Verification
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { verifyDiscordRequest } from '@xivdyetools/auth';
|
|
76
|
+
|
|
77
|
+
// Verify Discord interaction signature
|
|
78
|
+
const result = await verifyDiscordRequest(request, env.DISCORD_PUBLIC_KEY);
|
|
79
|
+
|
|
80
|
+
if (!result.valid) {
|
|
81
|
+
return new Response('Unauthorized', { status: 401 });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// result.body contains the parsed interaction
|
|
85
|
+
const interaction = result.body;
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Subpath Exports
|
|
89
|
+
|
|
90
|
+
Import only what you need for optimal tree-shaking:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
// JWT utilities only
|
|
94
|
+
import { verifyJWT, decodeJWT } from '@xivdyetools/auth/jwt';
|
|
95
|
+
|
|
96
|
+
// HMAC utilities only
|
|
97
|
+
import { hmacSign, hmacVerify } from '@xivdyetools/auth/hmac';
|
|
98
|
+
|
|
99
|
+
// Timing utilities only
|
|
100
|
+
import { timingSafeEqual } from '@xivdyetools/auth/timing';
|
|
101
|
+
|
|
102
|
+
// Discord utilities only
|
|
103
|
+
import { verifyDiscordRequest } from '@xivdyetools/auth/discord';
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## API Reference
|
|
107
|
+
|
|
108
|
+
### JWT (`@xivdyetools/auth/jwt`)
|
|
109
|
+
|
|
110
|
+
| Function | Description |
|
|
111
|
+
|----------|-------------|
|
|
112
|
+
| `verifyJWT(token, secret)` | Verify JWT signature, algorithm (HS256 only), and expiration |
|
|
113
|
+
| `verifyJWTSignatureOnly(token, secret, maxAgeMs?)` | Verify signature only (for refresh token grace periods) |
|
|
114
|
+
| `decodeJWT(token)` | Decode JWT without verification (debugging only) |
|
|
115
|
+
| `isJWTExpired(payload)` | Check if JWT payload is expired |
|
|
116
|
+
| `getJWTTimeToExpiry(payload)` | Get milliseconds until JWT expires |
|
|
117
|
+
|
|
118
|
+
### HMAC (`@xivdyetools/auth/hmac`)
|
|
119
|
+
|
|
120
|
+
| Function | Description |
|
|
121
|
+
|----------|-------------|
|
|
122
|
+
| `createHmacKey(secret, usage)` | Create CryptoKey for HMAC operations |
|
|
123
|
+
| `hmacSign(data, secret)` | Sign data, return base64url signature |
|
|
124
|
+
| `hmacSignHex(data, secret)` | Sign data, return hex signature |
|
|
125
|
+
| `hmacVerify(data, signature, secret)` | Verify base64url signature |
|
|
126
|
+
| `hmacVerifyHex(data, signature, secret)` | Verify hex signature |
|
|
127
|
+
| `verifyBotSignature(sig, ts, userId, userName, secret, opts?)` | Verify bot request signature |
|
|
128
|
+
|
|
129
|
+
### Timing (`@xivdyetools/auth/timing`)
|
|
130
|
+
|
|
131
|
+
| Function | Description |
|
|
132
|
+
|----------|-------------|
|
|
133
|
+
| `timingSafeEqual(a, b)` | Constant-time string comparison |
|
|
134
|
+
| `timingSafeEqualBytes(a, b)` | Constant-time Uint8Array comparison |
|
|
135
|
+
|
|
136
|
+
### Discord (`@xivdyetools/auth/discord`)
|
|
137
|
+
|
|
138
|
+
| Function | Description |
|
|
139
|
+
|----------|-------------|
|
|
140
|
+
| `verifyDiscordRequest(request, publicKey, opts?)` | Verify Discord Ed25519 signature |
|
|
141
|
+
| `unauthorizedResponse()` | Return 401 response |
|
|
142
|
+
| `badRequestResponse(message?)` | Return 400 response |
|
|
143
|
+
|
|
144
|
+
## Security Features
|
|
145
|
+
|
|
146
|
+
- **Algorithm Validation**: JWT verification only accepts HS256, preventing algorithm confusion attacks
|
|
147
|
+
- **Timing-Safe Comparison**: Uses `crypto.subtle.timingSafeEqual()` with XOR fallback
|
|
148
|
+
- **Timestamp Validation**: Bot signatures include clock skew tolerance and max age checks
|
|
149
|
+
- **Body Size Limits**: Discord verification enforces 100KB max body size by default
|
|
150
|
+
|
|
151
|
+
## Dependencies
|
|
152
|
+
|
|
153
|
+
- `@xivdyetools/crypto` - Base64URL and hex encoding utilities
|
|
154
|
+
- `discord-interactions` - Discord Ed25519 signature verification
|
|
155
|
+
|
|
156
|
+
## License
|
|
157
|
+
|
|
158
|
+
MIT
|