File size: 1,676 Bytes
c0dd699
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { createMiddleware } from 'hono/factory';
import { Context } from 'hono';
import { HTTPException } from 'hono/http-exception';

import { verifyToken } from './jwt';
import { Bindings, Variables } from '../types';


export const authMiddleware = createMiddleware(async (c: Context, next) => {
	const TOKEN_STRINGS = '[A-Za-z0-9._~+/-]+=*';
	const PREFIX = 'Bearer';
	const HEADER = 'Authorization';

	const headerToken = c.req.header(HEADER);
	if (!headerToken) {
		// No Authorization header
		const res = new Response('Unauthorized', {
			status: 401,
			headers: {
				'WWW-Authenticate': `${PREFIX} realm="` + '"',
			},
		});
		throw new HTTPException(401, { res });
	} else {
		const regexp = new RegExp('^' + PREFIX + ' +(' + TOKEN_STRINGS + ') *$');
		const match = regexp.exec(headerToken);
		if (!match) {
			// Invalid Request
			const res = new Response('Bad Request', {
				status: 400,
				headers: {
					'WWW-Authenticate': `${PREFIX} error="invalid_request"`,
				},
			});
			throw new HTTPException(400, { res });
		} else {
			let equal = await verifyToken(match[1], c);
			if (!equal) {
				// Invalid Token
				const res = new Response('Unauthorized', {
					status: 401,
					headers: {
						'WWW-Authenticate': `${PREFIX} error="invalid_token"`,
					},
				});
				throw new HTTPException(401, { res });
			}
		}
	}
	await next();
});

export const durableObjectMiddleware = createMiddleware(async (c: Context<{ Bindings: Bindings; Variables: Variables }>, next) => {
	const id = c.env.TASK_STATUS_DURABLE_OBJECT.idFromName('a1d-rb-task-status');
	const stub = c.env.TASK_STATUS_DURABLE_OBJECT.get(id);
	c.set('stub', stub);
	await next();
});