hono 1.4.4 → 1.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/hono.js CHANGED
@@ -102,6 +102,9 @@ class Hono extends defineDynamicClass() {
102
102
  let context;
103
103
  try {
104
104
  context = await composed(c);
105
+ if (!context.finalized) {
106
+ throw new Error('Context is not finalized. You may forget returning Response object or `await next()`');
107
+ }
105
108
  }
106
109
  catch (err) {
107
110
  if (err instanceof Error) {
@@ -0,0 +1,8 @@
1
+ import type { Context } from '../../context';
2
+ import type { Next } from '../../hono';
3
+ export declare const bearerAuth: (options: {
4
+ token: string;
5
+ realm?: string;
6
+ prefix?: string;
7
+ hashFunction?: Function;
8
+ }) => (c: Context, next: Next) => Promise<void>;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bearerAuth = void 0;
4
+ const buffer_1 = require("../../utils/buffer");
5
+ const TOKEN_STRINGS = '[A-Za-z0-9._~+/-]+=*';
6
+ const PREFIX = 'Bearer';
7
+ const bearerAuth = (options) => {
8
+ if (!options.token) {
9
+ throw new Error('bearer auth middleware requires options for "token"');
10
+ }
11
+ if (!options.realm) {
12
+ options.realm = '';
13
+ }
14
+ if (!options.prefix) {
15
+ options.prefix = PREFIX;
16
+ }
17
+ const realm = options.realm?.replace(/"/g, '\\"');
18
+ return async (c, next) => {
19
+ const headerToken = c.req.headers.get('Authorization');
20
+ if (!headerToken) {
21
+ // No Authorization header
22
+ c.res = new Response('Unauthorized', {
23
+ status: 401,
24
+ headers: {
25
+ 'WWW-Authenticate': `${options.prefix} realm="` + realm + '"',
26
+ },
27
+ });
28
+ }
29
+ else {
30
+ const regexp = new RegExp('^' + options.prefix + ' +(' + TOKEN_STRINGS + ') *$');
31
+ const match = regexp.exec(headerToken);
32
+ if (!match) {
33
+ // Invalid Request
34
+ c.res = new Response('Bad Request', {
35
+ status: 400,
36
+ headers: {
37
+ 'WWW-Authenticate': `${options.prefix} error="invalid_request"`,
38
+ },
39
+ });
40
+ }
41
+ else {
42
+ const equal = await (0, buffer_1.timingSafeEqual)(options.token, match[1], options.hashFunction);
43
+ if (!equal) {
44
+ // Invalid Token
45
+ c.res = new Response('Unauthorized', {
46
+ status: 401,
47
+ headers: {
48
+ 'WWW-Authenticate': `${options.prefix} error="invalid_token"`,
49
+ },
50
+ });
51
+ }
52
+ else {
53
+ // Authorize OK
54
+ await next();
55
+ return;
56
+ }
57
+ }
58
+ }
59
+ };
60
+ };
61
+ exports.bearerAuth = bearerAuth;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "1.4.4",
3
+ "version": "1.4.5",
4
4
  "description": "Ultrafast web framework for Cloudflare Workers.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -18,6 +18,7 @@
18
18
  "exports": {
19
19
  ".": "./dist/index.js",
20
20
  "./basic-auth": "./dist/middleware/basic-auth/index.js",
21
+ "./bearer-auth": "./dist/middleware/bearer-auth/index.js",
21
22
  "./body-parse": "./dist/middleware/body-parse/index.js",
22
23
  "./cookie": "./dist/middleware/cookie/index.js",
23
24
  "./cors": "./dist/middleware/cors/index.js",
@@ -41,6 +42,9 @@
41
42
  "basic-auth": [
42
43
  "./dist/middleware/basic-auth"
43
44
  ],
45
+ "bearer-auth": [
46
+ "./dist/middleware/bearer-auth"
47
+ ],
44
48
  "body-parse": [
45
49
  "./dist/middleware/body-parse"
46
50
  ],