I want to extend Restler to check if a valid value of custom header Authorization was passed. I am having trouble in getting around the fix, I tried this, but no chance:
class AuthenticateMe implements iAuthenticate() {
function __isAuthenticated() {
//return isset($_SERVER['HTTP_AUTH_KEY']) && $_SERVER['HTTP_AUTH_KEY']==AuthenticateMe::KEY ? TRUE : FALSE;
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
if($header == "Authorization") {
return TRUE;
} else {
//return FALSE;
throw new RestException(404);
}
}
}
}
Header Authentication
there are three ways to do it
You can read more from PHP Manual
Restler 1.0 had a Digest Authentication example. I've modified to make it work with Restler 2.0
Let me quickly fix your custom auth header example
I have tested it to make sure it works!
Here is how to make it work with Authorization header, it works only on apache servers
I figured out that PHP converts
Authorization
header into$_SERVER['PHP_AUTH_DIGEST']
or$_SERVER['PHP_AUTH_USER']
and$_SERVER['PHP_AUTH_PW']
depending on the type of auth request (digest or basic), we can use the following.htaccess
file to enable the$_SERVER['HTTP_AUTHORIZATION']
headerDirectoryIndex index.php
important part is RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]
Now our example can be simplified to: