Skip to content

Commit

Permalink
Merge pull request #538 from lcobucci/rename-getters
Browse files Browse the repository at this point in the history
Rename getters
  • Loading branch information
lcobucci authored Nov 20, 2020
2 parents cd011df + aad6449 commit 51fee47
Show file tree
Hide file tree
Showing 45 changed files with 274 additions and 274 deletions.
14 changes: 7 additions & 7 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ Once you've made all the necessary configuration you can pass the configuration

These are the available getters:

* `Lcobucci\JWT\Configuration#createBuilder()`: retrieves the token builder (always creating a new instance)
* `Lcobucci\JWT\Configuration#getParser()`: retrieves the token parser
* `Lcobucci\JWT\Configuration#getSigner()`: retrieves the signer
* `Lcobucci\JWT\Configuration#getSigningKey()`: retrieves the key for signature creation
* `Lcobucci\JWT\Configuration#getVerificationKey()`: retrieves the key for signature verification
* `Lcobucci\JWT\Configuration#getValidator()`: retrieves the token validator
* `Lcobucci\JWT\Configuration#getValidationConstraints()`: retrieves the default set of validation constraints
* `Lcobucci\JWT\Configuration#builder()`: retrieves the token builder (always creating a new instance)
* `Lcobucci\JWT\Configuration#parser()`: retrieves the token parser
* `Lcobucci\JWT\Configuration#signer()`: retrieves the signer
* `Lcobucci\JWT\Configuration#signingKey()`: retrieves the key for signature creation
* `Lcobucci\JWT\Configuration#verificationKey()`: retrieves the key for signature verification
* `Lcobucci\JWT\Configuration#validator()`: retrieves the token validator
* `Lcobucci\JWT\Configuration#validationConstraints()`: retrieves the default set of validation constraints
2 changes: 1 addition & 1 deletion docs/extending-the-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ final class UnixTimestampDates implements ClaimsFormatter
$config = $container->get(Configuration::class);
assert($config instanceof Configuration);

$config->createBuilder(new UnixTimestampDates());
$config->builder(new UnixTimestampDates());
```

The class `Lcobucci\JWT\Encoding\ChainedFormatter` allows for users to combine multiple formatters.
Expand Down
8 changes: 4 additions & 4 deletions docs/issuing-tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $config = $container->get(Configuration::class);
assert($config instanceof Configuration);

$now = new DateTimeImmutable();
$token = $config->createBuilder()
$token = $config->builder()
// Configures the issuer (iss claim)
->issuedBy('http://example.com')
// Configures the audience (aud claim)
Expand All @@ -31,7 +31,7 @@ $token = $config->createBuilder()
// Configures a new header, called "foo"
->withHeader('foo', 'bar')
// Builds a new token
->getToken($config->getSigner(), $config->getSigningKey());
->getToken($config->signer(), $config->signingKey());
```

Once you've created a token, you're able to retrieve its data and convert it to its string representation:
Expand All @@ -42,11 +42,11 @@ use Lcobucci\JWT\Configuration;
$config = $container->get(Configuration::class);
assert($config instanceof Configuration);

$token = $config->createBuilder()
$token = $config->builder()
->issuedBy('http://example.com')
->withClaim('uid', 1)
->withHeader('foo', 'bar')
->getToken($config->getSigner(), $config->getSigningKey());
->getToken($config->signer(), $config->signingKey());

$token->headers(); // Retrieves the token headers
$token->claims(); // Retrieves the token claims
Expand Down
2 changes: 1 addition & 1 deletion docs/parsing-tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use Lcobucci\JWT\Token\Plain;
$config = $container->get(Configuration::class);
assert($config instanceof Configuration);

$token = $config->getParser()->parse(
$token = $config->parser()->parse(
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.'
. 'eyJzdWIiOiIxMjM0NTY3ODkwIn0.'
. '2gSBz9EOsQRN9I-3iSxJoFt7NtgV6Rm0IL6a8CAwl3Q'
Expand Down
12 changes: 6 additions & 6 deletions docs/validating-tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
$token = $container->get(Configuration::class);
assert($config instanceof Configuration);

$token = $config->getParser()->parse('...');
$token = $config->parser()->parse('...');
assert($token instanceof Plain);

$constraints = $config->getValidationConstraints();
$constraints = $config->validationConstraints();

try {
$config->getValidator()->assert($token, ...$constraints);
$config->validator()->assert($token, ...$constraints);
} catch (RequiredConstraintsViolated $e) {
// list of constraints violation exceptions:
var_dump($e->violations());
Expand All @@ -48,12 +48,12 @@ use Lcobucci\JWT\Token\Plain;
$token = $container->get(Configuration::class);
assert($config instanceof Configuration);

$token = $config->getParser()->parse('...');
$token = $config->parser()->parse('...');
assert($token instanceof Plain);

$constraints = $config->getValidationConstraints();
$constraints = $config->validationConstraints();

if (! $config->getValidator()->validate($token, ...$constraints)) {
if (! $config->validator()->validate($token, ...$constraints)) {
throw new RuntimeException('No way!');
}
```
Expand Down
14 changes: 7 additions & 7 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ public function setBuilderFactory(callable $builderFactory): void
$this->builderFactory = Closure::fromCallable($builderFactory);
}

public function createBuilder(?ClaimsFormatter $claimFormatter = null): Builder
public function builder(?ClaimsFormatter $claimFormatter = null): Builder
{
return ($this->builderFactory)($claimFormatter ?? ChainedFormatter::default());
}

public function getParser(): Parser
public function parser(): Parser
{
return $this->parser;
}
Expand All @@ -116,22 +116,22 @@ public function setParser(Parser $parser): void
$this->parser = $parser;
}

public function getSigner(): Signer
public function signer(): Signer
{
return $this->signer;
}

public function getSigningKey(): Key
public function signingKey(): Key
{
return $this->signingKey;
}

public function getVerificationKey(): Key
public function verificationKey(): Key
{
return $this->verificationKey;
}

public function getValidator(): Validator
public function validator(): Validator
{
return $this->validator;
}
Expand All @@ -142,7 +142,7 @@ public function setValidator(Validator $validator): void
}

/** @return Constraint[] */
public function getValidationConstraints(): array
public function validationConstraints(): array
{
return $this->validationConstraints;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Signer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface Signer
/**
* Returns the algorithm id
*/
public function getAlgorithmId(): string;
public function algorithmId(): string;

/**
* Creates a hash for the given payload
Expand Down
8 changes: 4 additions & 4 deletions src/Signer/Ecdsa.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ final public function sign(string $payload, Key $key): string
{
return $this->converter->fromAsn1(
$this->createSignature($key->contents(), $key->passphrase(), $payload),
$this->getKeyLength()
$this->keyLength()
);
}

final public function verify(string $expected, string $payload, Key $key): bool
{
return $this->verifySignature(
$this->converter->toAsn1($expected, $this->getKeyLength()),
$this->converter->toAsn1($expected, $this->keyLength()),
$payload,
$key->contents()
);
}

final public function getKeyType(): int
final public function keyType(): int
{
return OPENSSL_KEYTYPE_EC;
}
Expand All @@ -49,5 +49,5 @@ final public function getKeyType(): int
*
* @internal
*/
abstract public function getKeyLength(): int;
abstract public function keyLength(): int;
}
6 changes: 3 additions & 3 deletions src/Signer/Ecdsa/Sha256.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

final class Sha256 extends Ecdsa
{
public function getAlgorithmId(): string
public function algorithmId(): string
{
return 'ES256';
}

public function getAlgorithm(): int
public function algorithm(): int
{
return OPENSSL_ALGO_SHA256;
}

public function getKeyLength(): int
public function keyLength(): int
{
return 64;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Signer/Ecdsa/Sha384.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

final class Sha384 extends Ecdsa
{
public function getAlgorithmId(): string
public function algorithmId(): string
{
return 'ES384';
}

public function getAlgorithm(): int
public function algorithm(): int
{
return OPENSSL_ALGO_SHA384;
}

public function getKeyLength(): int
public function keyLength(): int
{
return 96;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Signer/Ecdsa/Sha512.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

final class Sha512 extends Ecdsa
{
public function getAlgorithmId(): string
public function algorithmId(): string
{
return 'ES512';
}

public function getAlgorithm(): int
public function algorithm(): int
{
return OPENSSL_ALGO_SHA512;
}

public function getKeyLength(): int
public function keyLength(): int
{
return 132;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Signer/Hmac.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ abstract class Hmac implements Signer
{
final public function sign(string $payload, Key $key): string
{
return hash_hmac($this->getAlgorithm(), $payload, $key->contents(), true);
return hash_hmac($this->algorithm(), $payload, $key->contents(), true);
}

final public function verify(string $expected, string $payload, Key $key): bool
{
return hash_equals($expected, $this->sign($payload, $key));
}

abstract public function getAlgorithm(): string;
abstract public function algorithm(): string;
}
4 changes: 2 additions & 2 deletions src/Signer/Hmac/Sha256.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

final class Sha256 extends Hmac
{
public function getAlgorithmId(): string
public function algorithmId(): string
{
return 'HS256';
}

public function getAlgorithm(): string
public function algorithm(): string
{
return 'sha256';
}
Expand Down
4 changes: 2 additions & 2 deletions src/Signer/Hmac/Sha384.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

final class Sha384 extends Hmac
{
public function getAlgorithmId(): string
public function algorithmId(): string
{
return 'HS384';
}

public function getAlgorithm(): string
public function algorithm(): string
{
return 'sha384';
}
Expand Down
4 changes: 2 additions & 2 deletions src/Signer/Hmac/Sha512.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

final class Sha512 extends Hmac
{
public function getAlgorithmId(): string
public function algorithmId(): string
{
return 'HS512';
}

public function getAlgorithm(): string
public function algorithm(): string
{
return 'sha512';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Signer/None.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

final class None implements Signer
{
public function getAlgorithmId(): string
public function algorithmId(): string
{
return 'none';
}
Expand Down
10 changes: 5 additions & 5 deletions src/Signer/OpenSSL.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final protected function createSignature(
try {
$signature = '';

if (! openssl_sign($payload, $signature, $key, $this->getAlgorithm())) {
if (! openssl_sign($payload, $signature, $key, $this->algorithm())) {
$error = openssl_error_string();
assert(is_string($error));

Expand Down Expand Up @@ -68,7 +68,7 @@ final protected function verifySignature(
string $pem
): bool {
$key = $this->getPublicKey($pem);
$result = openssl_verify($payload, $expected, $key, $this->getAlgorithm());
$result = openssl_verify($payload, $expected, $key, $this->algorithm());
$this->freeKey($key);

return $result === 1;
Expand Down Expand Up @@ -106,7 +106,7 @@ private function validateKey($key): void
$details = openssl_pkey_get_details($key);
assert(is_array($details));

if (! array_key_exists('key', $details) || $details['type'] !== $this->getKeyType()) {
if (! array_key_exists('key', $details) || $details['type'] !== $this->keyType()) {
throw InvalidKeyProvided::incompatibleKey();
}
}
Expand All @@ -126,12 +126,12 @@ private function freeKey($key): void
*
* @internal
*/
abstract public function getKeyType(): int;
abstract public function keyType(): int;

/**
* Returns which algorithm to be used to create/verify the signature (using OpenSSL constants)
*
* @internal
*/
abstract public function getAlgorithm(): int;
abstract public function algorithm(): int;
}
2 changes: 1 addition & 1 deletion src/Signer/Rsa.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final public function verify(string $expected, string $payload, Key $key): bool
return $this->verifySignature($expected, $payload, $key->contents());
}

final public function getKeyType(): int
final public function keyType(): int
{
return OPENSSL_KEYTYPE_RSA;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Signer/Rsa/Sha256.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

final class Sha256 extends Rsa
{
public function getAlgorithmId(): string
public function algorithmId(): string
{
return 'RS256';
}

public function getAlgorithm(): int
public function algorithm(): int
{
return OPENSSL_ALGO_SHA256;
}
Expand Down
Loading

0 comments on commit 51fee47

Please sign in to comment.