Jwt-auth: JWT payload does not contain the required claims

Created on 10 Jul 2015  ·  17Comments  ·  Source: tymondesigns/jwt-auth

Hey @tymondesigns, just updated and got

TokenInvalidException in PayloadValidator.php line 57:
JWT payload does not contain the required claims

Most helpful comment

to overcome this issue for those who use JWT_TTL=null, remove 'exp' key from your config/jwt.php's 'required_claims' array then you'll be fine! :)

All 17 comments

Which version?

The dev-develop branch - 0.6.@dev

Sent from my iPhone

On Jul 10, 2015, at 9:56 AM, Sean Tymon [email protected] wrote:

Which version?


Reply to this email directly or view it on GitHub.

Hmm.. this hasn't changed. Are you sure that your config matches whats in your token?

I have default config; I get the error coz 'sub' is not provided: I get a token if I remove 'sub' from config, but now it's useless coz there won't be user infor 😐

Sent from my iPhone

On Jul 10, 2015, at 10:53 AM, Sean Tymon [email protected] wrote:

Hmm.. this hasn't changed. Are you sure that your config matches whats in your token?


Reply to this email directly or view it on GitHub.

that's weird.. I will take a look when I'm back at my machine.

could you post your code your using to create the token

Here is my config

'required_claims' => ['iss', 'iat', 'exp', 'nbf', 'sub', 'jti'],

Then my auth code

        $this->validate($request, [
            'email' => 'required', 'password' => 'required',
        ]);
        try {
            if (!$token = JWTAuth::attempt($this->getCredentials($request))) {
                throw new Exception('Invalid credentials');
            }
            return response()->json(['token' => $token]);
        } catch (Exception $e) {
            return response()->json(['error' => $e->getMessage()]);
        }

The user model implements

JWTSubject

With

    /**
     * @return int
     */
    public function getJWTIdentifier () {
        return $this->url;
    }

    /**
     * @return array
     */
    public function getJWTCustomClaims () {
        return [];
    }

This is still an issue. It looks like the custom claims aren't being passed to make function in the PayloadFactory class.

My use case is creating a token using JWTAuth::fromUser. That calls makePayload, which is line 183 in JWTAuth:

    public function makePayload(JWTSubject $user)
    {
        return $this->factory()->make($this->getClaimsArray($user));
    }

However - the make function from PayloadFactory doesn't receive parameters:

    public function make()
    {
        $claims = $this->buildClaims()->resolveClaims();

        return new Payload($claims, $this->validator, $this->refreshFlow);
    }

My suggestion would be to tweak it with:

    public function make($claims = array())
    {
        $claims =  $this->addClaims($claims)->buildClaims()->resolveClaims();

        return new Payload($claims, $this->validator, $this->refreshFlow);
    }

This allows for empty claims and builds all the required claims.

Apologies.. Didn't post back here... This has changed as described in https://github.com/tymondesigns/jwt-auth/pull/177#issuecomment-120417203

I can see - but that's not solving the problem when using JWTAuth::fromUser.

As of the latest commit, using "tymon/jwt-auth": "0.6.*, I still receive this error using JWTAuth::fromUser:

JWT payload does not contain the required claims

This used to work before the claims were reworked.

_Whilst the makePayload function in JWTAuth is passing forward the claims, the make function being called does not receive the claims_.

See line 183 in JWTAuth (https://github.com/tymondesigns/jwt-auth/blob/develop/src/JWTAuth.php#L183):

    public function makePayload(JWTSubject $user)
    {
        return $this->factory()->make($this->getClaimsArray($user));
    }

Whilst the $this->getClaimsArray($user) is returns the claims array and passed it to make, the make function isn't receiving it, because it's currently defined like this:

    public function make()
    {
        $claims = $this->buildClaims()->resolveClaims();

        return new Payload($claims, $this->validator, $this->refreshFlow);
    }

See the latest code:
https://github.com/tymondesigns/jwt-auth/blob/develop/src/PayloadFactory.php#L66

_There are no parameters set in the make function, and the custom claims are not being added to the $claims array._

The change would be to tweak it to be:

   public function make($claims = array())
    {
        $claims =  $this->addClaims($claims)->buildClaims()->resolveClaims();

        return new Payload($claims, $this->validator, $this->refreshFlow);
    }

I'm happy to make a pull request if need be, as it looks like #177 was committed and then reverted.

Ah yea, I see what you mean.. Will fix this shortly, and also I'll need to look at my tests for this, as they were not failing :/

My fix would be slightly different than your suggestion (although that would indeed work) but I'm only on my phone ATM, so can't illustrate that easily right now

Thanks

I had this error after setting the TTL to null

Edit: Aha!

https://github.com/tymondesigns/jwt-auth/issues/340

to overcome this issue for those who use JWT_TTL=null, remove 'exp' key from your config/jwt.php's 'required_claims' array then you'll be fine! :)

@trousers and @eness Really Thankful to you guys. It solved issue

@trousers & @eness You saved my time. It solved the issue after much hours of debugging. Thank God

required_claims

not really, removed the exp key and still receiving error.

In file config/jwt.php from key

'required_claims' => ['iss', 'iat', 'exp', 'nbf', 'jti', 'sub']

I remove unused claims (in my case 'sub')

Ah, thanks for this comment, finally I know where I did wrong

I had this error after setting the TTL to null

Edit: Aha!

340

Was this page helpful?
0 / 5 - 0 ratings