I found code online for basic http authentication for visiting certain parts of my site via Cloudflare workers but I’m having issues implementing it.
Here’s the code that I’m using.
The issue is that it works fine on one of my URLs but goes into an authentication loop for all other URLs.
Also noticed that it sends a header different from what I use in the code on the ones in which it fails.
The response headers on the pages where it doesn’t work:
Also, according to the code, the WWW-Authenticate header should have the value Basic realm=“Secure Area” and not Basic realm=“authorize_account”.
Any help is appreciated.
The request headers for that URL:
Response headers on the one URL on which it works:
Request headers for the URL on which it works:
Can you copy/paste the headers into a Peformatted Text Block
</>? Much easier than having to retype from an image. You can redact anything sensitive.
Request headers for the URLs on which it goes into auth loop:
accept-encoding: gzip, deflate, br
authorization: Basic <Redacted>
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41 Safari/537.36
I have not been able to replicate with the code you linked to.
There is a more fully featured implementation on the Cloudflare Developer site.
The primary difference I can see is in the parsing of the authorization header. Might be worth trying the CF version.
You could also look at using Cloudflare Zero Trust (a.k.a. Cloudflare Access) to protect sensitive areas of the website.