jwt-auth icon indicating copy to clipboard operation
jwt-auth copied to clipboard

Use JWT in Artisan Command

Open Benyamin001 opened this issue 1 year ago • 8 comments

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

image

Benyamin001 avatar Feb 18 '24 07:02 Benyamin001

The screenshot it's an command in your interface extending the Laravel Command right?

Messhias avatar Feb 20 '24 23:02 Messhias

yes I created a command extended Laravel Console Command

Benyamin001 avatar Feb 21 '24 12:02 Benyamin001

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?

Messhias avatar Feb 21 '24 12:02 Messhias

I need a token for admin in my commands and try to generate it by this code:

public function handle() { auth('api')->tokenById(1); }

Benyamin001 avatar Feb 21 '24 13:02 Benyamin001

I tested this in tinker and wrote a quick console command, worked in both cases.

I suspect you've a config / setup issue here.

mfn avatar Feb 21 '24 14:02 mfn

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

mfn avatar Feb 21 '24 14:02 mfn

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 !?

Benyamin001 avatar Feb 21 '24 15:02 Benyamin001

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.

Messhias avatar Feb 21 '24 15:02 Messhias

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 :(

Benyamin001 avatar Mar 05 '24 14:03 Benyamin001

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 :(

See if that could help you.

Also check the Laravel documentation for paths.

Messhias avatar Mar 05 '24 15:03 Messhias