Subiendo proyecto completo sin restricciones de git ignore
This commit is contained in:
12
vendor/lcobucci/jwt/src/Validation/Constraint.php
vendored
Normal file
12
vendor/lcobucci/jwt/src/Validation/Constraint.php
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Validation;
|
||||
|
||||
use Lcobucci\JWT\Token;
|
||||
|
||||
interface Constraint
|
||||
{
|
||||
/** @throws ConstraintViolation */
|
||||
public function assert(Token $token): void;
|
||||
}
|
||||
27
vendor/lcobucci/jwt/src/Validation/Constraint/IdentifiedBy.php
vendored
Normal file
27
vendor/lcobucci/jwt/src/Validation/Constraint/IdentifiedBy.php
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Validation\Constraint;
|
||||
|
||||
use Lcobucci\JWT\Token;
|
||||
use Lcobucci\JWT\Validation\Constraint;
|
||||
use Lcobucci\JWT\Validation\ConstraintViolation;
|
||||
|
||||
final class IdentifiedBy implements Constraint
|
||||
{
|
||||
private string $id;
|
||||
|
||||
public function __construct(string $id)
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function assert(Token $token): void
|
||||
{
|
||||
if (! $token->isIdentifiedBy($this->id)) {
|
||||
throw new ConstraintViolation(
|
||||
'The token is not identified with the expected ID'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
28
vendor/lcobucci/jwt/src/Validation/Constraint/IssuedBy.php
vendored
Normal file
28
vendor/lcobucci/jwt/src/Validation/Constraint/IssuedBy.php
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Validation\Constraint;
|
||||
|
||||
use Lcobucci\JWT\Token;
|
||||
use Lcobucci\JWT\Validation\Constraint;
|
||||
use Lcobucci\JWT\Validation\ConstraintViolation;
|
||||
|
||||
final class IssuedBy implements Constraint
|
||||
{
|
||||
/** @var string[] */
|
||||
private array $issuers;
|
||||
|
||||
public function __construct(string ...$issuers)
|
||||
{
|
||||
$this->issuers = $issuers;
|
||||
}
|
||||
|
||||
public function assert(Token $token): void
|
||||
{
|
||||
if (! $token->hasBeenIssuedBy(...$this->issuers)) {
|
||||
throw new ConstraintViolation(
|
||||
'The token was not issued by the given issuers'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
15
vendor/lcobucci/jwt/src/Validation/Constraint/LeewayCannotBeNegative.php
vendored
Normal file
15
vendor/lcobucci/jwt/src/Validation/Constraint/LeewayCannotBeNegative.php
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Validation\Constraint;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Lcobucci\JWT\Exception;
|
||||
|
||||
final class LeewayCannotBeNegative extends InvalidArgumentException implements Exception
|
||||
{
|
||||
public static function create(): self
|
||||
{
|
||||
return new self('Leeway cannot be negative');
|
||||
}
|
||||
}
|
||||
27
vendor/lcobucci/jwt/src/Validation/Constraint/PermittedFor.php
vendored
Normal file
27
vendor/lcobucci/jwt/src/Validation/Constraint/PermittedFor.php
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Validation\Constraint;
|
||||
|
||||
use Lcobucci\JWT\Token;
|
||||
use Lcobucci\JWT\Validation\Constraint;
|
||||
use Lcobucci\JWT\Validation\ConstraintViolation;
|
||||
|
||||
final class PermittedFor implements Constraint
|
||||
{
|
||||
private string $audience;
|
||||
|
||||
public function __construct(string $audience)
|
||||
{
|
||||
$this->audience = $audience;
|
||||
}
|
||||
|
||||
public function assert(Token $token): void
|
||||
{
|
||||
if (! $token->isPermittedFor($this->audience)) {
|
||||
throw new ConstraintViolation(
|
||||
'The token is not allowed to be used by this audience'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
27
vendor/lcobucci/jwt/src/Validation/Constraint/RelatedTo.php
vendored
Normal file
27
vendor/lcobucci/jwt/src/Validation/Constraint/RelatedTo.php
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Validation\Constraint;
|
||||
|
||||
use Lcobucci\JWT\Token;
|
||||
use Lcobucci\JWT\Validation\Constraint;
|
||||
use Lcobucci\JWT\Validation\ConstraintViolation;
|
||||
|
||||
final class RelatedTo implements Constraint
|
||||
{
|
||||
private string $subject;
|
||||
|
||||
public function __construct(string $subject)
|
||||
{
|
||||
$this->subject = $subject;
|
||||
}
|
||||
|
||||
public function assert(Token $token): void
|
||||
{
|
||||
if (! $token->isRelatedTo($this->subject)) {
|
||||
throw new ConstraintViolation(
|
||||
'The token is not related to the expected subject'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
36
vendor/lcobucci/jwt/src/Validation/Constraint/SignedWith.php
vendored
Normal file
36
vendor/lcobucci/jwt/src/Validation/Constraint/SignedWith.php
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Validation\Constraint;
|
||||
|
||||
use Lcobucci\JWT\Signer;
|
||||
use Lcobucci\JWT\Token;
|
||||
use Lcobucci\JWT\Validation\Constraint;
|
||||
use Lcobucci\JWT\Validation\ConstraintViolation;
|
||||
|
||||
final class SignedWith implements Constraint
|
||||
{
|
||||
private Signer $signer;
|
||||
private Signer\Key $key;
|
||||
|
||||
public function __construct(Signer $signer, Signer\Key $key)
|
||||
{
|
||||
$this->signer = $signer;
|
||||
$this->key = $key;
|
||||
}
|
||||
|
||||
public function assert(Token $token): void
|
||||
{
|
||||
if (! $token instanceof Token\Plain) {
|
||||
throw new ConstraintViolation('You should pass a plain token');
|
||||
}
|
||||
|
||||
if ($token->headers()->get('alg') !== $this->signer->algorithmId()) {
|
||||
throw new ConstraintViolation('Token signer mismatch');
|
||||
}
|
||||
|
||||
if (! $this->signer->verify($token->signature()->hash(), $token->payload(), $this->key)) {
|
||||
throw new ConstraintViolation('Token signature mismatch');
|
||||
}
|
||||
}
|
||||
}
|
||||
69
vendor/lcobucci/jwt/src/Validation/Constraint/ValidAt.php
vendored
Normal file
69
vendor/lcobucci/jwt/src/Validation/Constraint/ValidAt.php
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Validation\Constraint;
|
||||
|
||||
use DateInterval;
|
||||
use DateTimeInterface;
|
||||
use Lcobucci\Clock\Clock;
|
||||
use Lcobucci\JWT\Token;
|
||||
use Lcobucci\JWT\Validation\Constraint;
|
||||
use Lcobucci\JWT\Validation\ConstraintViolation;
|
||||
|
||||
final class ValidAt implements Constraint
|
||||
{
|
||||
private Clock $clock;
|
||||
private DateInterval $leeway;
|
||||
|
||||
public function __construct(Clock $clock, ?DateInterval $leeway = null)
|
||||
{
|
||||
$this->clock = $clock;
|
||||
$this->leeway = $this->guardLeeway($leeway);
|
||||
}
|
||||
|
||||
private function guardLeeway(?DateInterval $leeway): DateInterval
|
||||
{
|
||||
if ($leeway === null) {
|
||||
return new DateInterval('PT0S');
|
||||
}
|
||||
|
||||
if ($leeway->invert === 1) {
|
||||
throw LeewayCannotBeNegative::create();
|
||||
}
|
||||
|
||||
return $leeway;
|
||||
}
|
||||
|
||||
public function assert(Token $token): void
|
||||
{
|
||||
$now = $this->clock->now();
|
||||
|
||||
$this->assertIssueTime($token, $now->add($this->leeway));
|
||||
$this->assertMinimumTime($token, $now->add($this->leeway));
|
||||
$this->assertExpiration($token, $now->sub($this->leeway));
|
||||
}
|
||||
|
||||
/** @throws ConstraintViolation */
|
||||
private function assertExpiration(Token $token, DateTimeInterface $now): void
|
||||
{
|
||||
if ($token->isExpired($now)) {
|
||||
throw new ConstraintViolation('The token is expired');
|
||||
}
|
||||
}
|
||||
|
||||
/** @throws ConstraintViolation */
|
||||
private function assertMinimumTime(Token $token, DateTimeInterface $now): void
|
||||
{
|
||||
if (! $token->isMinimumTimeBefore($now)) {
|
||||
throw new ConstraintViolation('The token cannot be used yet');
|
||||
}
|
||||
}
|
||||
|
||||
/** @throws ConstraintViolation */
|
||||
private function assertIssueTime(Token $token, DateTimeInterface $now): void
|
||||
{
|
||||
if (! $token->hasBeenIssuedBefore($now)) {
|
||||
throw new ConstraintViolation('The token was issued in the future');
|
||||
}
|
||||
}
|
||||
}
|
||||
11
vendor/lcobucci/jwt/src/Validation/ConstraintViolation.php
vendored
Normal file
11
vendor/lcobucci/jwt/src/Validation/ConstraintViolation.php
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Validation;
|
||||
|
||||
use Lcobucci\JWT\Exception;
|
||||
use RuntimeException;
|
||||
|
||||
final class ConstraintViolation extends RuntimeException implements Exception
|
||||
{
|
||||
}
|
||||
11
vendor/lcobucci/jwt/src/Validation/NoConstraintsGiven.php
vendored
Normal file
11
vendor/lcobucci/jwt/src/Validation/NoConstraintsGiven.php
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Validation;
|
||||
|
||||
use Lcobucci\JWT\Exception;
|
||||
use RuntimeException;
|
||||
|
||||
final class NoConstraintsGiven extends RuntimeException implements Exception
|
||||
{
|
||||
}
|
||||
46
vendor/lcobucci/jwt/src/Validation/RequiredConstraintsViolated.php
vendored
Normal file
46
vendor/lcobucci/jwt/src/Validation/RequiredConstraintsViolated.php
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Validation;
|
||||
|
||||
use Lcobucci\JWT\Exception;
|
||||
use RuntimeException;
|
||||
|
||||
use function array_map;
|
||||
use function implode;
|
||||
|
||||
final class RequiredConstraintsViolated extends RuntimeException implements Exception
|
||||
{
|
||||
/** @var ConstraintViolation[] */
|
||||
private array $violations = [];
|
||||
|
||||
public static function fromViolations(ConstraintViolation ...$violations): self
|
||||
{
|
||||
$exception = new self(self::buildMessage($violations));
|
||||
$exception->violations = $violations;
|
||||
|
||||
return $exception;
|
||||
}
|
||||
|
||||
/** @param ConstraintViolation[] $violations */
|
||||
private static function buildMessage(array $violations): string
|
||||
{
|
||||
$violations = array_map(
|
||||
static function (ConstraintViolation $violation): string {
|
||||
return '- ' . $violation->getMessage();
|
||||
},
|
||||
$violations
|
||||
);
|
||||
|
||||
$message = "The token violates some mandatory constraints, details:\n";
|
||||
$message .= implode("\n", $violations);
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/** @return ConstraintViolation[] */
|
||||
public function violations(): array
|
||||
{
|
||||
return $this->violations;
|
||||
}
|
||||
}
|
||||
56
vendor/lcobucci/jwt/src/Validation/Validator.php
vendored
Normal file
56
vendor/lcobucci/jwt/src/Validation/Validator.php
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\JWT\Validation;
|
||||
|
||||
use Lcobucci\JWT\Token;
|
||||
|
||||
final class Validator implements \Lcobucci\JWT\Validator
|
||||
{
|
||||
public function assert(Token $token, Constraint ...$constraints): void
|
||||
{
|
||||
if ($constraints === []) {
|
||||
throw new NoConstraintsGiven('No constraint given.');
|
||||
}
|
||||
|
||||
$violations = [];
|
||||
|
||||
foreach ($constraints as $constraint) {
|
||||
$this->checkConstraint($constraint, $token, $violations);
|
||||
}
|
||||
|
||||
if ($violations) {
|
||||
throw RequiredConstraintsViolated::fromViolations(...$violations);
|
||||
}
|
||||
}
|
||||
|
||||
/** @param ConstraintViolation[] $violations */
|
||||
private function checkConstraint(
|
||||
Constraint $constraint,
|
||||
Token $token,
|
||||
array &$violations
|
||||
): void {
|
||||
try {
|
||||
$constraint->assert($token);
|
||||
} catch (ConstraintViolation $e) {
|
||||
$violations[] = $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function validate(Token $token, Constraint ...$constraints): bool
|
||||
{
|
||||
if ($constraints === []) {
|
||||
throw new NoConstraintsGiven('No constraint given.');
|
||||
}
|
||||
|
||||
try {
|
||||
foreach ($constraints as $constraint) {
|
||||
$constraint->assert($token);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (ConstraintViolation $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user