* Benchmarks bearer token authentication performance
()
| 133 | * Benchmarks bearer token authentication performance |
| 134 | */ |
| 135 | function benchmarkBearerAuth () { |
| 136 | console.log("\n📊 Bearer Token Authentication Benchmarks"); |
| 137 | console.log("-".repeat(40)); |
| 138 | |
| 139 | const server = createAuthServer({ // eslint-disable-line no-unused-vars |
| 140 | bearer: { |
| 141 | enabled: true, |
| 142 | tokens: ["token123", "token456", "secret789", "admin-token"] |
| 143 | } |
| 144 | }); |
| 145 | |
| 146 | const suite = new Benchmark.Suite(); |
| 147 | |
| 148 | // Create test requests with different token scenarios |
| 149 | const validTokenReq = createMockRequest({ |
| 150 | headers: { authorization: "Bearer token123" } |
| 151 | }); |
| 152 | |
| 153 | const invalidTokenReq = createMockRequest({ |
| 154 | headers: { authorization: "Bearer invalid-token" } |
| 155 | }); |
| 156 | |
| 157 | const noTokenReq = createMockRequest(); |
| 158 | |
| 159 | // Mock bearer token validation |
| 160 | const bearerAuthCheck = req => { |
| 161 | const auth = req.headers.authorization; |
| 162 | if (!auth || !auth.startsWith("Bearer ")) return false; |
| 163 | |
| 164 | const token = auth.slice(7); |
| 165 | const validTokens = ["token123", "token456", "secret789", "admin-token"]; |
| 166 | |
| 167 | return validTokens.includes(token); |
| 168 | }; |
| 169 | |
| 170 | suite |
| 171 | .add("Bearer Auth - Valid token", () => { |
| 172 | bearerAuthCheck(validTokenReq); |
| 173 | }) |
| 174 | .add("Bearer Auth - Invalid token", () => { |
| 175 | bearerAuthCheck(invalidTokenReq); |
| 176 | }) |
| 177 | .add("Bearer Auth - No token", () => { |
| 178 | bearerAuthCheck(noTokenReq); |
| 179 | }) |
| 180 | .on("cycle", event => { |
| 181 | console.log(` ${String(event.target)}`); |
| 182 | }) |
| 183 | .on("complete", function () { |
| 184 | console.log(` Fastest: ${this.filter("fastest").map("name")}`); |
| 185 | }) |
| 186 | .run(); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Benchmarks JWT token validation performance |
no test coverage detected