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

Mark all classes as readonly #1091

Open
wants to merge 4 commits into
base: 6.0.x
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ $configuration = Configuration::forSymmetricSigner(
InMemory::base64Encoded('mBC5v1sOKVvbdEitdSBenu59nfNfhwkedkJVNabosTw=')
);

$configuration->setBuilderFactory(
$configuration->withBuilderFactory(
static function (ClaimsFormatter $formatter): Builder {
// This assumes `MyCustomBuilder` is an existing class
return new MyCustomBuilder(new JoseEncoder(), $formatter);
Expand Down Expand Up @@ -165,7 +165,7 @@ $configuration = Configuration::forSymmetricSigner(
);

// This assumes `MyParser` is an existing class
$configuration->setParser(new MyParser());
$configuration->withParser(new MyParser());
```

### Validator
Expand All @@ -189,7 +189,7 @@ $configuration = Configuration::forSymmetricSigner(
);

// This assumes `MyValidator` is an existing class
$configuration->setValidator(new MyValidator());
$configuration->withValidator(new MyValidator());
```

### Validation constraints
Expand All @@ -216,7 +216,7 @@ $configuration = Configuration::forSymmetricSigner(
InMemory::base64Encoded('mBC5v1sOKVvbdEitdSBenu59nfNfhwkedkJVNabosTw=')
);

$configuration->setValidationConstraints(
$configuration->withValidationConstraints(
new SignedWith($configuration->signer(), $configuration->signingKey()),
new StrictValidAt(SystemClock::fromUTC()),
new IssuedBy('https://api.my-awesome-company.com')
Expand Down
6 changes: 3 additions & 3 deletions docs/extending-the-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use Lcobucci\JWT\Configuration;
$config = $container->get(Configuration::class);
assert($config instanceof Configuration);

$config->setBuilderFactory(
$config->withBuilderFactory(
static function (ClaimsFormatter $formatter): Builder {
return new MyCustomTokenBuilder($formatter);
}
Expand Down Expand Up @@ -99,7 +99,7 @@ use Lcobucci\JWT\Configuration;
$config = $container->get(Configuration::class);
assert($config instanceof Configuration);

$config->setParser(new MyCustomTokenParser());
$config->withParser(new MyCustomTokenParser());
```

## Signer
Expand Down Expand Up @@ -157,7 +157,7 @@ use Lcobucci\JWT\Configuration;
$config = $container->get(Configuration::class);
assert($config instanceof Configuration);

$config->setValidator(new MyCustomTokenValidator());
$config->withValidator(new MyCustomTokenValidator());
```

## Validation constraints
Expand Down
4 changes: 2 additions & 2 deletions docs/issuing-tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use Lcobucci\JWT\Token\Builder;

require 'vendor/autoload.php';

$tokenBuilder = (new Builder(new JoseEncoder(), ChainedFormatter::default()));
$tokenBuilder = Builder::new(new JoseEncoder(), ChainedFormatter::default());
$algorithm = new Sha256();
$signingKey = InMemory::plainText(random_bytes(32));

Expand Down Expand Up @@ -58,7 +58,7 @@ use Lcobucci\JWT\Token\Builder;

require 'vendor/autoload.php';

$tokenBuilder = (new Builder(new JoseEncoder(), ChainedFormatter::default()));
$tokenBuilder = Builder::new(new JoseEncoder(), ChainedFormatter::default());
$algorithm = new Sha256();
$signingKey = InMemory::plainText(random_bytes(32));

Expand Down
94 changes: 74 additions & 20 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Serves like a small DI container to simplify the creation and usage
* of the objects.
*/
final class Configuration
final readonly class Configuration

Check failure on line 18 in src/Configuration.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

Method Lcobucci\JWT\Configuration#setBuilderFactory() was removed

Check failure on line 18 in src/Configuration.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

Method Lcobucci\JWT\Configuration#setParser() was removed

Check failure on line 18 in src/Configuration.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

Method Lcobucci\JWT\Configuration#setValidator() was removed

Check failure on line 18 in src/Configuration.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

Method Lcobucci\JWT\Configuration#setValidationConstraints() was removed
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to open a discussion about this component... its design is a bit too convoluted for me.
I'll post something soon...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious

{
private Parser $parser;
private Validator $validator;
Expand All @@ -24,21 +24,29 @@
private Closure $builderFactory;

/** @var Constraint[] */
private array $validationConstraints = [];
private array $validationConstraints;

/** @param Closure(ClaimsFormatter $claimFormatter): Builder|null $builderFactory */
private function __construct(
private readonly Signer $signer,
private readonly Key $signingKey,
private readonly Key $verificationKey,
Encoder $encoder,
Decoder $decoder,
private Signer $signer,
private Key $signingKey,
private Key $verificationKey,
private Encoder $encoder,
private Decoder $decoder,
?Parser $parser,
?Validator $validator,
?Closure $builderFactory,
Constraint ...$validationConstraints,
) {
$this->parser = new Token\Parser($decoder);
$this->validator = new Validation\Validator();
$this->parser = $parser ?? new Token\Parser($decoder);
$this->validator = $validator ?? new Validation\Validator();

$this->builderFactory = static function (ClaimsFormatter $claimFormatter) use ($encoder): Builder {
return new Token\Builder($encoder, $claimFormatter);
};
$this->builderFactory = $builderFactory
?? static function (ClaimsFormatter $claimFormatter) use ($encoder): Builder {
return Token\Builder::new($encoder, $claimFormatter);
};

$this->validationConstraints = $validationConstraints;
}

public static function forAsymmetricSigner(
Expand All @@ -54,6 +62,9 @@
$verificationKey,
$encoder,
$decoder,
null,
null,
null,
);
}

Expand All @@ -69,13 +80,26 @@
$key,
$encoder,
$decoder,
null,
null,
null,
);
}

/** @param callable(ClaimsFormatter): Builder $builderFactory */
public function setBuilderFactory(callable $builderFactory): void
public function withBuilderFactory(callable $builderFactory): self
{
$this->builderFactory = $builderFactory(...);
return new self(
$this->signer,
$this->signingKey,
$this->verificationKey,
$this->encoder,
$this->decoder,
$this->parser,
$this->validator,
$builderFactory(...),
...$this->validationConstraints,
);
}

public function builder(?ClaimsFormatter $claimFormatter = null): Builder
Expand All @@ -88,9 +112,19 @@
return $this->parser;
}

public function setParser(Parser $parser): void
public function withParser(Parser $parser): self
{
$this->parser = $parser;
return new self(
$this->signer,
$this->signingKey,
$this->verificationKey,
$this->encoder,
$this->decoder,
$parser,
$this->validator,
$this->builderFactory,
...$this->validationConstraints,
);
}

public function signer(): Signer
Expand All @@ -113,9 +147,19 @@
return $this->validator;
}

public function setValidator(Validator $validator): void
public function withValidator(Validator $validator): self
{
$this->validator = $validator;
return new self(
$this->signer,
$this->signingKey,
$this->verificationKey,
$this->encoder,
$this->decoder,
$this->parser,
$validator,
$this->builderFactory,
...$this->validationConstraints,
);
}

/** @return Constraint[] */
Expand All @@ -124,8 +168,18 @@
return $this->validationConstraints;
}

public function setValidationConstraints(Constraint ...$validationConstraints): void
public function withValidationConstraints(Constraint ...$validationConstraints): self
{
$this->validationConstraints = $validationConstraints;
return new self(
$this->signer,
$this->signingKey,
$this->verificationKey,
$this->encoder,
$this->decoder,
$this->parser,
$this->validator,
$this->builderFactory,
...$validationConstraints,
);
}
}
2 changes: 1 addition & 1 deletion src/Encoding/ChainedFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use Lcobucci\JWT\ClaimsFormatter;

final class ChainedFormatter implements ClaimsFormatter
final readonly class ChainedFormatter implements ClaimsFormatter
{
/** @var array<ClaimsFormatter> */
private array $formatters;
Expand Down
2 changes: 1 addition & 1 deletion src/Encoding/JoseEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* A utilitarian class that encodes and decodes data according to JOSE specifications
*/
final class JoseEncoder implements Encoder, Decoder
final readonly class JoseEncoder implements Encoder, Decoder
{
public function jsonEncode(mixed $data): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Encoding/MicrosecondBasedDateConversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use function array_key_exists;

final class MicrosecondBasedDateConversion implements ClaimsFormatter
final readonly class MicrosecondBasedDateConversion implements ClaimsFormatter
{
/** @inheritdoc */
public function formatClaims(array $claims): array
Expand Down
2 changes: 1 addition & 1 deletion src/Encoding/UnifyAudience.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use function count;
use function current;

final class UnifyAudience implements ClaimsFormatter
final readonly class UnifyAudience implements ClaimsFormatter
{
/** @inheritdoc */
public function formatClaims(array $claims): array
Expand Down
2 changes: 1 addition & 1 deletion src/Encoding/UnixTimestampDates.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use function array_key_exists;

final class UnixTimestampDates implements ClaimsFormatter
final readonly class UnixTimestampDates implements ClaimsFormatter
{
/** @inheritdoc */
public function formatClaims(array $claims): array
Expand Down
8 changes: 4 additions & 4 deletions src/JwtFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

use function assert;

final class JwtFacade
final readonly class JwtFacade
{
private readonly Clock $clock;
private Clock $clock;

public function __construct(
private readonly Parser $parser = new Token\Parser(new JoseEncoder()),
private Parser $parser = new Token\Parser(new JoseEncoder()),
?Clock $clock = null,
) {
$this->clock = $clock ?? new class implements Clock {
Expand All @@ -38,7 +38,7 @@ public function issue(
Key $signingKey,
Closure $customiseBuilder,
): UnencryptedToken {
$builder = new Token\Builder(new JoseEncoder(), ChainedFormatter::withUnixTimestampDates());
$builder = Token\Builder::new(new JoseEncoder(), ChainedFormatter::withUnixTimestampDates());

$now = $this->clock->now();
$builder = $builder
Expand Down
2 changes: 1 addition & 1 deletion src/Signer/Blake2b.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use function sodium_crypto_generichash;
use function strlen;

final class Blake2b implements Signer
final readonly class Blake2b implements Signer
{
private const MINIMUM_KEY_LENGTH_IN_BITS = 256;

Expand Down
4 changes: 2 additions & 2 deletions src/Signer/Ecdsa.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

use const OPENSSL_KEYTYPE_EC;

abstract class Ecdsa extends OpenSSL
abstract readonly class Ecdsa extends OpenSSL
{
public function __construct(
private readonly SignatureConverter $converter = new MultibyteStringConverter(),
private SignatureConverter $converter = new MultibyteStringConverter(),
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Signer/Ecdsa/MultibyteStringConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*
* @internal
*/
final class MultibyteStringConverter implements SignatureConverter
final readonly class MultibyteStringConverter implements SignatureConverter
{
private const ASN1_SEQUENCE = '30';
private const ASN1_INTEGER = '02';
Expand Down
2 changes: 1 addition & 1 deletion src/Signer/Ecdsa/Sha256.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use const OPENSSL_ALGO_SHA256;

final class Sha256 extends Ecdsa
final readonly class Sha256 extends Ecdsa
{
public function algorithmId(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Signer/Ecdsa/Sha384.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use const OPENSSL_ALGO_SHA384;

final class Sha384 extends Ecdsa
final readonly class Sha384 extends Ecdsa
{
public function algorithmId(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Signer/Ecdsa/Sha512.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use const OPENSSL_ALGO_SHA512;

final class Sha512 extends Ecdsa
final readonly class Sha512 extends Ecdsa
{
public function algorithmId(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Signer/Eddsa.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use function sodium_crypto_sign_detached;
use function sodium_crypto_sign_verify_detached;

final class Eddsa implements Signer
final readonly class Eddsa implements Signer
{
public function algorithmId(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Signer/Hmac.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use function hash_hmac;
use function strlen;

abstract class Hmac implements Signer
abstract readonly class Hmac implements Signer
{
final public function sign(string $payload, Key $key): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Signer/Hmac/Sha256.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use Lcobucci\JWT\Signer\Hmac;

final class Sha256 extends Hmac
final readonly class Sha256 extends Hmac
{
public function algorithmId(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Signer/Hmac/Sha384.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use Lcobucci\JWT\Signer\Hmac;

final class Sha384 extends Hmac
final readonly class Sha384 extends Hmac
{
public function algorithmId(): string
{
Expand Down
Loading
Loading