softius / php-cross-domain-proxy

PHP Proxy for Cross Domain Requests
GNU General Public License v3.0
269 stars 141 forks source link

Handling resources which need authorization #32

Open simonseyock opened 7 years ago

simonseyock commented 7 years ago

At the moment there is no possibility in the proxy to request resources that need authorization. If you add 'authorization' headers those are meant for access of the proxy.php file and will be filtered by most web servers like apache, iis or nginx. Each them have abilities to turn this option off. But I don't think that is a proper solution because you might want to protect the proxy with authorization, too - Therefore you need two different authorization headers.

I needed a solution for this, so i researched a little bit and encounterd the 'Proxy-Authorization' and 'Proxy-Authenticate' headers which would normally be fitting perfectly for this cause - but this is not meant to be used in scripts running inside the browser. For security reasons. The w3 standard prohibts using any header starting with 'Proxy-'.

So I created a solution in our version of the proxy which uses a custom http header named 'X-Proxy-Forward-Authorization' where the authorization information can be saved which will be used to access the resource.

See https://github.com/KlausBenndorf/guide4you-proxy/pull/6/commits/7a5644e2be73ebc9352f4f5dfa64e50aaac4432a

If you are interested i can provide a pull request.

simonseyock commented 7 years ago

Might be better to name it 'Proxy-Forward-Authorization' as the use of the X- prefix is discouraged nowadays. (https://stackoverflow.com/questions/3561381/custom-http-headers-naming-conventions)

jcubic commented 6 years ago

I've used this code for basic Authentication:

if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
    curl_setopt($ch, CURLOPT_USERPWD, $_SERVER['PHP_AUTH_USER'] . ":" . $_SERVER['PHP_AUTH_PW']);
}

I've also needed to add CORS headers in php because basic auth require Access-Control-Allow-Origin header to be set to requested origin and it can't be asterisk.

you can see my code here proxy.php