fix: basic auth (#83)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Installing `buffer` and `crypto` packages enables basic auth, if `buffer` of `crypto` is not in pollyfills such as webpack 5 environment. Close #82
authored by Yusuke Wada · committed by GitHub · parent 5587878 · Browse files · 4 files·+30 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+30 added−16 removed
- other:+30 added−16 removed
Provenance
- Commit
0fe95bc- Tree
4b37221- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
5587878
4 files changed+30 added−16 removed
modified:src/middleware/basic-auth/basic-auth.ts+21 added-12 removed
123454567821 unmodified lines303132333435363637383939404142434041424344454647484950515253545556575859505160616263646566575867686970713 unmodified linesimport type { Context } from '../../context'import { timingSafeEqual } from '../../utils/buffer'
const CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/const CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/const USER_PASS_REGEXP = /^([^:]*):(.*)$/
const auth = (req: Request) => { if (!req) {21 unmodified lines
return { username: userPass[1], password: userPass[2] }}
let b: BufferConstructor
function decodeBase64(str: string) { return Buffer.from(str, 'base64').toString() return b.from(str, 'base64').toString()}
export const basicAuth = (options: { username: string; password: string; realm?: string;}) => {export const basicAuth = (options: { username: string; password: string; realm?: string }) => { try { b = Buffer } catch {}
if (b === undefined) { try { const { Buffer } = require('buffer') b = Buffer } catch (e) { throw new Error('If you want to use Basic Auth Middleware, install "buffer" package.') } }
if (!options.realm) { options.realm = 'Secure Area' }
return async (ctx: Context, next: Function) => { const user = auth(ctx.req) const usernameEqual = user && await timingSafeEqual(options.username, user.username) const passwordEqual = user && await timingSafeEqual(options.password, user.password) const usernameEqual = user && (await timingSafeEqual(options.username, user.username)) const passwordEqual = user && (await timingSafeEqual(options.password, user.password))
if (!user || !usernameEqual || !passwordEqual) { ctx.res = new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': 'Basic realm="' + options.realm.replace(/"/g, '\\"') + '"', 'WWW-Authenticate': 'Basic realm="' + options.realm.replace(/"/g, '\\"') + '"', }, }) return }3 unmodified linesmodified:src/middleware/mustache/mustache.ts+1 added-1 removed
9 unmodified lines1011121314141516171831 unmodified lines9 unmodified lines let Mustache: any try { Mustache = require('mustache') } catch { throw new Error('If you want to use Mustache Middleware, install mustache package first.') throw new Error('If you want to use Mustache Middleware, install "mustache" package first.') }
c.render = async (filename, view = {}, options?) => { const buffer = await getContentFromKVAsset(`${filename}${EXTENSION}`)31 unmodified linesmodified:src/middleware/mustache/README.md+3 added-2 removed
123455678911 unmodified lines212223242526272828293031323334 unmodified lines# Mustache Middleware
Mustache Middleware is available only on Cloudflare Workers.
## Dependencies## Requirements
This middleware depends on [mustache.js](https://www.npmjs.com/package/mustache).
```plain11 unmodified linesindex.js:
```jsimport { Hono, Middleware } from 'hono'import { mustache } from 'hono/mustache'
const app = new Hono()
app.use('*', Middleware.mustache())app.use('*', mustache())
app.get('/', (c) => { return c.render( 'index',34 unmodified linesmodified:src/compose.ts+5 added-1 removed
1 // Based on the code in the MIT licensed `koa-compose` package.2 export const compose = (middleware: any) => {3+ const errors: Error[] = []4 return function (context: any, next?: Function) {5 let index = -16 return dispatch(0)7 function dispatch(i: number): any {2 unmodified lines10 let fn = middleware[i]11 if (i === middleware.length) fn = next12 if (!fn) return Promise.resolve()13 try {13- return Promise.resolve(fn(context, dispatch.bind(null, i + 1)))14+ return Promise.resolve(fn(context, dispatch.bind(null, i + 1))).catch((e) => {15+ errors.push(e)16+ throw errors[0] // XXX17+ })18 } catch (err) {19 return Promise.reject(err)20 }21 }2 unmodified lines