* Benchmarks JWT token validation performance
()
| 190 | * Benchmarks JWT token validation performance |
| 191 | */ |
| 192 | function benchmarkJwtAuth () { |
| 193 | console.log("\n📊 JWT Authentication Benchmarks"); |
| 194 | console.log("-".repeat(40)); |
| 195 | |
| 196 | const suite = new Benchmark.Suite(); |
| 197 | |
| 198 | // Mock JWT tokens (in real scenario these would be actual JWTs) |
| 199 | const validJwtToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; |
| 200 | const invalidJwtToken = "invalid.jwt.token"; |
| 201 | |
| 202 | const validJwtReq = createMockRequest({ |
| 203 | headers: { authorization: `Bearer ${validJwtToken}` } |
| 204 | }); |
| 205 | |
| 206 | const invalidJwtReq = createMockRequest({ |
| 207 | headers: { authorization: `Bearer ${invalidJwtToken}` } |
| 208 | }); |
| 209 | |
| 210 | // Mock JWT validation (simplified - in real scenario would use jsonwebtoken library) |
| 211 | const jwtAuthCheck = req => { |
| 212 | const auth = req.headers.authorization; |
| 213 | if (!auth || !auth.startsWith("Bearer ")) return false; |
| 214 | |
| 215 | const token = auth.slice(7); |
| 216 | // Simplified JWT structure check |
| 217 | const parts = token.split("."); |
| 218 | if (parts.length !== 3) return false; |
| 219 | |
| 220 | // Mock validation - in real scenario would verify signature |
| 221 | try { |
| 222 | const payload = JSON.parse(Buffer.from(parts[1], "base64").toString()); |
| 223 | |
| 224 | return payload.sub && payload.iat; |
| 225 | } catch { |
| 226 | return false; |
| 227 | } |
| 228 | }; |
| 229 | |
| 230 | suite |
| 231 | .add("JWT Auth - Valid token", () => { |
| 232 | jwtAuthCheck(validJwtReq); |
| 233 | }) |
| 234 | .add("JWT Auth - Invalid token", () => { |
| 235 | jwtAuthCheck(invalidJwtReq); |
| 236 | }) |
| 237 | .add("JWT Auth - Token parsing", () => { |
| 238 | const token = validJwtToken; |
| 239 | const parts = token.split("."); |
| 240 | if (parts.length === 3) { |
| 241 | JSON.parse(Buffer.from(parts[1], "base64").toString()); |
| 242 | } |
| 243 | }) |
| 244 | .on("cycle", event => { |
| 245 | console.log(` ${String(event.target)}`); |
| 246 | }) |
| 247 | .on("complete", function () { |
| 248 | console.log(` Fastest: ${this.filter("fastest").map("name")}`); |
| 249 | }) |
no test coverage detected