Using Apache as a BasicAuth proxy to an internal resource
- 0 comments
- tagged with apache, proxy, basic auth, security, mocra
This article isn’t named ideally so if you have a better title, tell me.
This is the scenario: we have a simple HTTP resource on the internal network at Mocra. This resource is a closed source tool which is great except that it has one drawback — there are only two security modes: basic authentication on, or basic authentication off. There is only one username/password and you can’t control when or by whom it is required.
Now, my preferred behaviour here is to be able to give out multiple login/passwords and also to only require authentication outside of our local network. I set out to get this working.
I devised this solution a few months ago (which is why it’s using Apache at all) but I thought it might be of some use to someone. I created an Apache VirtualHost to act as a proxy between this resource and its clients. It looks like this:
<VirtualHost *:80># This virtual host proxies requests for the internal resourceServerName external.resource.com# Set auth header to base64 encoded 'username:password'# where username and password are the credentials for internal resourceRequestHeader set Authorization "Basic dXNlcm5hbWU6cGFzc3dvcmQ="ProxyPass /resource_path/ http://192.168.1.123:1337/resource_path/ProxyPassReverse /resource_path/ http://192.168.1.123:1337/resource_path/<Location />Order allow,denyAllow from 192.168.1.0/24 # our local subnetAllow from 127.0.0.1 # duhAllow from 1.3.3.7 # our external router IPAuthType basicAuthName "Our Internal Resource"# where all the external login/passwords areAuthUserFile /var/www/.htpasswdRequire valid-user# this is the secret sauce that allows EITHER# local access or Basic Auth accessSatisfy Any</Location>RewriteEngine onRewriteRule ^/?$ http://external.resource.com:80/resource_path/ [R]</VirtualHost>