Skip to content

Instantly share code, notes, and snippets.

@kt3k
Last active May 22, 2021 08:46
Show Gist options
  • Save kt3k/c31af2ae8d7c6bc075296c1462238010 to your computer and use it in GitHub Desktop.
Save kt3k/c31af2ae8d7c6bc075296c1462238010 to your computer and use it in GitHub Desktop.
addEventListener("fetch", (e: FetchEvent) => {
const authorization = e.request.headers.get("authorization");
console.log("authorization", authorization);
if (!authorization) {
e.respondWith(new Response("", {
status: 401,
statusText: "Unauthorized",
headers: {
"www-authenticate": `Basic realm="Access to the metrics"`,
},
}));
return;
}
const m = authorization.match(/^Basic\s+(.*)$/);
if (!m) {
e.respondWith(new Response("Forbidden", {
status: 403,
statusText: "Forbidden",
}));
return;
}
const cred = atob(m[1]);
e.respondWith(new Response(cred), {
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment