0

I've written this simple filter below:

Route::filter('token', function() 
{
    $headers = json_decode(json_encode(getallheaders()), true);

    if(array_key_exists($headers['Authorization'], $headers)) {
        echo 'test';
    }
});

It uses the getallheaders() function instead of Laravel's Request class because the Request class does not yet recognize custom HTTP headers.

When printing this array, it returns:

Array
(
    [Host] => api.myapi.com
    [Connection] => keep-alive
    [Accept] => application/json, text/plain, */*
    [Origin] => http://myorigin
    [User-Agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
    [Authorization] => eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9hcGkuYmFjazlpbnMuY29tIiwiYXVkIjoiaHR0cDpcL1wvMTYyLjI0My4xMDEuMjAyXC9zbWFydDIiLCJpYXQiOjE0MTQ2ODE1NzgsIm5iZiI6MTQxNDY4MzM3OCwidXNlciI6ImI4YTAwNzIxLTQwZjEtNzgwMS1iNGI5LTUwY2UxNTJjZTJlYyJ9.VrcWRwupzuS_Y5PNiNfXCAMl3bVbifFIHptMO6
    [Referer] => http://myorigin/myproject/
    [Accept-Encoding] => gzip,deflate,sdch
    [Accept-Language] => en-US,en;q=0.8
)

However, array_key_exists($headers['Authorization'], $headers) continually evaluates to false. In this circumstance why would it evaluate to false? I am sending the Authorization token in the headers and when I print_r() the array it's clearly recognizing the header and giivng the correct value upon request.

What could be the problem with this?

2 Answers2

0

So I solved my own problem. A different way, but I achieved what I was trying to (check to be sure the Authorization header was sent).

I ran a print_r() on array_keys($headers) and it returned an array of all of the headers.

I then wrote:

echo in_array('Authorization', array_keys($headers));

And it returned true because it is true.

If anyone has a better solution or the answer to why array_key_exists() is not working, I'd love to hear why.

0

Your original example fails because you used array_key_exists($headers['Authorization'], $headers) when you should have used array_key_exists('Authorization', $headers).

But more importantly, this isn't the best solution for the problem. It's rather difficult to test and you're easily provided better methods for solving this problem, mainly the supplied Request object.

Route::filter('token', function ($route, \Illuminate\Http\Request $request) {
    if ($request->headers('Authorization')) {
        echo "Token is " . {$request->headers('Authorization')};
    }
});

This easily provides the ability to test that your filter works as expected. The code is also easier to read.

Logan Bailey
  • 7,039
  • 7
  • 36
  • 44
  • Ah.. No duh. Dumb mistake. I've tried to use the Request class, but it doesn't recognize custom HTTP headers like I said. –  Oct 30 '14 at 16:02
  • You can fix the Authorization error in your .htaccess see http://stackoverflow.com/questions/4947548/what-does-this-this-http-authorization-rewriterule-do for more information about this issue. – Logan Bailey Oct 30 '14 at 16:24