Skip to content
Myr

feat: basic auth middleware supports overriding `hashFunction` (#128)

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

authored by Yusuke Wada · committed by GitHub · parent ccf27fb · Browse files · 7 files+78 added−10 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+78 added−10 removed

  • tests:+24 added−0 removed
  • other:+54 added−10 removed

7 files+78 added−10 removed

modified:src/middleware/basic-auth/basic-auth.test.ts+22 added
modified:src/middleware/basic-auth/basic-auth.ts+11 added-4 removed
modified:src/middleware/basic-auth/README.md+23 added-2 removed
1# Basic Auth Middleware23## Usage45index.js:65```js6import { Hono } from 'hono'7import { basicAuth } from 'hono/basic-auth'8
12 unmodified lines
21})2223app.fire()24```2526For Fastly Compute@Edge, polyfill `crypto` or use `crypto-js`.2728Install:2930```31npm i crypto-js32```3334Override `hashFunction`:3536```js37import { SHA256 } from 'crypto-js'3839app.use(40 '/auth/*',41 basicAuth({42 username: 'hono',43 password: 'acoolproject',44 hashFunction: (d: string) => SHA256(d).toString(), // <---45 })46)47```
modified:src/utils/buffer.test.ts+2 added
1import { timingSafeEqual } from './buffer'2import { SHA256 as sha256CryptoJS } from 'crypto-js'34describe('buffer', () => {5 it('positive', async () => {6 expect(
6 unmodified lines
13 expect(await timingSafeEqual('', '')).toBe(true)14 expect(await timingSafeEqual(undefined, undefined)).toBe(true)15 expect(await timingSafeEqual(true, true)).toBe(true)16 expect(await timingSafeEqual(false, false)).toBe(true)17 expect(await timingSafeEqual(true, true, (d: string) => sha256CryptoJS(d).toString()))18 })1920 it('negative', async () => {21 expect(await timingSafeEqual('a', 'b')).toBe(false)
22 unmodified lines
modified:src/utils/buffer.ts+7 added-3 removed
21 unmodified lines
22}2324export const timingSafeEqual = async (25 a: string | object | boolean,26 b: string | object | boolean26 b: string | object | boolean,27 hashFunction?: Function28) => {29 if (!hashFunction) {30 hashFunction = sha25631 }28 const sa = await sha256(a)29 const sb = await sha256(b)32 const sa = await hashFunction(a)33 const sb = await hashFunction(b)34 return sa === sb && a === b35}
modified:package.json+3 added-1 removed
76 unmodified lines
77 "compute@edge"78 ],79 "devDependencies": {80 "@cloudflare/workers-types": "^3.3.0",81 "@types/crypto-js": "^4.1.1",82 "@types/jest": "^27.4.0",83 "@types/mustache": "^4.1.2",84 "@types/node": "^17.0.8",85 "@typescript-eslint/eslint-plugin": "^5.9.0",86 "@typescript-eslint/parser": "^5.9.0",87 "crypto-js": "^4.1.1",88 "eslint": "^7.26.0",89 "eslint-config-prettier": "^8.3.0",90 "eslint-define-config": "^1.2.1",91 "eslint-import-resolver-typescript": "^2.0.0",
12 unmodified lines
104 },105 "engines": {106 "node": ">=11.0.0"107 }106}108}
modified:yarn.lock+10 added
726 unmodified lines
727 integrity sha512-K2waXdXBi2302XUdcHcR1jCeU0LL4TD9HRs/gk0N2Xvrht+G/BfJa4QObBQZfhMdxiCpV3COl5Nfq4uKTeTnJA==728 dependencies:729 "@babel/types" "^7.3.0"730731"@types/crypto-js@^4.1.1":732 version "4.1.1"733 resolved "https://registry.yarnpkg.com/@types/crypto-js/-/crypto-js-4.1.1.tgz#602859584cecc91894eb23a4892f38cfa927890d"734 integrity sha512-BG7fQKZ689HIoc5h+6D2Dgq1fABRa0RbBWKBd9SP/MVRVXROflpm5fhwyATX5duFmbStzyzyycPB8qUYKDH3NA==735736"@types/graceful-fs@^4.1.2":737 version "4.1.5"738 resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.5.tgz#21ffba0d98da4350db64891f92a9e5db3cdb4e15"739 integrity sha512-anKkLmZZ+xm4p8JWBf4hElkM4XR+EZeA2M9BAkkTldmcyDY4mbdIJnRghDJH3Ov5ooY7/UAoENtmdMSkaAd7Cw==
563 unmodified lines
1303 path-key "^3.1.0"1304 shebang-command "^2.0.0"1305 which "^2.0.1"13061307crypto-js@^4.1.1:1308 version "4.1.1"1309 resolved "https://registry.yarnpkg.com/crypto-js/-/crypto-js-4.1.1.tgz#9e485bcf03521041bd85844786b83fb7619736cf"1310 integrity sha512-o2JlM7ydqd3Qk9CA0L4NL6mTzU2sdx96a+oOfPu8Mkl/PK51vSyoi8/rQ8NknZtk44vq15lmhAj9CIAGwgeWKw==13111312cssom@^0.4.4:1313 version "0.4.4"1314 resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.4.4.tgz#5a66cf93d2d0b661d80bf6a44fb65f5c2e4e0a10"1315 integrity sha512-p3pvU7r1MyyqbTk+WbNJIgJjG2VmTIaB10rI93LzVPrmDJKkzKYMtxxyAvQXR/NS6otuzveI7+7BBq3SjBS2mw==
2571 unmodified lines