Skip to content

Commit a27a53d

Browse files
authored
Rename bundle and library folders (#510)
* Rename bundle and library folders * Fix Composer deps tests * Move packs to deprecated repos
0 parents  commit a27a53d

File tree

213 files changed

+13873
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

213 files changed

+13873
-0
lines changed

.github/CONTRIBUTING.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Contributing
2+
3+
This repository is a sub repository of [the JWT Framework](https://github.com/web-token/jwt-framework) project and is
4+
READ ONLY.
5+
Please do not submit any Pull Requests here. It will be automatically closed.

.github/FUNDING.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github: Spomky
2+
patreon: FlorentMorselli

.github/PULL_REQUEST_TEMPLATE.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Please do not submit any Pull Requests here. It will be automatically closed.
2+
3+
You should submit it here: https://github.com/web-token/jwt-framework/pulls

.github/stale.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
daysUntilStale: 60
2+
daysUntilClose: 7
3+
staleLabel: wontfix
4+
markComment: >
5+
This issue has been automatically marked as stale because it has not had
6+
recent activity. It will be closed if no further activity occurs. Thank you
7+
for your contributions.
8+
closeComment: false

Checker/AlgorithmChecker.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jose\Component\Checker;
6+
7+
use function in_array;
8+
use function is_string;
9+
10+
/**
11+
* This class is a header parameter checker. When the "alg" header parameter is present, it will check if the value is
12+
* within the allowed ones.
13+
*/
14+
final class AlgorithmChecker implements HeaderChecker
15+
{
16+
private const HEADER_NAME = 'alg';
17+
18+
/**
19+
* @param string[] $supportedAlgorithms
20+
*/
21+
public function __construct(
22+
private readonly array $supportedAlgorithms,
23+
private readonly bool $protectedHeader = false
24+
) {
25+
}
26+
27+
public function checkHeader(mixed $value): void
28+
{
29+
if (! is_string($value)) {
30+
throw new InvalidHeaderException('"alg" must be a string.', self::HEADER_NAME, $value);
31+
}
32+
if (! in_array($value, $this->supportedAlgorithms, true)) {
33+
throw new InvalidHeaderException('Unsupported algorithm.', self::HEADER_NAME, $value);
34+
}
35+
}
36+
37+
public function supportedHeader(): string
38+
{
39+
return self::HEADER_NAME;
40+
}
41+
42+
public function protectedHeaderOnly(): bool
43+
{
44+
return $this->protectedHeader;
45+
}
46+
}

Checker/AudienceChecker.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jose\Component\Checker;
6+
7+
use function in_array;
8+
use function is_array;
9+
use function is_string;
10+
11+
/**
12+
* This class is a header parameter and claim checker. When the "aud" header parameter or claim is present, it will
13+
* check if the value is within the allowed ones.
14+
*/
15+
final class AudienceChecker implements ClaimChecker, HeaderChecker
16+
{
17+
private const CLAIM_NAME = 'aud';
18+
19+
public function __construct(
20+
private readonly string $audience,
21+
private readonly bool $protectedHeader = false
22+
) {
23+
}
24+
25+
public function checkClaim(mixed $value): void
26+
{
27+
$this->checkValue($value, InvalidClaimException::class);
28+
}
29+
30+
public function checkHeader(mixed $value): void
31+
{
32+
$this->checkValue($value, InvalidHeaderException::class);
33+
}
34+
35+
public function supportedClaim(): string
36+
{
37+
return self::CLAIM_NAME;
38+
}
39+
40+
public function supportedHeader(): string
41+
{
42+
return self::CLAIM_NAME;
43+
}
44+
45+
public function protectedHeaderOnly(): bool
46+
{
47+
return $this->protectedHeader;
48+
}
49+
50+
private function checkValue(mixed $value, string $class): void
51+
{
52+
if (is_string($value) && $value !== $this->audience) {
53+
throw new $class('Bad audience.', self::CLAIM_NAME, $value);
54+
}
55+
if (is_array($value) && ! in_array($this->audience, $value, true)) {
56+
throw new $class('Bad audience.', self::CLAIM_NAME, $value);
57+
}
58+
if (! is_array($value) && ! is_string($value)) {
59+
throw new $class('Bad audience.', self::CLAIM_NAME, $value);
60+
}
61+
}
62+
}

Checker/CallableChecker.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jose\Component\Checker;
6+
7+
use InvalidArgumentException;
8+
use function call_user_func;
9+
use function is_callable;
10+
11+
/**
12+
* @see \Jose\Tests\Component\Checker\CallableCheckerTest
13+
*/
14+
final class CallableChecker implements ClaimChecker, HeaderChecker
15+
{
16+
/**
17+
* @param string $key The claim or header parameter name to check.
18+
* @param callable(mixed $value): bool $callable The callable function that will be invoked.
19+
*/
20+
public function __construct(
21+
private readonly string $key,
22+
private $callable,
23+
private readonly bool $protectedHeaderOnly = true
24+
) {
25+
if (! is_callable($this->callable)) { // @phpstan-ignore-line
26+
throw new InvalidArgumentException('The $callable argument must be a callable.');
27+
}
28+
}
29+
30+
public function checkClaim(mixed $value): void
31+
{
32+
if (call_user_func($this->callable, $value) !== true) {
33+
throw new InvalidClaimException(sprintf('The "%s" claim is invalid.', $this->key), $this->key, $value);
34+
}
35+
}
36+
37+
public function supportedClaim(): string
38+
{
39+
return $this->key;
40+
}
41+
42+
public function checkHeader(mixed $value): void
43+
{
44+
if (call_user_func($this->callable, $value) !== true) {
45+
throw new InvalidHeaderException(sprintf('The "%s" header is invalid.', $this->key), $this->key, $value);
46+
}
47+
}
48+
49+
public function supportedHeader(): string
50+
{
51+
return $this->key;
52+
}
53+
54+
public function protectedHeaderOnly(): bool
55+
{
56+
return $this->protectedHeaderOnly;
57+
}
58+
}

Checker/ClaimChecker.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jose\Component\Checker;
6+
7+
interface ClaimChecker
8+
{
9+
/**
10+
* When the token has the applicable claim, the value is checked. If for some reason the value is not valid, an
11+
* InvalidClaimException must be thrown.
12+
*/
13+
public function checkClaim(mixed $value): void;
14+
15+
/**
16+
* The method returns the claim to be checked.
17+
*/
18+
public function supportedClaim(): string;
19+
}

Checker/ClaimCheckerManager.php

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jose\Component\Checker;
6+
7+
use function array_key_exists;
8+
use function count;
9+
10+
/**
11+
* This manager handles as many claim checkers as needed.
12+
*
13+
* @see \Jose\Tests\Component\Checker\ClaimCheckerManagerTest
14+
*/
15+
class ClaimCheckerManager
16+
{
17+
/**
18+
* @var ClaimChecker[]
19+
*/
20+
private array $checkers = [];
21+
22+
/**
23+
* @param ClaimChecker[] $checkers
24+
*/
25+
public function __construct(array $checkers)
26+
{
27+
foreach ($checkers as $checker) {
28+
$this->add($checker);
29+
}
30+
}
31+
32+
/**
33+
* This method returns all checkers handled by this manager.
34+
*
35+
* @return ClaimChecker[]
36+
*/
37+
public function getCheckers(): array
38+
{
39+
return $this->checkers;
40+
}
41+
42+
/**
43+
* This method checks all the claims passed as argument. All claims are checked against the claim checkers. If one
44+
* fails, the InvalidClaimException is thrown.
45+
*
46+
* This method returns an array with all checked claims. It is up to the implementor to decide use the claims that
47+
* have not been checked.
48+
*
49+
* @param string[] $mandatoryClaims
50+
*/
51+
public function check(array $claims, array $mandatoryClaims = []): array
52+
{
53+
$this->checkMandatoryClaims($mandatoryClaims, $claims);
54+
$checkedClaims = [];
55+
foreach ($this->checkers as $claim => $checker) {
56+
if (array_key_exists($claim, $claims)) {
57+
$checker->checkClaim($claims[$claim]);
58+
$checkedClaims[$claim] = $claims[$claim];
59+
}
60+
}
61+
62+
return $checkedClaims;
63+
}
64+
65+
private function add(ClaimChecker $checker): void
66+
{
67+
$claim = $checker->supportedClaim();
68+
$this->checkers[$claim] = $checker;
69+
}
70+
71+
/**
72+
* @param string[] $mandatoryClaims
73+
*/
74+
private function checkMandatoryClaims(array $mandatoryClaims, array $claims): void
75+
{
76+
if (count($mandatoryClaims) === 0) {
77+
return;
78+
}
79+
$diff = array_keys(array_diff_key(array_flip($mandatoryClaims), $claims));
80+
if (count($diff) !== 0) {
81+
throw new MissingMandatoryClaimException(sprintf(
82+
'The following claims are mandatory: %s.',
83+
implode(', ', $diff)
84+
), $diff);
85+
}
86+
}
87+
}
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jose\Component\Checker;
6+
7+
use InvalidArgumentException;
8+
9+
/**
10+
* @see \Jose\Tests\Component\Checker\ClaimCheckerManagerFactoryTest
11+
*/
12+
class ClaimCheckerManagerFactory
13+
{
14+
/**
15+
* @var ClaimChecker[]
16+
*/
17+
private array $checkers = [];
18+
19+
/**
20+
* This method creates a Claim Checker Manager and populate it with the claim checkers found based on the alias. If
21+
* the alias is not supported, an InvalidArgumentException is thrown.
22+
*
23+
* @param string[] $aliases
24+
*/
25+
public function create(array $aliases): ClaimCheckerManager
26+
{
27+
$checkers = [];
28+
foreach ($aliases as $alias) {
29+
if (! isset($this->checkers[$alias])) {
30+
throw new InvalidArgumentException(sprintf(
31+
'The claim checker with the alias "%s" is not supported.',
32+
$alias
33+
));
34+
}
35+
$checkers[] = $this->checkers[$alias];
36+
}
37+
38+
return new ClaimCheckerManager($checkers);
39+
}
40+
41+
/**
42+
* This method adds a claim checker to this factory.
43+
*/
44+
public function add(string $alias, ClaimChecker $checker): void
45+
{
46+
$this->checkers[$alias] = $checker;
47+
}
48+
49+
/**
50+
* Returns all claim checker aliases supported by this factory.
51+
*
52+
* @return string[]
53+
*/
54+
public function aliases(): array
55+
{
56+
return array_keys($this->checkers);
57+
}
58+
59+
/**
60+
* Returns all claim checkers supported by this factory.
61+
*
62+
* @return ClaimChecker[]
63+
*/
64+
public function all(): array
65+
{
66+
return $this->checkers;
67+
}
68+
}

Checker/ClaimExceptionInterface.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jose\Component\Checker;
6+
7+
use Throwable;
8+
9+
/**
10+
* Exceptions thrown by this component.
11+
*/
12+
interface ClaimExceptionInterface extends Throwable
13+
{
14+
}

0 commit comments

Comments
 (0)