Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2.3 getRequest()->headers not showing Authorization Bearer Token

I am passing an Authorization: Bearer { Token } as a HTTP request to my Symfony Rest Controller.

My Request:

GET /app_dev.php/api/members HTTP/1.1
Host: localhost
Authorization: Bearer 123456789
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded

Inside My controller:

$this->getRequest()->headers;

For some reason when I use Symfony's Request method the Authorization header isn't available to my controller. When I use PHP's getallheaders() the Authorization header shows up as expected. Any Ideas on why Symfony isn't seeing it?

Thanks

like image 705
Pathsofdesign Avatar asked Oct 18 '13 07:10

Pathsofdesign


People also ask

How do I get the authorization token for a request?

You can say $request->headers->get ('Authorization'). That’s going to get you the actual raw token ABCD123 type of thing:

How do I get the Authorization header from a request object?

The job of the listener is to look at the request object and get the token information off of it. And hey, since we’re sending the token on the Authorization header, we are going to look for it there. So let’s get rid of this hard coded text and instead go get that Authorization header. You can say $request->headers->get ('Authorization').

How to find which user object an API Token is attached to?

The guts for getting this all working can be complicated, but the end result is so simple: send an Authorization header with the api token and use that to look in your database and figure out which User object if any this token is attached to.

How do you know if a token is valid?

So you can imagine a big table full of tokens and each token is related to exactly one user. For example, if we look up the entry in the token table, we can figure out “yes” this is a valid token and it is a valid token for a user whose id is 5.


1 Answers

It is most likely stripped by Apache. Bearer is not a known scheme, it is sort of proprietary.

Therefore, either you use a custom header, like X-Bearer-Token: 123456789 or you can try to add this rewrite condition in your .htaccess

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
like image 165
Boris Guéry Avatar answered Oct 01 '22 01:10

Boris Guéry