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);
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"
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`.
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".
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'),
]);
```
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_IDis simply a secret shared amongst the services.