Skip to content
Myr

feat: Basic-auth with polyfill (#86)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat: Basic-auth with polyfill Basic auth support Fastly Compute@Edge with polyfills * Try-cache * Add negative tests

authored by Yusuke Wada · committed by GitHub · parent 0297e17 · Browse files · 7 files+83 added−55 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+83 added−55 removed

  • tests:+17 added−13 removed
  • other:+66 added−42 removed

7 files+83 added−55 removed

modified:examples/compute-at-edge/index.js+4 added-5 removed
modified:examples/compute-at-edge/package.json+6 added-4 removed
modified:examples/compute-at-edge/webpack.config.js+9 added-1 removed
modified:src/middleware/basic-auth/basic-auth.ts+1 added-17 removed
modified:src/utils/buffer.test.ts+17 added-13 removed
modified:src/utils/buffer.ts+45 added-14 removed
17 unmodified lines
1819 return true20}2122export const timingSafeEqual = async (a: any, b: any) => {23 const sa = await crypto.subtle.digest(24 {25 name: 'SHA-256',26 },27 new TextEncoder().encode(String(a))28 )22export const decodeBase64 = (str: string) => {23 try {24 const text = atob(str)25 const length = text.length26 const bytes = new Uint8Array(length)27 for (let i = 0; i < length; i++) {28 bytes[i] = text.charCodeAt(i)29 }30 const decoder = new TextDecoder()31 return decoder.decode(bytes)32 } catch {}3330 const sb = await crypto.subtle.digest(31 {32 name: 'SHA-256',33 },34 new TextEncoder().encode(String(b))35 )34 try {35 const { Buffer } = require('buffer')36 return Buffer.from(str, 'base64').toString()37 } catch (e) {38 console.error('If you want to do "decodeBase64", polyfill "buffer" module.')39 throw e40 }41}4237 return equal(sa, sb) && a === b43export const sha256 = async (a: string): Promise<string> => {44 if (crypto && crypto.subtle) {45 const buffer = await crypto.subtle.digest(46 {47 name: 'SHA-256',48 },49 new TextEncoder().encode(String(a))50 )51 const hash = Array.prototype.map.call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2)).join('')52 return hash53 }5455 try {56 const crypto = await import('crypto')57 const hash = crypto.createHash('sha256').update(a).digest('hex')58 return hash59 } catch (e) {60 console.error('If you want to do "sha256", polyfill "crypto" module.')61 throw e62 }63}6465export const timingSafeEqual = async (a: any, b: any) => {66 const sa = await sha256(a)67 const sb = await sha256(b)68 return sa === sb && a === b69}
modified:package.json+1 added-1 removed
1{2 "name": "hono",3 "version": "0.3.2",3 "version": "0.3.3-pre",4 "description": "[炎] Ultrafast web framework for Cloudflare Workers.",5 "main": "dist/index.js",6 "types": "dist/index.d.ts",7 "files": [
95 unmodified lines