fix: use `atob` for basic auth (#84)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix: use `atob` for basic auth * Update readme * fallback
authored by Yusuke Wada · committed by GitHub · parent 0fe95bc · Browse files · 4 files·+40 added−28 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+40 added−28 removed
- other:+40 added−28 removed
Provenance
- Commit
4c213e4- Tree
935215a- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
0fe95bc
4 files changed+40 added−28 removed
modified:examples/basic/index.js+3 added-10 removed
20 unmodified lines21222324252526272829303132333428293031324033343536373839404149424344454653 unmodified lines20 unmodified lines// Custom Middleware// Add Custom Headerapp.use('/hello/*', async (c, next) => { await next() await c.res.headers.append('X-message', 'This is addHeader middleware!') c.header('X-message', 'This is addHeader middleware!')})
// Log response timeapp.use('*', async (c, next) => { await next() const responseTime = await c.res.headers.get('X-Response-Time') console.log(`X-Response-Time: ${responseTime}`)})
// Add X-Response-Time headerapp.use('*', async (c, next) => { const start = Date.now() await next() const ms = Date.now() - start await c.res.headers.append('X-Response-Time', `${ms}ms`) await c.header('X-Response-Time', `${ms}ms`)})
// Handle errorapp.use('*', async (c, next) => { try { await next() } catch (err) { console.error(`${err}`) c.res = new Response('Custom Error Message', { status: 500 }) c.res = c.text('Custom Error Message', 500) }})
// Routing53 unmodified linesmodified:examples/basic/package.json+5 added-2 removed
3 unmodified lines45678989101112131415161718193 unmodified lines "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "dev": "wrangler dev", "publish": "wrangler publish" "dev": "wrangler dev index.js", "publish": "wrangler publish index.js" }, "author": "Yusuke Wada <yusuke@kamawada.com> (https://github.com/yusukebe)", "license": "MIT", "dependencies": { "hono": "*" }, "devDependencies": { "wrangler": "^0.0.15" }}No newline at end of fileNo newline at end of filemodified:src/middleware/basic-auth/basic-auth.ts+6 added-16 removed
29 unmodified lines3031323334353435363738373940414243414243444546474849505152534445464717 unmodified lines29 unmodified lines
return { username: userPass[1], password: userPass[2] }}
let b: BufferConstructor
function decodeBase64(str: string) { if (atob && btoa) { return atob(str) } else { const { Buffer } = require('buffer') return b.from(str, 'base64').toString() return Buffer.from(str, 'base64').toString() }}
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' }
17 unmodified linesadded:src/middleware/basic-auth/README.md+26 added
1+# Basic Auth Middleware2+3+## Usage4+5+index.js:6+7+```js8+import { Hono } from 'hono'9+import { basicAuth } from 'hono/basic-auth'10+11+const app = new Hono()12+13+app.use(14+ '/auth/*',15+ basicAuth({16+ username: 'hono',17+ password: 'acoolproject',18+ })19+)20+21+app.get('/auth/page', (c) => {22+ return c.text('You are authorized')23+})24+25+app.fire()26+```