Skip to content

Commit

Permalink
Add more info to Algorithm enum (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin255 authored Jan 9, 2023
1 parent be0a869 commit 6bd70b7
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Signer/Algorithm.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,26 @@ public function getPhpAlgName(): string
default => '',
};
}

public function getImplementation(): string
{
return match ($this) {
Algorithm::HMAC_SHA_256 => HmacSha256Signer::class,
Algorithm::HMAC_SHA_384 => HmacSha384Signer::class,
Algorithm::HMAC_SHA_512 => HmacSha512Signer::class,
Algorithm::RSA_SHA_256 => RsaSha256Signer::class,
Algorithm::RSA_SHA_384 => RsaSha384Signer::class,
Algorithm::RSA_SHA_512 => RsaSha512Signer::class,
Algorithm::NONE => NoneSigner::class,
};
}

public function getType(): AlgorithmType
{
return match ($this) {
Algorithm::HMAC_SHA_256, Algorithm::HMAC_SHA_384, Algorithm::HMAC_SHA_512 => AlgorithmType::HMAC,
Algorithm::RSA_SHA_256, Algorithm::RSA_SHA_384, Algorithm::RSA_SHA_512 => AlgorithmType::RSA,
Algorithm::NONE => AlgorithmType::NONE,
};
}
}
15 changes: 15 additions & 0 deletions src/Signer/AlgorithmType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Marvin255\Jwt\Signer;

/**
* Enum for algorithms types.
*/
enum AlgorithmType: string
{
case HMAC = 'HMAC';
case RSA = 'RSA';
case NONE = 'NONE';
}
92 changes: 92 additions & 0 deletions tests/Signer/AlgorithmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
namespace Marvin255\Jwt\Test\Signer;

use Marvin255\Jwt\Signer\Algorithm;
use Marvin255\Jwt\Signer\AlgorithmType;
use Marvin255\Jwt\Signer\HmacSha256Signer;
use Marvin255\Jwt\Signer\HmacSha384Signer;
use Marvin255\Jwt\Signer\HmacSha512Signer;
use Marvin255\Jwt\Signer\NoneSigner;
use Marvin255\Jwt\Signer\RsaSha256Signer;
use Marvin255\Jwt\Signer\RsaSha384Signer;
use Marvin255\Jwt\Signer\RsaSha512Signer;
use Marvin255\Jwt\Test\BaseCase;

/**
Expand Down Expand Up @@ -33,4 +41,88 @@ public function provideGetPhpAlgName(): array
],
];
}

/**
* @dataProvider provideGetImplementation
*/
public function testGetImplementation(Algorithm $alg, string $result): void
{
$this->assertSame($result, $alg->getImplementation());
}

public function provideGetImplementation(): array
{
return [
'NONE' => [
Algorithm::NONE,
NoneSigner::class,
],
'HMAC_SHA_256' => [
Algorithm::HMAC_SHA_256,
HmacSha256Signer::class,
],
'HMAC_SHA_384' => [
Algorithm::HMAC_SHA_384,
HmacSha384Signer::class,
],
'HMAC_SHA_512' => [
Algorithm::HMAC_SHA_512,
HmacSha512Signer::class,
],
'RSA_SHA_256' => [
Algorithm::RSA_SHA_256,
RsaSha256Signer::class,
],
'RSA_SHA_384' => [
Algorithm::RSA_SHA_384,
RsaSha384Signer::class,
],
'RSA_SHA_512' => [
Algorithm::RSA_SHA_512,
RsaSha512Signer::class,
],
];
}

/**
* @dataProvider provideTestGetType
*/
public function testGetType(Algorithm $alg, AlgorithmType $result): void
{
$this->assertSame($result, $alg->getType());
}

public function provideTestGetType(): array
{
return [
'NONE' => [
Algorithm::NONE,
AlgorithmType::NONE,
],
'HMAC_SHA_256' => [
Algorithm::HMAC_SHA_256,
AlgorithmType::HMAC,
],
'HMAC_SHA_384' => [
Algorithm::HMAC_SHA_384,
AlgorithmType::HMAC,
],
'HMAC_SHA_512' => [
Algorithm::HMAC_SHA_512,
AlgorithmType::HMAC,
],
'RSA_SHA_256' => [
Algorithm::RSA_SHA_256,
AlgorithmType::RSA,
],
'RSA_SHA_384' => [
Algorithm::RSA_SHA_384,
AlgorithmType::RSA,
],
'RSA_SHA_512' => [
Algorithm::RSA_SHA_512,
AlgorithmType::RSA,
],
];
}
}

0 comments on commit 6bd70b7

Please sign in to comment.