Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply fixes from StyleCI #2

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Secrets/Secret.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

interface Secret
{

}
2 changes: 1 addition & 1 deletion src/Signer/Hs/Hs256Signer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ public function algorithm(): string
{
return 'HS256';
}
}
}
2 changes: 1 addition & 1 deletion src/Signer/Hs/Hs384Signer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ public function algorithm(): string
{
return 'HS384';
}
}
}
2 changes: 1 addition & 1 deletion src/Signer/Hs/Hs512Signer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ public function algorithm(): string
{
return 'HS512';
}
}
}
1 change: 0 additions & 1 deletion src/Signer/Rs/Rs384Signer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class Rs384Signer extends RsSigner
{

/**
* @var int
*/
Expand Down
1 change: 0 additions & 1 deletion src/Signer/Rs/Rs512Signer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class Rs512Signer extends RsSigner
{

/**
* @var int
*/
Expand Down
7 changes: 5 additions & 2 deletions src/Signer/Rs/RsSigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ public function sign(string $header, string $payload): mixed
$secret = $this->getSecret();
$signature = null;
$signed = openssl_sign(
"$header.$payload", $signature, $secret->privateKey, $this->openSslAlgorithm
"$header.$payload",
$signature,
$secret->privateKey,
$this->openSslAlgorithm
);

if (!$signed) {
throw new RuntimeException("Failed to create signature");
throw new RuntimeException('Failed to create signature');
}

return $signature;
Expand Down
2 changes: 1 addition & 1 deletion src/Signer/Signer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getSecret(): Secret|null
public function setSecret(Secret $secret): void
{
if (!$this->validateSecret($secret)) {
throw new InvalidArgumentException("Invalid secret provided");
throw new InvalidArgumentException('Invalid secret provided');
}

$this->secret = $secret;
Expand Down
4 changes: 3 additions & 1 deletion src/SignerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static function assign(string $algorithm, string $className): void
* @param string $algorithm
*
* @throws Exception
*
* @return Signer
*/
public static function build(string $algorithm): Signer
Expand All @@ -49,6 +50,7 @@ public static function build(string $algorithm): Signer
}

$className = self::$algorithmMap[$algorithm];
return new $className;

return new $className();
}
}
23 changes: 17 additions & 6 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

class Token
{
/**
*
*/
public const DEFAULT_SIGNER = Hs256Signer::class;

/**
Expand Down Expand Up @@ -75,6 +72,7 @@ public function __construct()
public function assignValidator(Validator $validator): Token
{
$this->validators[$validator->validates()] = $validator;

return $this;
}

Expand All @@ -85,11 +83,12 @@ private static function getDefaultSigner(): Signer
{
$signer = self::DEFAULT_SIGNER;

return new $signer;
return new $signer();
}

/**
* @throws Exception
*
* @return Token
*/
public static function fromAuthorizationBearer(): Token
Expand Down Expand Up @@ -121,7 +120,6 @@ public static function fromAuthorizationBearer(): Token
throw new Exception('Authorization header is not set');
}


$matches = [];
if (!preg_match('/Bearer\s((.*)\.(.*)\.(.*))/', $authorizationHeader, $matches)) {
throw new Exception('Invalid "Authorization" header value');
Expand All @@ -134,6 +132,7 @@ public static function fromAuthorizationBearer(): Token
* @param string $token
*
* @throws Exception
*
* @return Token
*/
public static function fromString(string $token): Token
Expand Down Expand Up @@ -177,6 +176,7 @@ private static function decodeSection(string $encoded): string
public function withSigner(Signer $signer): Token
{
$this->signer = $signer;

return $this->withHeader('alg', $signer->algorithm());
}

Expand All @@ -189,6 +189,7 @@ public function withSigner(Signer $signer): Token
public function withHeader(string $index, mixed $value): Token
{
$this->headers[$index] = $value;

return $this;
}

Expand All @@ -200,6 +201,7 @@ public function withHeader(string $index, mixed $value): Token
public function setSignature(string $signature): Token
{
$this->signature = $signature;

return $this;
}

Expand All @@ -215,6 +217,7 @@ public static function create(): Token
* @param $param
*
* @throws Exception
*
* @return Token
*/
public static function fromQueryString($param = 'token'): Token
Expand All @@ -232,6 +235,7 @@ public static function fromQueryString($param = 'token'): Token
public function assignHeaderValidator(Validator $validator): Token
{
$this->headerValidators[$validator->validates()] = $validator;

return $this;
}

Expand All @@ -257,6 +261,7 @@ public function getHeaders(string $index = null): mixed
public function setHeaders(array $headers): Token
{
$this->headers = $headers;

return $this;
}

Expand All @@ -282,6 +287,7 @@ public function getPayload(string $index = null): mixed
public function setPayload(array $payload): Token
{
$this->payload = $payload;

return $this;
}

Expand All @@ -301,6 +307,7 @@ public function getLastValidationIssue(): ?string
public function remove(string $index): Token
{
unset($this->payload[$index]);

return $this;
}

Expand All @@ -312,6 +319,7 @@ public function remove(string $index): Token
public function removeHeader(string $index): Token
{
unset($this->headers[$index]);

return $this;
}

Expand Down Expand Up @@ -350,6 +358,7 @@ public function validate(Secret $secret): bool

if (isset($this->headers[$i]) && !$validator->validate($this->headers[$i])) {
$this->lastValidationIssue = "Header '$i' value is invalid.";

return false;
}
}
Expand All @@ -360,6 +369,7 @@ public function validate(Secret $secret): bool

if (isset($this->payload[$i]) && !$validator->validate($this->payload[$i])) {
$this->lastValidationIssue = "Payload field '$i' value is invalid.";

return false;
}
}
Expand All @@ -374,7 +384,7 @@ public function validate(Secret $secret): bool
$valid = $signer->verify($headers, $payload, $this->signature);

if (!$valid) {
$this->lastValidationIssue = "Token signature is invalid.";
$this->lastValidationIssue = 'Token signature is invalid.';
}

return $valid;
Expand Down Expand Up @@ -406,6 +416,7 @@ public function sign(mixed $secret): Token
public function with(string $index, mixed $value): Token
{
$this->payload[$index] = $value;

return $this;
}
}
2 changes: 0 additions & 2 deletions src/Validator/EqualityValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Webdevcave\Jwt\Validator;

use Webdevcave\Jwt\Validator\Validator;

abstract class EqualityValidator extends Validator
{
/**
Expand Down
5 changes: 2 additions & 3 deletions src/Validator/ExpValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@

class ExpValidator extends Validator
{

/**
* @inheritDoc
*/
function validates(): string
public function validates(): string
{
return 'exp';
}

/**
* @inheritDoc
*/
function validate(mixed $value): bool
public function validate(mixed $value): bool
{
return $value > time();
}
Expand Down
7 changes: 3 additions & 4 deletions src/Validator/NbfValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@

class NbfValidator extends Validator
{

/**
* @inheritDoc
*/
function validates(): string
public function validates(): string
{
return 'nbf';
}

/**
* @inheritDoc
*/
function validate(mixed $value): bool
public function validate(mixed $value): bool
{
return $value <= time();
}
}
}
2 changes: 1 addition & 1 deletion src/Validator/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
abstract class Validator
{
/**
* Must return the validated field name;
* Must return the validated field name;.
*
* @return string
*/
Expand Down
4 changes: 1 addition & 3 deletions tests/Signer/Rs/RsSignerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
namespace Webdevcave\Jwt\Tests\Signer\Rs;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Webdevcave\Jwt\Secrets\RsSecret;
use Webdevcave\Jwt\Signer\Rs\Rs256Signer;
use Webdevcave\Jwt\Signer\Rs\RsSigner;
use PHPUnit\Framework\TestCase;
use Webdevcave\Jwt\Signer\Signer;
use Webdevcave\Jwt\SignerFactory;
use Webdevcave\Jwt\Token;

#[CoversClass(RsSecret::class)]
#[CoversClass(RsSigner::class)]
Expand Down
2 changes: 1 addition & 1 deletion tests/SignerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use Exception;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Webdevcave\Jwt\Signer\Hs\Hs256Signer;
use Webdevcave\Jwt\Signer\Hs\Hs384Signer;
use Webdevcave\Jwt\Signer\Hs\Hs512Signer;
use Webdevcave\Jwt\Signer\Rs\Rs256Signer;
use Webdevcave\Jwt\Signer\Rs\Rs384Signer;
use Webdevcave\Jwt\Signer\Rs\Rs512Signer;
use Webdevcave\Jwt\SignerFactory;
use PHPUnit\Framework\TestCase;
use Webdevcave\Jwt\Tests\CustomSigner\MyCustomSigner;

#[CoversClass(SignerFactory::class)]
Expand Down
2 changes: 1 addition & 1 deletion tests/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Webdevcave\Jwt\Secrets\HsSecret;
use Webdevcave\Jwt\Secrets\RsSecret;
use Webdevcave\Jwt\Signer\Hs\Hs256Signer;
Expand All @@ -17,7 +18,6 @@
use Webdevcave\Jwt\Signer\Signer;
use Webdevcave\Jwt\SignerFactory;
use Webdevcave\Jwt\Token;
use PHPUnit\Framework\TestCase;
use Webdevcave\Jwt\Validator\AudValidator;
use Webdevcave\Jwt\Validator\EqualityValidator;
use Webdevcave\Jwt\Validator\ExpValidator;
Expand Down
5 changes: 2 additions & 3 deletions tests/Validator/IssValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Webdevcave\Jwt\Validator\IssValidator;
use Webdevcave\Jwt\Validator\EqualityValidator;
use Webdevcave\Jwt\Validator\IssValidator;

#[CoversClass(IssValidator::class)]
#[CoversClass(EqualityValidator::class)]
Expand All @@ -14,10 +14,9 @@ class IssValidatorTest extends TestCase
public function testValidate()
{
$validator = new IssValidator('issuer');

$this->assertEquals('iss', $validator->validates());
$this->assertTrue($validator->validate('issuer'));
$this->assertFalse($validator->validate('not-issuer'));
}

}
5 changes: 2 additions & 3 deletions tests/Validator/SubValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Webdevcave\Jwt\Validator\SubValidator;
use Webdevcave\Jwt\Validator\EqualityValidator;
use Webdevcave\Jwt\Validator\SubValidator;

#[CoversClass(SubValidator::class)]
#[CoversClass(EqualityValidator::class)]
Expand All @@ -14,10 +14,9 @@ class SubValidatorTest extends TestCase
public function testValidate()
{
$validator = new SubValidator('sub');

$this->assertEquals('sub', $validator->validates());
$this->assertTrue($validator->validate('sub'));
$this->assertFalse($validator->validate('not-sub'));
}

}
Loading