Skip to content

Commit

Permalink
Merge pull request #2 from stechstudio/Signer_And_Formatter_Config
Browse files Browse the repository at this point in the history
Signer and formatter config
  • Loading branch information
jszobody authored Aug 9, 2023
2 parents 4c5430c + 1b63ba8 commit 4dd8301
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 8 deletions.
12 changes: 10 additions & 2 deletions config/jwt.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<?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')),

// Default Signer class
'signer' => \Lcobucci\JWT\Signer\Hmac\Sha256::class,

// Default lifetime in seconds
'lifetime' => env('JWT_LIFETIME', 600),

Expand All @@ -17,5 +23,7 @@
'validate' => [
// If you really need to avoid automatic audience validation
'audience' => env('JWT_VALIDATE_AUDIENCE', true),
]
];
],

'chained_formatter' => ChainedFormatter::default(),
];
10 changes: 6 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Illuminate\Support\Traits\ForwardsCalls;
use Lcobucci\JWT\Encoding\ChainedFormatter;
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Token\Builder;
use Lcobucci\JWT\Token\Plain;
Expand All @@ -28,11 +28,13 @@ class Client

public function __construct(
protected string $signingKey,
protected Signer $signer,
protected ChainedFormatter $chainedFormatter,
protected int|CarbonImmutable $lifetime,
protected string $issuer,
protected string $audience)
{
$this->builder = new Builder(new JoseEncoder(), ChainedFormatter::default());
$this->builder = new Builder(new JoseEncoder(), $this->chainedFormatter);
}

public function signWith(string $signingKey): self
Expand Down Expand Up @@ -64,7 +66,7 @@ public function getToken(): Plain
in_array('issuedBy', $this->configures) || $this->issuedBy($this->issuer());
in_array('expiresAt', $this->configures) || $this->lifetime($this->lifetime);

return $this->builder->getToken(new Sha256(), InMemory::plainText($this->signingKey()));
return $this->builder->getToken($this->signer, InMemory::plainText($this->signingKey()));
}

public function __toString(): string
Expand Down Expand Up @@ -127,4 +129,4 @@ public function parse(string $jwt): ParsedToken
{
return ParsedToken::fromString($jwt);
}
}
}
7 changes: 6 additions & 1 deletion src/JWTServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ public function register(): void
$key = base64_decode(substr($key, 7));
}

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

return new Client(
$key,
new $signer(),
$chainedFormatter,
config('jwt.lifetime'),
config('jwt.issuer'),
config('jwt.audience')
Expand All @@ -49,4 +54,4 @@ public function provides(): array
{
return [Client::class];
}
}
}
66 changes: 65 additions & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Lcobucci\JWT\Encoding\ChainedFormatter;
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key\InMemory;
Expand Down Expand Up @@ -32,6 +33,8 @@ protected function getEnvironmentSetUp($app): void
'audience' => 'myappaud',
'issuer' => 'myappiss',
'lifetime' => 900,
'signer' => \Lcobucci\JWT\Signer\Hmac\Sha256::class,
'chained_formatter' => ChainedFormatter::default(),
]]);
}

Expand Down Expand Up @@ -113,6 +116,66 @@ public function testLifetime()
$this->assertTrue($token->isExpired(CarbonImmutable::now()->addMinutes(5)));
}

public function testDefaultSigner()
{
$token = JWT::expiresAt(Carbon::now()->addMinutes(10))->getToken();

$this->assertTrue(
(new Validator())->validate(
$token,
new SignedWith(new Sha256(), InMemory::plainText("thisissigningkeythisissigningkey"))
)
);
}

public function testRsaSha256Signer()
{
$rsa = new \Lcobucci\JWT\Signer\Rsa\Sha256();
$privateKey = file_get_contents(__DIR__ . '/keys/jwtRS256.key');
$publicKey = InMemory::plainText(file_get_contents(__DIR__ . '/keys/jwtRS256.key.pub'));

config(['jwt.signer' => \Lcobucci\JWT\Signer\Rsa\Sha256::class]);

$token = JWT::get('test-id', ['foo' => 'bar'], 1800, $privateKey);

$parsedToken = (new Parser(new JoseEncoder()))->parse($token);

$this->assertTrue(
(new Validator())->validate(
$parsedToken,
new SignedWith($rsa, $publicKey)
)
);
}

public function testDefaultTimestampFormatter()
{
$time = Carbon::now()->addMinutes(10);
$token = JWT::expiresAt($time)->getToken();

$parts = array_map('base64_decode', explode('.', $token->toString()));

$this->assertEquals(
$time->format('U.u'),
json_decode($parts[1])->exp
);
}

public function testUnixTimestampFormatter()
{
config(['jwt.chained_formatter' => ChainedFormatter::withUnixTimestampDates()]);

$time = Carbon::now()->addMinutes(10);
$token = JWT::expiresAt($time)->getToken();

$parts = array_map('base64_decode', explode('.', $token->toString()));

$this->assertEquals(
$time->format('U'),
json_decode($parts[1])->exp
);
}

public function testQuickGet()
{
$jwt = JWT::get('test-id', ['foo' => 'bar'], 1800);
Expand All @@ -132,4 +195,5 @@ public function testQuickGet()
)
);
}
}
}

51 changes: 51 additions & 0 deletions tests/keys/jwtRS256.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
-----BEGIN RSA PRIVATE KEY-----
MIIJJwIBAAKCAgEAt/kVyHFiIz1uQdiL2ECXxVGqy2brucaCmp5Tp1uVvgx17v/x
2fhzBqBSJvpZ37s41klVQP1ZMLoqoB9jkuuiKJxnjCJMA7YQZEwI9Z+mZETdnDF1
0IY/uC+9aGGmDdcDoKjkPKsXcjNypZpclHWnhmeY4+L+Ep9fdx4ust5j0lYIaEZa
InM0itZrepVENjMwFCsnr7DZNZSuWljZ9gquD1A20uLOutTxCRpRB/3I+qOwUdHR
G3w6hyE4pLNhqWsjVMVCYps9e/YePmBuDnURRWe/5Kpnai/ojVzKjDzn+z2Y9F8h
tR46Tpg/3PUpNcEwK2WOFN4CaJsC7leXy5TO4U/f9sBlJ/ER5Z9CMIZKInK+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xl0hLvScVDhRTI+Mbtycz4NnppjtIDWZEUcoYhe7wUpvxzc3WZX48Y9dmA
zjBsOAJjOIBJr0EIllygvfy+yzGsq3G69QsjNEuaVNjdQeHeK7CrIk5ICe0xYVxH
bebtfmBizgL8I7bRWn5zk7yqRywXvzccR4BExsyVAX782uybkWvGuCytSm9C8m+9
66xTOMOTdOW3Qkle9lgzN6yyqA5IH3ZnIkhICsuM/pHCWGqCW3xOHmpieAjF1f2m
RSm7RGjMa65XwQj0Lr9j/BBdlNtqYwsCggEAKsBX0ph2HAvH0Ml8kZ1cRnGSi9jM
hAwHSlq62yJzn4aOO/7qhgi3Ul4F82aW11GVd7Sw0zIoJaBRNfRoxLbHLA1hkEa/
Rof2+LmxuwGoR2YyIzGAWNecYMIghSTNYfYF0IhGuU1/nsMocBABrJ+IOZTw2DmH
r4rJ4m15qwWSZUgAqaQpu2zqrgFEzgGlpfC2vki5J2JTxxjfp7V+vViHsuQNLO1w
AZcNhzgxgW2tu4BycVJYqQBfQsA10UMMzbQ4zXN0oQp/fs6cedjBeAYirowojsS9
aBbkYOF6swTErTIsaeoE5IPgv391TKJQp9TS6bc5iHMu9jVwQawBtbPPbg==
-----END RSA PRIVATE KEY-----
14 changes: 14 additions & 0 deletions tests/keys/jwtRS256.key.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAt/kVyHFiIz1uQdiL2ECX
xVGqy2brucaCmp5Tp1uVvgx17v/x2fhzBqBSJvpZ37s41klVQP1ZMLoqoB9jkuui
KJxnjCJMA7YQZEwI9Z+mZETdnDF10IY/uC+9aGGmDdcDoKjkPKsXcjNypZpclHWn
hmeY4+L+Ep9fdx4ust5j0lYIaEZaInM0itZrepVENjMwFCsnr7DZNZSuWljZ9gqu
D1A20uLOutTxCRpRB/3I+qOwUdHRG3w6hyE4pLNhqWsjVMVCYps9e/YePmBuDnUR
RWe/5Kpnai/ojVzKjDzn+z2Y9F8htR46Tpg/3PUpNcEwK2WOFN4CaJsC7leXy5TO
4U/f9sBlJ/ER5Z9CMIZKInK+QvOJwExelyWfDZzGuTfRHxWl/HuNQLUKciuqzwKj
DfV4wEdQjz7SaLyTjAQzy9ZArylA3pEzJmlnWVdCTi5Xt+q5mWtfbZUY+XeIuvjq
A1u/dCXiPpArQgpTfwb3CKxLiXRvRV73UQ3moTNS/4XP33eqY0HXlmW9HvhUVhty
AT6/zLbRx+dLyq89kvgt1kUXke4cRxDbrajlKurrQSSp8BZ+MQQrvT1cucuNAjhz
OQw/Jlj4Is6OgoUAENX4az0TjWmhb41gplZGfhfF7f5YHattY55HH/jovB5mO2Jy
jOJhDZN3soMdwwO4lYysZz0CAwEAAQ==
-----END PUBLIC KEY-----

0 comments on commit 4dd8301

Please sign in to comment.