jwt-auth
jwt-auth copied to clipboard
Use JWT in Artisan Command
Use JWT in Artisan Command
I try to get a admin token in a artisan command with this code:
$token = Auth('api')->tokenById(1);
and this exception returned:
PHPOpenSourceSaver\JWTAuth\Exceptions\JWTException
Could not create token: It was not possible to parse your key, reason: error:80000003:system library::No such process
at vendor\php-open-source-saver\jwt-auth\src\Providers\JWT\Lcobucci.php:143
The screenshot it's an command in your interface extending the Laravel Command right?
yes I created a command extended Laravel Console Command
yes I created a command extended Laravel Console Command
Can you provide a snippet of the command or at least a working project to us reproduce it?
I need a token for admin in my commands and try to generate it by this code:
public function handle() { auth('api')->tokenById(1); }
I tested this in tinker and wrote a quick console command, worked in both cases.
I suspect you've a config / setup issue here.
Could not create token: It was not possible to parse your key, reason: error:80000003:system library::No such process
The message says it: there's a problem with your key, the error is thrown from the lcobucci library here
I get token in normal action like this
Route::get('/', function () { return auth('api')->tokenById(1); });
My Env:
JWT_ALGO=RS256 JWT_PRIVATE_KEY=file://../storage/certs/jwt-rsa-2048-private.pem JWT_PUBLIC_KEY=file://../storage/certs/jwt-rsa-2048-public.pem
it could be possible can't get key with this path in artisan command !?
It's, but as the console threw at you, there's something with the key, might be the path, or for some reason of config it wasn't possible to parse it. I did as @mfn and worked fine here at my side.
The problem is use relative path ` public function encode(array $payload) { $this->builder = null; $this->builder = $this->config->builder();
try {
foreach ($payload as $key => $value) {
$this->addClaim($key, $value);
}
//Add This Line
dd(getcwd() , $this->config->signingKey());
return $this->builder->getToken($this->config->signer(), $this->config->signingKey())->toString();
} catch (Exception $e) {
throw new JWTException('Could not create token: '.$e->getMessage(), $e->getCode(), $e);
}
}`
When call from command:
PS D:\Projects\PolBan> php artisan app:update-rates "D:\Projects\PolBan" // vendor\php-open-source-saver\jwt-auth\src\Providers\JWT\Lcobucci.php:140 Lcobucci\JWT\Signer\Key\InMemory {#1820 -contents: "file://storage/certs/jwt-rsa-2048-private.pem" -passphrase: "" }
when call from default route:
"D:\Projects\PolBan\public" // vendor\php-open-source-saver\jwt-auth\src\Providers\JWT\Lcobucci.php:140 Lcobucci\JWT\Signer\Key\InMemory {#1453 // vendor\php-open-source-saver\jwt-auth\src\Providers\JWT\Lcobucci.php:140 -contents: "file://../storage/certs/jwt-rsa-2048-private.pem" -passphrase: "" }
"/public" path is missing and relative path going wrong :(
The problem is use relative path ` public function encode(array $payload) { $this->builder = null; $this->builder = $this->config->builder();
try { foreach ($payload as $key => $value) { $this->addClaim($key, $value); } //Add This Line dd(getcwd() , $this->config->signingKey()); return $this->builder->getToken($this->config->signer(), $this->config->signingKey())->toString(); } catch (Exception $e) { throw new JWTException('Could not create token: '.$e->getMessage(), $e->getCode(), $e); } }`
When call from command:
PS D:\Projects\PolBan> php artisan app:update-rates "D:\Projects\PolBan" // vendor\php-open-source-saver\jwt-auth\src\Providers\JWT\Lcobucci.php:140 Lcobucci\JWT\Signer\Key\InMemory {#1820 -contents: "file://storage/certs/jwt-rsa-2048-private.pem" -passphrase: "" }
when call from default route:
"D:\Projects\PolBan\public" // vendor\php-open-source-saver\jwt-auth\src\Providers\JWT\Lcobucci.php:140 Lcobucci\JWT\Signer\Key\InMemory {#1453 // vendor\php-open-source-saver\jwt-auth\src\Providers\JWT\Lcobucci.php:140 -contents: "file://../storage/certs/jwt-rsa-2048-private.pem" -passphrase: "" }
"/public" path is missing and relative path going wrong :(