Skip to content

Commit

Permalink
remove chained formatter instance from config, use string value
Browse files Browse the repository at this point in the history
  • Loading branch information
jszobody committed Feb 13, 2024
1 parent b725930 commit 1ec4fa0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
4 changes: 1 addition & 3 deletions config/jwt.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use Lcobucci\JWT\Encoding\ChainedFormatter;

return [
// Look for a dedicated signing key, fall back to app key
'key' => env('JWT_SIGNING_KEY', env('APP_KEY')),
Expand All @@ -25,5 +23,5 @@
'audience' => env('JWT_VALIDATE_AUDIENCE', true),
],

'chained_formatter' => ChainedFormatter::default(),
'chained_formatter' => env('JWT_CHAINED_FORMATTER', 'default'),
];
8 changes: 7 additions & 1 deletion src/JWTServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Lcobucci\JWT\Encoding\ChainedFormatter;

class JWTServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -37,7 +38,12 @@ public function register(): void
}

$signer = config('jwt.signer');
$chainedFormatter = config('jwt.chained_formatter');

$chainedFormatter = match(true) {
config('jwt.chained_formatter') instanceof ChainedFormatter => config('jwt.chained_formatter'),
config('jwt.chained_formatter') === "unix" => ChainedFormatter::withUnixTimestampDates(),
default => ChainedFormatter::default()
};

return new Client(
$key,
Expand Down
2 changes: 1 addition & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function getEnvironmentSetUp($app): void
'issuer' => 'myappiss',
'lifetime' => 900,
'signer' => \Lcobucci\JWT\Signer\Hmac\Sha256::class,
'chained_formatter' => ChainedFormatter::default(),
'chained_formatter' => "default",
]]);
}

Expand Down
9 changes: 9 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ public function testSigningKeyWithExplicitJwtKey()

$this->assertEquals('thisisjwtkey', config('jwt.key'));
}

public function testChainedFormatter()
{
putenv('JWT_CHAINED_FORMATTER=unix');

$this->refreshApplication();

$this->assertEquals('unix', config('jwt.chained_formatter'));
}
}

0 comments on commit 1ec4fa0

Please sign in to comment.