Public
Script
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
function extractToken(authorization) {
const parts = authorization.split(" ");
if (parts[0] != "Bearer") {
return null;
}
return parts[1];
}
export function bearerAuth(next: (req?: Request) => Response | Promise<Response>, params: {
verifyToken: (password: string) => boolean | Promise<boolean>;
}) {
return async (req: Request) => {
if (!req.headers.get("authorization")) {
return new Response("Unauthorized", {
status: 401,
headers: {
"WWW-Authenticate": "Basic",
},
});
}
const token = await extractToken(req.headers.get("authorization"));
if (!token) {
return new Response("Unauthorized", {
status: 401,
headers: {
"WWW-Authenticate": "Basic",
},
});
}
if (!await params.verifyToken(token)) {
return new Response("Unauthorized", {
status: 403,
headers: {
"WWW-Authenticate": "Basic",
},
});
}
return next(req);
};
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
June 20, 2024