Basic Authentication from FastCGI
Posted: Wed Feb 05, 2020 8:35 pm
So today I was doing a bit of custom code for a project and needed a way to authenticate less sensitive parts of a reverse proxy using only nginx and PB. I found the subrequest stuff quite interesting and got a bit curious if I could trigger the HTTP Basic Authentication features in the browser directly from my fastcgi-project.
A little Wikipedia later, and some (less than clever) googling on top of that, I found how easy this was:Then the nightmare started. I can't figure out how to read the input from the client/visitor. All documentation says that the "Client sends a request header containing 'Authorization: Basic XXXXXX', where XXXXXX is username:password in base64.", but I can't figure out how (or where) to read the "Authorization"-value from. Anyone? Feels like I'm halfway there, but still not.
And before anyone starts a discussion involving the method: Yes, I know that old classic basic authentication it not recommended nowadays. I still want to know how to do it.
A little Wikipedia later, and some (less than clever) googling on top of that, I found how easy this was:
Code: Select all
If Not InitCGI() Or Not ReadCGI()
End
EndIf
If Not InitFastCGI(5600) ; Create the FastCGI program on port 5600
End
EndIf
While WaitFastCGIRequest()
If ReadCGI()
WriteCGIHeader("Status", "401")
WriteCGIHeader("WWW-Authenticate", "Basic realm=" + Chr(34) + "My Realm" + Chr(34), #PB_CGI_LastHeader)
Endif
Wend
And before anyone starts a discussion involving the method: Yes, I know that old classic basic authentication it not recommended nowadays. I still want to know how to do it.