Jwt-auth: Trying to generate a access token but getting error JWT payload does not contain the required claims

Created on 21 Dec 2017  路  5Comments  路  Source: tymondesigns/jwt-auth

Hello guys,

I'm trying to create a access token using your JWT library but the problem is it keeps on throwing me the above error mentioned in the subject.

I was able to create the access token with this online tool : https://jwt.io/

in the payload data we need to give this

{
"iss": "",
"exp": 1496091964000
}

and in secret I gave the secret

but when I use your library to create a payload which I want to encode using my secret it throws me this error

JWT payload does not contain the required claims

$customClaims = [
'alg' => 'HS256',
'typ' => 'JWT',
'iss' => '',
'exp' => '1496091964000'
];
$payload = JWTFactory::make($customClaims);

Most helpful comment

We resolved this issue by changing the method called to build the custom token, previously before RC1 we made our custom token as such:

````php
$factory = JWTFactory::addClaims([
'sub' => env('API_ID'),
]);

$payload = $factory->make();

$token = JWTAuth::encode($payload);

return ['HTTP_Authorization' => "Bearer {$token}"];
````

When upgrading we received the error that @Gardezi1 was seeing:

php "payload does not contain the required claims"

Solution

the resolution was simply changing the factory calls to build the token as such

````php
$factory = JWTFactory::customClaims([
'sub' => env('API_ID'),
]);

    $payload = $factory->make();

    $token = JWTAuth::encode($payload);

    return ['HTTP_Authorization' => "Bearer {$token}"];

``` ChangeaddClaimstocustomClaims`.

For interest

We create custom "service-account" tokens to authenticate HTTP requests between services in our micro-service app that is built of around 6 laravel apps. API_ID is simply a secret shared amongst the services.

All 5 comments

Have you removed the other claims from the required claims in the configuration file?

This is how the config file looks by default (in v1.0.0-rc.1)

    /*
    |--------------------------------------------------------------------------
    | Required Claims
    |--------------------------------------------------------------------------
    |
    | Specify the required claims that must exist in any token.
    | A TokenInvalidException will be thrown if any of these claims are not
    | present in the payload.
    |
    */

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

Also seeing this since 1.0.0-rc1 creating a token with JWTFactory prior to RC1 I was able to do this. Now I recieve the above error also. We are doing the following:

````php
$factory = JWTFactory::addClaims([
'sub' => env('API_ID'),
'iss' => config('app.name'),
'iat' => Carbon::now()->timestamp,
'exp' => JWTFactory::getTTL(),
'nbf' => Carbon::now()->timestamp,
'jti' => uniqid(),
]);

    $payload = $factory->make();

    $token = JWTAuth::encode($payload);

    return ['HTTP_Authorization' => "Bearer {$token}"];

````

our config shows:

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

We resolved this issue by changing the method called to build the custom token, previously before RC1 we made our custom token as such:

````php
$factory = JWTFactory::addClaims([
'sub' => env('API_ID'),
]);

$payload = $factory->make();

$token = JWTAuth::encode($payload);

return ['HTTP_Authorization' => "Bearer {$token}"];
````

When upgrading we received the error that @Gardezi1 was seeing:

php "payload does not contain the required claims"

Solution

the resolution was simply changing the factory calls to build the token as such

````php
$factory = JWTFactory::customClaims([
'sub' => env('API_ID'),
]);

    $payload = $factory->make();

    $token = JWTAuth::encode($payload);

    return ['HTTP_Authorization' => "Bearer {$token}"];

``` ChangeaddClaimstocustomClaims`.

For interest

We create custom "service-account" tokens to authenticate HTTP requests between services in our micro-service app that is built of around 6 laravel apps. API_ID is simply a secret shared amongst the services.

@andrewmclagan I made the addClaims method protected as it was causing some issues, so yea you are correct to use customClaims (or just claims() works too) instead.

This should work too..

$payload = auth()->factory()->claims(['sub' => env('API_ID')])->make();
$token = auth()->manager()->encode($payload);

Note that the JWTAuth class will be deprecated in the next major release, and everything will go through Laravel's Guard

I running into the same error with 0.5.*
The following code does prevent the error:

$factory = JWTFactory::customClaims([
            'sub'   => env('API_ID'),
        ]);

        $payload = $factory->make();

        $token = JWTAuth::encode($payload);

        return ['HTTP_Authorization' => "Bearer {$token}"];

However the token I get cannot be validated by jwt.io. It claims the "signature is invalid".

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hfalucas picture hfalucas  路  3Comments

shah-newaz picture shah-newaz  路  3Comments

kofi1995 picture kofi1995  路  3Comments

functionpointdaniel picture functionpointdaniel  路  3Comments

marciomansur picture marciomansur  路  3Comments