Skip to content

Commit

Permalink
Applied Laravel Pint changes
Browse files Browse the repository at this point in the history
  • Loading branch information
little-apps authored and github-actions[bot] committed Jun 1, 2024
1 parent f4f5f9b commit 569c2a2
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 75 deletions.
1 change: 0 additions & 1 deletion src/Core/Concerns/HandlesCreateSigned.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace LittleApps\LittleJWT\Core\Concerns;

use LittleApps\LittleJWT\Build\Sign;
use LittleApps\LittleJWT\Factories\JWTHasher;

trait HandlesCreateSigned
Expand Down
2 changes: 0 additions & 2 deletions src/Core/Concerns/HandlesCreateUnsigned.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
use LittleApps\LittleJWT\Build\Build;
use LittleApps\LittleJWT\Build\Builder;
use LittleApps\LittleJWT\Factories\JWTBuilder;
use LittleApps\LittleJWT\Factories\JWTHasher;
use LittleApps\LittleJWT\JWT\JsonWebToken;
use LittleApps\LittleJWT\JWT\SignedJsonWebToken;

trait HandlesCreateUnsigned
{
Expand Down
24 changes: 14 additions & 10 deletions src/Factories/AlgorithmBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace LittleApps\LittleJWT\Factories;

use Jose\Component\Core\Algorithm as AlgorithmContract;
use Jose\Component\Signature\Algorithm as JoseAlgorithms;
use LittleApps\LittleJWT\Exceptions\HashAlgorithmNotFoundException;
use LittleApps\LittleJWT\Exceptions\InvalidHashAlgorithmException;
use Jose\Component\Core\Algorithm as AlgorithmContract;

class AlgorithmBuilder
{
Expand Down Expand Up @@ -41,7 +41,8 @@ class AlgorithmBuilder
*
* @return array<string, class-string<\Jose\Component\Core\Algorithm>>
*/
public static function getAlgorithmMappings(): array {
public static function getAlgorithmMappings(): array
{
return static::$algorithmMappings;
}

Expand All @@ -50,7 +51,8 @@ public static function getAlgorithmMappings(): array {
*
* @return list<string>
*/
public static function getSupportedAlgorithmIdentifiers(): array {
public static function getSupportedAlgorithmIdentifiers(): array
{
return array_keys(static::$algorithmMappings);
}

Expand All @@ -59,28 +61,30 @@ public static function getSupportedAlgorithmIdentifiers(): array {
*
* @return list<class-string<\Jose\Component\Core\Algorithm>>
*/
public static function getSupportedAlgorithmClasses(): array {
public static function getSupportedAlgorithmClasses(): array
{
return array_values(static::$algorithmMappings);
}

/**
* Gets algorithm class from algorithm identifier.
*
* @param string $identifier Algorithm identifier (ex: 'HS256')
* @param mixed $default Returned if identifier doesn't exist.
* @param string $identifier Algorithm identifier (ex: 'HS256')
* @param mixed $default Returned if identifier doesn't exist.
* @return class-string<\Jose\Component\Core\Algorithm>|mixed
*/
public static function getAlgorithmClass(string $identifier, $default = null) {
public static function getAlgorithmClass(string $identifier, $default = null)
{
return static::$algorithmMappings[$identifier] ?? $default;
}

/**
* Builds Algorithm instance.
*
* @param string $identifier Algorithm identifier (ex: 'HS256')
* @return AlgorithmContract
* @param string $identifier Algorithm identifier (ex: 'HS256')
*/
public static function build(string $identifier): AlgorithmContract {
public static function build(string $identifier): AlgorithmContract
{
$alg = strtoupper($identifier);

$class = static::getAlgorithmClass($alg);
Expand Down
2 changes: 0 additions & 2 deletions src/Factories/JWTBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
namespace LittleApps\LittleJWT\Factories;

use Illuminate\Support\Str;
use LittleApps\LittleJWT\Build\Sign;
use LittleApps\LittleJWT\Exceptions\CantParseJWTException;
use LittleApps\LittleJWT\JWT\ClaimManager;
use LittleApps\LittleJWT\JWT\ClaimManagers;
use LittleApps\LittleJWT\JWT\JsonWebToken;
use LittleApps\LittleJWT\JWT\SignedJsonWebToken;
use LittleApps\LittleJWT\Utils\Base64Encoder;
Expand Down
10 changes: 5 additions & 5 deletions src/Factories/JWTHasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ class JWTHasher
/**
* Signs a JSON Web Token
*
* @param JsonWebToken $jsonWebToken
* @param JsonWebKey $jsonWebKey
* @return SignedJsonWebToken
*/
public static function sign(JsonWebToken $jsonWebToken, JsonWebKey $jsonWebKey) {
public static function sign(JsonWebToken $jsonWebToken, JsonWebKey $jsonWebKey)
{
$algorithm = $jsonWebKey->algorithm();
$signature = JWTHasher::hash($algorithm, $jsonWebKey, $jsonWebToken->getHeaders(), $jsonWebToken->getPayload());

Expand Down Expand Up @@ -70,10 +69,11 @@ public static function hash(AlgorithmContract $algorithm, JsonWebKey $jwk, Claim
return $algorithm->sign($jwk, $input);
}
} catch (InvalidArgumentException $e) {
if ($e->getMessage() === 'Wrong key type.')
if ($e->getMessage() === 'Wrong key type.') {
throw new IncompatibleHashAlgorithmJWK($e);
else
} else {
throw $e;
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Factories/KeyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public static function buildFromConfig(array $config)
/**
* Builds a JWK to use to sign/verify JWTs
*
* @param string $keyType Key type (one of KEY_* constants).
* @param array $options Options to build key type.
* @param array $extra Any extra values to include in JWK.
* @param string $keyType Key type (one of KEY_* constants).
* @param array $options Options to build key type.
* @param array $extra Any extra values to include in JWK.
* @return JsonWebKey
*
* @throws InvalidJWKException Thrown if JWK is invalid.
Expand Down
21 changes: 9 additions & 12 deletions src/Factories/LittleJWTBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,34 @@
namespace LittleApps\LittleJWT\Factories;

use LittleApps\LittleJWT\JWK\JsonWebKey;
use LittleApps\LittleJWT\LittleJWT;
use LittleApps\LittleJWT\JWK\JWKValidator;
use LittleApps\LittleJWT\LittleJWT;

class LittleJWTBuilder
{
/**
* JWKValidator to use
*
* @var JWKValidator|null
*/
protected ?JWKValidator $jwkValidator;

/**
* Initializes LittleJWTBuilder instance
*
* @param JsonWebKey $jwk JWK to use with LittleJWT instance.
* @param JsonWebKey $jwk JWK to use with LittleJWT instance.
*/
public function __construct(
protected readonly JsonWebKey $jwk
)
{
) {

}

/**
* Specifies JWKValidator to use before building LittleJWT.
*
* @param JWKValidator $jwkValidator
* @return $this
*/
public function withJwkValidator(JWKValidator $jwkValidator) {
public function withJwkValidator(JWKValidator $jwkValidator)
{
$this->jwkValidator = $jwkValidator;

return $this;
Expand All @@ -44,18 +41,18 @@ public function withJwkValidator(JWKValidator $jwkValidator) {
*
* @return $this
*/
public function withoutJwkValidator() {
public function withoutJwkValidator()
{
$this->jwkValidator = null;

return $this;
}

/**
* Builds LittleJWT instance
*
* @return LittleJWT
*/
public function build(): LittleJWT {
public function build(): LittleJWT
{
$jwk = isset($this->jwkValidator) ? $this->jwkValidator->__invoke($this->jwk) : $this->jwk;

return new LittleJWT(app(), $jwk);
Expand Down
63 changes: 37 additions & 26 deletions src/JWK/JWKValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class JWKValidator

/**
* Initializes JWKValidator instance
*
*/
public function __construct()
{
Expand All @@ -32,16 +31,18 @@ public function __construct()
/**
* Sets fallback
*
* @param callable(): static $fallback
* @param callable(): static $fallback
* @return void
*/
public function withFallback(callable $fallback) {
public function withFallback(callable $fallback)
{
$this->fallback = $fallback;

return $this;
}

public function withoutFallback() {
public function withoutFallback()
{
$this->fallback = null;

return $this;
Expand All @@ -51,6 +52,7 @@ public function withoutFallback() {
* Validates the JSON Web Key
*
* @return JsonWebKey
*
* @throws InvalidJWKException Thrown if JWK is invalid and fallback is not set.
*/
public function __invoke(JsonWebKey $jsonWebKey)
Expand All @@ -60,73 +62,78 @@ public function __invoke(JsonWebKey $jsonWebKey)

return $jsonWebKey;
} catch (InvalidJWKException $ex) {
if (isset($this->fallback))
if (isset($this->fallback)) {
return call_user_func($this->fallback);
else
} else {
throw $ex;
}
}
}

/**
* Performs the validation
*
* @return void
*
* @throws InvalidJWKException Thrown if JWK is invalid.
*/
protected function perform(JsonWebKey $jsonWebKey) {
protected function perform(JsonWebKey $jsonWebKey)
{
$values = $jsonWebKey->all();

if (!isset($values['alg'])) {
if (! isset($values['alg'])) {
throw new InvalidJWKException("The 'alg' value is missing.");
}

if (!isset($values['kty'])) {
if (! isset($values['kty'])) {
throw new InvalidJWKException("The 'kty' value is missing.");
}

$this->validateAlgorithm($jsonWebKey);

$algorithm = $this->getAlgorithm($jsonWebKey);

if ($algorithm instanceof HMAC)
if ($algorithm instanceof HMAC) {
$this->validateHmacKey($jsonWebKey);
else if ($algorithm instanceof ECDSA)
} elseif ($algorithm instanceof ECDSA) {
$this->validateEcdsaKey($jsonWebKey);
else if ($algorithm instanceof RSAPSS || $algorithm instanceof RSAPKCS1)
} elseif ($algorithm instanceof RSAPSS || $algorithm instanceof RSAPKCS1) {
$this->validateRsaKey($jsonWebKey);
}
}

/**
* Checks if algorithm is supported.
*
* @param JsonWebKey $jsonWebKey
* @return void
*/
protected function validateAlgorithm(JsonWebKey $jsonWebKey) {
protected function validateAlgorithm(JsonWebKey $jsonWebKey)
{
$alg = $jsonWebKey->get('alg');

$class = AlgorithmBuilder::getAlgorithmClass($alg);

if (is_null($class))
if (is_null($class)) {
throw new InvalidJWKException("JSON Web Key algorithm '{$alg}' is not supported.");
}

try {
$jsonWebKey->algorithm();
} catch (InvalidHashAlgorithmException | HashAlgorithmNotFoundException $ex) {
} catch (InvalidHashAlgorithmException|HashAlgorithmNotFoundException $ex) {
throw new InvalidJWKException($ex->getMessage());
}
}

/**
* Gets algorithm instance from JWK
*
* @param JsonWebKey $jsonWebKey
* @return \Jose\Component\Core\Algorithm
*/
protected function getAlgorithm(JsonWebKey $jsonWebKey) {
protected function getAlgorithm(JsonWebKey $jsonWebKey)
{
try {
return $jsonWebKey->algorithm();
} catch (InvalidHashAlgorithmException | HashAlgorithmNotFoundException $ex) {
} catch (InvalidHashAlgorithmException|HashAlgorithmNotFoundException $ex) {
throw new InvalidJWKException($ex->getMessage());
}
}
Expand All @@ -136,7 +143,8 @@ protected function getAlgorithm(JsonWebKey $jsonWebKey) {
*
* @return void
*/
protected function validateHmacKey(JsonWebKey $jsonWebKey) {
protected function validateHmacKey(JsonWebKey $jsonWebKey)
{
static $minKeyLengths = [
'HS256' => 32,
'HS384' => 48,
Expand All @@ -146,20 +154,22 @@ protected function validateHmacKey(JsonWebKey $jsonWebKey) {
$alg = strtoupper($jsonWebKey->get('alg'));
$key = $jsonWebKey->get('k') ?? '';

if (mb_strlen($key, '8bit') < $minKeyLengths[$alg])
throw new InvalidJWKException("The key is not long enough.");
if (mb_strlen($key, '8bit') < $minKeyLengths[$alg]) {
throw new InvalidJWKException('The key is not long enough.');
}
}

/**
* Validates an ECDSA key.
*
* @return void
*/
protected function validateEcdsaKey(JsonWebKey $jsonWebKey) {
protected function validateEcdsaKey(JsonWebKey $jsonWebKey)
{
$required = ['x', 'y', 'crv'];

foreach ($required as $key) {
if (!$jsonWebKey->has($key)) {
if (! $jsonWebKey->has($key)) {
throw new InvalidJWKException("The '{$key}' key is required in ECDSA JSON Web Keys.");
}
}
Expand All @@ -170,11 +180,12 @@ protected function validateEcdsaKey(JsonWebKey $jsonWebKey) {
*
* @return void
*/
protected function validateRsaKey(JsonWebKey $jsonWebKey) {
protected function validateRsaKey(JsonWebKey $jsonWebKey)
{
$required = ['n', 'e'];

foreach ($required as $key) {
if (!$jsonWebKey->has($key)) {
if (! $jsonWebKey->has($key)) {
throw new InvalidJWKException("The '{$key}' key is required in RSA JSON Web Keys.");
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/JWK/JsonWebKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
namespace LittleApps\LittleJWT\JWK;

use Jose\Component\Core\JWK;
use Jose\Component\Signature\Algorithm as JoseAlgorithms;
use LittleApps\LittleJWT\Exceptions\HashAlgorithmNotFoundException;
use LittleApps\LittleJWT\Exceptions\InvalidHashAlgorithmException;
use LittleApps\LittleJWT\Factories\AlgorithmBuilder;

class JsonWebKey extends JWK
Expand Down
Loading

0 comments on commit 569c2a2

Please sign in to comment.