Skip to content

Commit 6bd70b7

Browse files
authored
Add more info to Algorithm enum (#13)
1 parent be0a869 commit 6bd70b7

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

src/Signer/Algorithm.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,26 @@ public function getPhpAlgName(): string
2929
default => '',
3030
};
3131
}
32+
33+
public function getImplementation(): string
34+
{
35+
return match ($this) {
36+
Algorithm::HMAC_SHA_256 => HmacSha256Signer::class,
37+
Algorithm::HMAC_SHA_384 => HmacSha384Signer::class,
38+
Algorithm::HMAC_SHA_512 => HmacSha512Signer::class,
39+
Algorithm::RSA_SHA_256 => RsaSha256Signer::class,
40+
Algorithm::RSA_SHA_384 => RsaSha384Signer::class,
41+
Algorithm::RSA_SHA_512 => RsaSha512Signer::class,
42+
Algorithm::NONE => NoneSigner::class,
43+
};
44+
}
45+
46+
public function getType(): AlgorithmType
47+
{
48+
return match ($this) {
49+
Algorithm::HMAC_SHA_256, Algorithm::HMAC_SHA_384, Algorithm::HMAC_SHA_512 => AlgorithmType::HMAC,
50+
Algorithm::RSA_SHA_256, Algorithm::RSA_SHA_384, Algorithm::RSA_SHA_512 => AlgorithmType::RSA,
51+
Algorithm::NONE => AlgorithmType::NONE,
52+
};
53+
}
3254
}

src/Signer/AlgorithmType.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Marvin255\Jwt\Signer;
6+
7+
/**
8+
* Enum for algorithms types.
9+
*/
10+
enum AlgorithmType: string
11+
{
12+
case HMAC = 'HMAC';
13+
case RSA = 'RSA';
14+
case NONE = 'NONE';
15+
}

tests/Signer/AlgorithmTest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
namespace Marvin255\Jwt\Test\Signer;
66

77
use Marvin255\Jwt\Signer\Algorithm;
8+
use Marvin255\Jwt\Signer\AlgorithmType;
9+
use Marvin255\Jwt\Signer\HmacSha256Signer;
10+
use Marvin255\Jwt\Signer\HmacSha384Signer;
11+
use Marvin255\Jwt\Signer\HmacSha512Signer;
12+
use Marvin255\Jwt\Signer\NoneSigner;
13+
use Marvin255\Jwt\Signer\RsaSha256Signer;
14+
use Marvin255\Jwt\Signer\RsaSha384Signer;
15+
use Marvin255\Jwt\Signer\RsaSha512Signer;
816
use Marvin255\Jwt\Test\BaseCase;
917

1018
/**
@@ -33,4 +41,88 @@ public function provideGetPhpAlgName(): array
3341
],
3442
];
3543
}
44+
45+
/**
46+
* @dataProvider provideGetImplementation
47+
*/
48+
public function testGetImplementation(Algorithm $alg, string $result): void
49+
{
50+
$this->assertSame($result, $alg->getImplementation());
51+
}
52+
53+
public function provideGetImplementation(): array
54+
{
55+
return [
56+
'NONE' => [
57+
Algorithm::NONE,
58+
NoneSigner::class,
59+
],
60+
'HMAC_SHA_256' => [
61+
Algorithm::HMAC_SHA_256,
62+
HmacSha256Signer::class,
63+
],
64+
'HMAC_SHA_384' => [
65+
Algorithm::HMAC_SHA_384,
66+
HmacSha384Signer::class,
67+
],
68+
'HMAC_SHA_512' => [
69+
Algorithm::HMAC_SHA_512,
70+
HmacSha512Signer::class,
71+
],
72+
'RSA_SHA_256' => [
73+
Algorithm::RSA_SHA_256,
74+
RsaSha256Signer::class,
75+
],
76+
'RSA_SHA_384' => [
77+
Algorithm::RSA_SHA_384,
78+
RsaSha384Signer::class,
79+
],
80+
'RSA_SHA_512' => [
81+
Algorithm::RSA_SHA_512,
82+
RsaSha512Signer::class,
83+
],
84+
];
85+
}
86+
87+
/**
88+
* @dataProvider provideTestGetType
89+
*/
90+
public function testGetType(Algorithm $alg, AlgorithmType $result): void
91+
{
92+
$this->assertSame($result, $alg->getType());
93+
}
94+
95+
public function provideTestGetType(): array
96+
{
97+
return [
98+
'NONE' => [
99+
Algorithm::NONE,
100+
AlgorithmType::NONE,
101+
],
102+
'HMAC_SHA_256' => [
103+
Algorithm::HMAC_SHA_256,
104+
AlgorithmType::HMAC,
105+
],
106+
'HMAC_SHA_384' => [
107+
Algorithm::HMAC_SHA_384,
108+
AlgorithmType::HMAC,
109+
],
110+
'HMAC_SHA_512' => [
111+
Algorithm::HMAC_SHA_512,
112+
AlgorithmType::HMAC,
113+
],
114+
'RSA_SHA_256' => [
115+
Algorithm::RSA_SHA_256,
116+
AlgorithmType::RSA,
117+
],
118+
'RSA_SHA_384' => [
119+
Algorithm::RSA_SHA_384,
120+
AlgorithmType::RSA,
121+
],
122+
'RSA_SHA_512' => [
123+
Algorithm::RSA_SHA_512,
124+
AlgorithmType::RSA,
125+
],
126+
];
127+
}
36128
}

0 commit comments

Comments
 (0)