You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/AUTHENTICATION.md
+19-13Lines changed: 19 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,13 +44,12 @@ import { BunGateway } from 'bungate'
44
44
const gateway =newBunGateway({
45
45
server: { port: 3000 },
46
46
auth: {
47
+
// HS256 secrets must be at least 32 bytes. Bungate derives the allowed
48
+
// algorithm from the key type when algorithms is omitted.
47
49
secret: process.env.JWT_SECRET,
48
-
jwtOptions: {
49
-
algorithms: ['HS256', 'RS256'],
50
-
issuer: 'https://auth.myapp.com',
51
-
audience: 'https://api.myapp.com',
52
-
},
53
-
// Paths that don't require authentication
50
+
issuer: 'https://auth.myapp.com',
51
+
audience: 'https://api.myapp.com',
52
+
// Paths that don't require authentication (boundary-aware matching)
54
53
excludePaths: [
55
54
'/health',
56
55
'/metrics',
@@ -86,12 +85,13 @@ gateway.addRoute({
86
85
pattern: '/admin/*',
87
86
target: 'http://admin-service:3000',
88
87
auth: {
89
-
secret: process.env.ADMIN_JWT_SECRET,
88
+
// For RS256/ES256, `secret` may be a PEM public key or a CryptoKey.
89
+
secret: process.env.ADMIN_JWT_PUBLIC_KEY!,
90
90
jwtOptions: {
91
91
algorithms: ['RS256'],
92
-
issuer: 'https://auth.myapp.com',
93
-
audience: 'https://admin.myapp.com',
94
92
},
93
+
issuer: 'https://auth.myapp.com',
94
+
audience: 'https://admin.myapp.com',
95
95
optional: false, // Authentication is required
96
96
},
97
97
})
@@ -102,10 +102,8 @@ gateway.addRoute({
102
102
target: 'http://user-service:3000',
103
103
auth: {
104
104
secret: process.env.JWT_SECRET,
105
-
jwtOptions: {
106
-
algorithms: ['HS256'],
107
-
issuer: 'https://auth.myapp.com',
108
-
},
105
+
issuer: 'https://auth.myapp.com',
106
+
audience: 'https://api.myapp.com',
109
107
},
110
108
})
111
109
@@ -117,6 +115,14 @@ gateway.addRoute({
117
115
})
118
116
```
119
117
118
+
### JWT Security Notes
119
+
120
+
-**Tokens must include an `exp` claim.** Bungate rejects tokens without an expiration time.
121
+
-**Pin `issuer` and `audience`** to prevent cross-service token replay, especially when using JWKS.
122
+
-**HS256 secrets must be at least 32 bytes.** Short secrets are rejected at startup.
123
+
-**Algorithm confusion is blocked.** PEM-like secrets cannot be used as HMAC keys; the allowed algorithm family is derived from the key type when `algorithms` is omitted.
124
+
-**`excludePaths` uses boundary-aware matching.**`/public/*` excludes `/public/foo` but not `/publicity/foo`.
125
+
120
126
### Custom Token Extraction
121
127
122
128
By default, JWT tokens are extracted from the `Authorization` header. You can customize this:
0 commit comments