diff options
| author | Divy Srivastava <dj.srivastava23@gmail.com> | 2022-08-19 15:54:54 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-19 15:54:54 +0530 |
| commit | 25a109d9ea27ad3a76fdce14bba283e953af9bce (patch) | |
| tree | 68f0280065c9df4be8fa325ba82693879b4b46cd /cli/bench/testdata/npm/hono/dist/middleware/basic-auth | |
| parent | 9e576dff7c39cfd510c60ba92aa0d1c15fd24a6b (diff) | |
chore(bench): add flash router benchmarks (#15495)
Diffstat (limited to 'cli/bench/testdata/npm/hono/dist/middleware/basic-auth')
| -rw-r--r-- | cli/bench/testdata/npm/hono/dist/middleware/basic-auth/index.d.ts | 11 | ||||
| -rw-r--r-- | cli/bench/testdata/npm/hono/dist/middleware/basic-auth/index.js | 48 |
2 files changed, 59 insertions, 0 deletions
diff --git a/cli/bench/testdata/npm/hono/dist/middleware/basic-auth/index.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/basic-auth/index.d.ts new file mode 100644 index 000000000..072df608f --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/basic-auth/index.d.ts @@ -0,0 +1,11 @@ +import type { Context } from '../../context'; +import type { Next } from '../../hono'; +export declare const basicAuth: (options: { + username: string; + password: string; + realm?: string; + hashFunction?: Function; +}, ...users: { + username: string; + password: string; +}[]) => (ctx: Context, next: Next) => Promise<void>; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/basic-auth/index.js b/cli/bench/testdata/npm/hono/dist/middleware/basic-auth/index.js new file mode 100644 index 000000000..c58244a3c --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/basic-auth/index.js @@ -0,0 +1,48 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.basicAuth = void 0; +const buffer_1 = require("../../utils/buffer"); +const encode_1 = require("../../utils/encode"); +const CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/; +const USER_PASS_REGEXP = /^([^:]*):(.*)$/; +const auth = (req) => { + const match = CREDENTIALS_REGEXP.exec(req.headers.get('Authorization') || ''); + if (!match) { + return undefined; + } + const userPass = USER_PASS_REGEXP.exec((0, encode_1.decodeBase64)(match[1])); + if (!userPass) { + return undefined; + } + return { username: userPass[1], password: userPass[2] }; +}; +const basicAuth = (options, ...users) => { + if (!options) { + throw new Error('basic auth middleware requires options for "username and password"'); + } + if (!options.realm) { + options.realm = 'Secure Area'; + } + users.unshift({ username: options.username, password: options.password }); + return async (ctx, next) => { + const requestUser = auth(ctx.req); + if (requestUser) { + for (const user of users) { + const usernameEqual = await (0, buffer_1.timingSafeEqual)(user.username, requestUser.username, options.hashFunction); + const passwordEqual = await (0, buffer_1.timingSafeEqual)(user.password, requestUser.password, options.hashFunction); + if (usernameEqual && passwordEqual) { + // Authorized OK + await next(); + return; + } + } + } + ctx.res = new Response('Unauthorized', { + status: 401, + headers: { + 'WWW-Authenticate': 'Basic realm="' + options.realm?.replace(/"/g, '\\"') + '"', + }, + }); + }; +}; +exports.basicAuth = basicAuth; |
