Last active
May 22, 2021 08:46
-
-
Save kt3k/c31af2ae8d7c6bc075296c1462238010 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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