yii2: Missing Authorization Header in request->getHeaders() on MAMP
Case: We are currently building a Yii2 Rest Application, which is access trough an Angular application. We have applied the authToken in a authInterceptor the verify the user who makes the request.
request : function (config) {
config.headers = config.headers || {};
config.headers.Authorization = 'Bearer ' + authToken;
return config;
}
Problem: The “Authorization Header” is not provided when using Yii2 Rest on a MAMP Server. The function getallheaders() in yii2\web\Request->getHeaders() does not return this Header entry. On an ubuntu server (for instance) it works like a charm, but not on every webserver configuration. Is there a workaround? Although, you should probably mention that in your Guide to use the correct Server Configuration (http://www.yiiframework.com/doc-2.0/guide-rest-authentication.html).
regards
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 25 (11 by maintainers)
We have fixed the problem. The problem appears if you’r using CGI/FastCGI mode. As mentioned in the php comments (http://php.net/manual/en/features.http-auth.php#114877) you can put
in the .htaccess file to make Authorizaion variable available.
@gauravparashar12 Try to add:
to your .htaccess file.
If you are using https://github.com/bshaffer/oauth2-server-php You can also take a look at this comment: https://github.com/bshaffer/oauth2-server-php/blob/develop/src/OAuth2/Request.php#L138
@nadar Two thumbs up for the solution. Inside MAMP Pro for those people using it, you need to switch back to Module Mode Under the PHP Tab, by selecting the Mode radio underneath PHP default version labeled as “Identical PHP version for all hosts (module)” if you choose to not edit the .htacess file.
Thank you again for the solution with CGI!
It does return data. But the requested Authorization parameter, which is required in the bearer auth filter, is missing.
$authHeader = $request->getHeaders()->get(‘Authorization’);
from: https://github.com/yiisoft/yii2/blob/master/framework/filters/auth/HttpBearerAuth.php
And as i could see, getHeaders() collects data from the function getallheaders(), i have debuged the output from getallheaders() and could have seen that Authorization parameter is not provided (at least on the mamp server installation).
from: https://github.com/yiisoft/yii2/blob/master/framework/web/Request.php