Jwt-auth: The token could not be parsed from the request

Created on 29 Apr 2015  路  6Comments  路  Source: tymondesigns/jwt-auth

Hi,

I am getting this error, when I try to use token on server. I am using Laravel4, and cookies to store token.

Before every request, in App::before filter I have

If(\Cookie::get("Bearer")){
$token = \Cookie::get("Bearer");
header("Authorization: Bearer $token");
};

Cookie with token is returned from server

after authentication like this:

return Response::make(compact('token'))
->withCookie(\Cookie::make("Bearer",$token);

And I see header is set properly on request:

screenshot from 2015-04-29 09 31 00

Most helpful comment

I've had the same issue using the 1.0.0-alpha.3 version with Lumen 5.3.
Fixed it by adding RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] to the .htaccess, so the authorisation header does not get discarded by Lumen/Laravel. Might be useful to add this to the docs.

I created a pull request based on the solution from Jeroen to make it work without the .htaccess rule in case that comes in handy.

All 6 comments

I came accross this too. In my first setup (without the jwt-auth package) I needed to parse the Authorization header and found that Laravel 5 was not allowing the header to be found. If I got the headers using the PHP built-in getallheaders() I could see the Authorization header...

So this is more of a Laravel 5 bug, @tymondesigns are you accepting a PR for this? I am willing to take a look at this.

@carousel Proposition (and fix for me), inside the vendor/tymon/jwt-auth/src/JWTAuth.php file:

protected function parseAuthHeader($headerName = 'authorization', $method = 'bearer')
{
    $header = $this->request->headers->get($headerName);

    if(is_null($header)) {
      $headers = array_change_key_case(getallheaders(), CASE_LOWER);

      if(array_key_exists($headerName, $headers)) {
        $header = $headers[$headerName];
      }
    }

    if (! starts_with(strtolower($header), $method)) {
        return false;
    }

    return trim(str_ireplace($method, '', $header));
}

I only added the if(is_null) check, that should do it without changing the rest of the method.

Any thoughts @tymondesigns ?

I'm going to extract the token parsing logic out of this class, to enable a more comprehensive aproach, without adding bloat

@tymondesigns that is great. I like you package, but found difficult to make it work in Laravel(4,5) because of token parsing.

will be resolved when new release drops - see here

I've had the same issue using the 1.0.0-alpha.3 version with Lumen 5.3.
Fixed it by adding RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] to the .htaccess, so the authorisation header does not get discarded by Lumen/Laravel. Might be useful to add this to the docs.

I created a pull request based on the solution from Jeroen to make it work without the .htaccess rule in case that comes in handy.

Was this page helpful?
0 / 5 - 0 ratings