Skip to content

Commit

Permalink
Merge pull request #12 from bizley/added-eddsa
Browse files Browse the repository at this point in the history
EdDSA signer
  • Loading branch information
Bizley authored Jun 24, 2021
2 parents 89b7e37 + ab21857 commit 3bb0306
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Symmetric:
Asymmetric:
- RSA (RS256, RS384, RS512)
- ECDSA (ES256, ES384, ES512)
- EdDSA (since 3.1.0)

Signer IDs are available as constants (like Jwt::HS256).

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"require": {
"php": ">=7.4",
"lcobucci/jwt": "^4.0",
"lcobucci/jwt": "^4.1",
"yiisoft/yii2": ">=2.0.14 <2.1"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@default": true,
"MethodCallRemoval": {
"ignore": [
"bizley\\jwt\\Jwt::init::187",
"bizley\\jwt\\Jwt::init::190",
"bizley\\jwt\\JwtHttpBearerAuth::init::68"
]
}
Expand Down
3 changes: 3 additions & 0 deletions src/Jwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Jwt extends Component
public const ES256 = 'ES256';
public const ES384 = 'ES384';
public const ES512 = 'ES512';
public const EDDSA = 'EdDSA';

public const STORE_IN_MEMORY = 'in_memory';
public const STORE_LOCAL_FILE_REFERENCE = 'local_file_reference';
Expand Down Expand Up @@ -129,6 +130,7 @@ class Jwt extends Component
self::ES256 => [Signer\Ecdsa\Sha256::class],
self::ES384 => [Signer\Ecdsa\Sha384::class],
self::ES512 => [Signer\Ecdsa\Sha512::class],
self::EDDSA => [Signer\Eddsa::class],
];

/**
Expand All @@ -148,6 +150,7 @@ class Jwt extends Component
self::ES256,
self::ES384,
self::ES512,
self::EDDSA,
],
];

Expand Down
8 changes: 4 additions & 4 deletions tests/BearerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Lcobucci\Clock\SystemClock;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Validation\Constraint\IssuedBy;
use Lcobucci\JWT\Validation\Constraint\ValidAt;
use Lcobucci\JWT\Validation\Constraint\LooseValidAt;
use PHPUnit\Framework\TestCase;
use Yii;
use yii\base\InvalidConfigException;
Expand Down Expand Up @@ -122,7 +122,7 @@ public function testHttpBearerAuthExpiredToken(): void
{
$now = new DateTimeImmutable();

$this->getJwt()->getConfiguration()->setValidationConstraints(new ValidAt(SystemClock::fromSystemTimezone()));
$this->getJwt()->getConfiguration()->setValidationConstraints(new LooseValidAt(SystemClock::fromSystemTimezone()));

$token = $this->getJwt()->getBuilder()
->issuedAt($now->modify('-10 minutes'))
Expand Down Expand Up @@ -151,7 +151,7 @@ public function testHttpBearerAuth(): void
$now = new DateTimeImmutable();

$this->getJwt()->getConfiguration()->setValidationConstraints(
new ValidAt(SystemClock::fromSystemTimezone()),
new LooseValidAt(SystemClock::fromSystemTimezone()),
new IssuedBy('test')
);

Expand Down Expand Up @@ -179,7 +179,7 @@ public function testHttpBearerAuthCustom(): void
{
$now = new DateTimeImmutable();

$this->getJwt()->getConfiguration()->setValidationConstraints(new ValidAt(SystemClock::fromSystemTimezone()));
$this->getJwt()->getConfiguration()->setValidationConstraints(new LooseValidAt(SystemClock::fromSystemTimezone()));

$token = $this->getJwt()->getBuilder()
->relatedTo('test')
Expand Down
43 changes: 43 additions & 0 deletions tests/JwtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use bizley\tests\stubs\JwtStub;
use Lcobucci\JWT\Decoder;
use Lcobucci\JWT\Encoder;
use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Validation\Constraint\IdentifiedBy;
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
use PHPUnit\Framework\TestCase;
Expand All @@ -16,6 +17,48 @@

class JwtTest extends TestCase
{
public function testAvailableSigners(): void
{
self::assertSame(
[
Jwt::HS256 => [Signer\Hmac\Sha256::class],
Jwt::HS384 => [Signer\Hmac\Sha384::class],
Jwt::HS512 => [Signer\Hmac\Sha512::class],
Jwt::RS256 => [Signer\Rsa\Sha256::class],
Jwt::RS384 => [Signer\Rsa\Sha384::class],
Jwt::RS512 => [Signer\Rsa\Sha512::class],
Jwt::ES256 => [Signer\Ecdsa\Sha256::class],
Jwt::ES384 => [Signer\Ecdsa\Sha384::class],
Jwt::ES512 => [Signer\Ecdsa\Sha512::class],
Jwt::EDDSA => [Signer\Eddsa::class],
],
(new Jwt())->signers,
);
}

public function testAvailableAlgorithmTypes(): void
{
self::assertSame(
[
Jwt::SYMMETRIC => [
Jwt::HS256,
Jwt::HS384,
Jwt::HS512,
],
Jwt::ASYMMETRIC => [
Jwt::RS256,
Jwt::RS384,
Jwt::RS512,
Jwt::ES256,
Jwt::ES384,
Jwt::ES512,
Jwt::EDDSA,
],
],
(new Jwt())->algorithmTypes,
);
}

public function testNoInit(): void
{
$this->expectException(InvalidConfigException::class);
Expand Down

0 comments on commit 3bb0306

Please sign in to comment.