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

198: Add CS checking to build #199

Merged
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: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ composer.lock
humbuglog.txt
coverage
phpcs.xml
/.phpcs.cache
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ jobs:
script:
- ./vendor/bin/phpbench run -l dots --report aggregate

- stage: Code-style
script:
- ./vendor/bin/phpcs

# - stage: Mutation
# before_script:
# - mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{.disabled,}
Expand Down
29 changes: 23 additions & 6 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
<?xml version="1.0"?>
<ruleset name="JWT style">
<description>The coding standard for lcobucci/jwt.</description>

<arg name="basepath" value="."/>
<arg name="extensions" value="php"/>
<ruleset>
<arg name="basepath" value="." />
<arg name="extensions" value="php" />
<arg name="parallel" value="80" />
<arg name="colors" />
<arg value="np"/>
<arg name="cache" value=".phpcs.cache" />
<arg value="p" />

<file>src</file>
<file>test</file>

<rule ref="PSR2"/>

<rule ref="Generic.Formatting.MultipleStatementAlignment">
<properties>
<property name="error" value="true"/>
</properties>
</rule>

<rule ref="Generic.Formatting.SpaceAfterCast" />
<rule ref="Generic.Formatting.SpaceAfterNot" />
<rule ref="Generic.Arrays.DisallowLongArraySyntax" />

<rule ref="Squiz.Strings.ConcatenationSpacing">
<properties>
<property name="spacing" value="1"/>
<property name="ignoreNewlines" value="true"/>
</properties>
</rule>
</ruleset>

4 changes: 2 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ private function __construct(
Key $signingKey,
Key $verificationKey
) {
$this->signer = $signer;
$this->signingKey = $signingKey;
$this->signer = $signer;
$this->signingKey = $signingKey;
$this->verificationKey = $verificationKey;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Signer/Ecdsa.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function create(): Ecdsa

public function __construct(EccAdapter $adapter, KeyParser $keyParser)
{
$this->adapter = $adapter;
$this->adapter = $adapter;
$this->keyParser = $keyParser;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Signer/Ecdsa/EccAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public function __construct(
SignatureSerializer $serializer,
RandomNumberGeneratorInterface $numberGenerator
) {
$this->signer = $signer;
$this->nistCurve = $nistCurve;
$this->serializer = $serializer;
$this->signer = $signer;
$this->nistCurve = $nistCurve;
$this->serializer = $serializer;
$this->numberGenerator = $numberGenerator;
}

Expand Down Expand Up @@ -118,7 +118,7 @@ public function createSigningHash(

private function generatorPoint(string $algorithm): GeneratorPoint
{
if (!array_key_exists($algorithm, self::GENERATOR_POINTS)) {
if (! array_key_exists($algorithm, self::GENERATOR_POINTS)) {
throw new InvalidArgumentException('Unknown algorithm');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Signer/Ecdsa/KeyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __construct(
PrivateKeySerializerInterface $privateKeySerializer,
PublicKeySerializerInterface $publicKeySerializer
) {
$this->publicKeySerializer = $publicKeySerializer;
$this->publicKeySerializer = $publicKeySerializer;
$this->privateKeySerializer = $privateKeySerializer;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Signer/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private function readFile(string $content): string
{
$file = substr($content, 7);

if (!is_readable($file)) {
if (! is_readable($file)) {
throw new \InvalidArgumentException('You must inform a valid key file');
}

Expand Down
4 changes: 2 additions & 2 deletions src/Signer/Rsa.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final public function sign(string $payload, Key $key): string

$signature = '';

if (!openssl_sign($payload, $signature, $key, $this->getAlgorithm())) {
if (! openssl_sign($payload, $signature, $key, $this->getAlgorithm())) {
throw new InvalidArgumentException(
'There was an error while creating the signature: ' . openssl_error_string()
);
Expand Down Expand Up @@ -67,7 +67,7 @@ private function validateKey($key): void

$details = openssl_pkey_get_details($key);

if (!isset($details['key']) || $details['type'] !== OPENSSL_KEYTYPE_RSA) {
if (! isset($details['key']) || $details['type'] !== OPENSSL_KEYTYPE_RSA) {
throw new InvalidArgumentException('This key is not compatible with RSA signatures');
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Token/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function permittedFor(string $audience): BuilderInterface
{
$audiences = $this->claims[RegisteredClaims::AUDIENCE] ?? [];

if (!in_array($audience, $audiences)) {
if (! in_array($audience, $audiences)) {
$audiences[] = $audience;
}

Expand Down Expand Up @@ -155,13 +155,13 @@ private function encode(array $items): string
*/
public function getToken(Signer $signer, Key $key): Plain
{
$headers = $this->headers;
$headers = $this->headers;
$headers['alg'] = $signer->getAlgorithmId();

$encodedHeaders = $this->encode($headers);
$encodedClaims = $this->encode($this->formatClaims($this->claims));
$encodedClaims = $this->encode($this->formatClaims($this->claims));

$signature = $signer->sign($encodedHeaders . '.' . $encodedClaims, $key);
$signature = $signer->sign($encodedHeaders . '.' . $encodedClaims, $key);
$encodedSignature = $this->encoder->base64UrlEncode($signature);

return new Plain(
Expand All @@ -173,7 +173,7 @@ public function getToken(Signer $signer, Key $key): Plain

private function formatClaims(array $claims): array
{
if (isset($claims[RegisteredClaims::AUDIENCE][0]) && !isset($claims[RegisteredClaims::AUDIENCE][1])) {
if (isset($claims[RegisteredClaims::AUDIENCE][0]) && ! isset($claims[RegisteredClaims::AUDIENCE][1])) {
$claims[RegisteredClaims::AUDIENCE] = $claims[RegisteredClaims::AUDIENCE][0];
}

Expand All @@ -189,7 +189,7 @@ private function formatClaims(array $claims): array
*/
private function convertDate(DateTimeImmutable $date)
{
$seconds = $date->format('U');
$seconds = $date->format('U');
$microseconds = $date->format('u');

if ((int) $microseconds === 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/Token/DataSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class DataSet

public function __construct(array $data, string $encoded)
{
$this->data = $data;
$this->data = $data;
$this->encoded = $encoded;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Token/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private function convertDate(string $value): DateTimeImmutable
*/
private function parseSignature(array $header, string $data): Signature
{
if ($data === '' || !isset($header['alg']) || $header['alg'] === 'none') {
if ($data === '' || ! isset($header['alg']) || $header['alg'] === 'none') {
return Signature::fromEmptyData();
}

Expand Down
6 changes: 3 additions & 3 deletions src/Token/Plain.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function __construct(
DataSet $claims,
Signature $signature
) {
$this->headers = $headers;
$this->claims = $claims;
$this->headers = $headers;
$this->claims = $claims;
$this->signature = $signature;
}

Expand Down Expand Up @@ -136,7 +136,7 @@ public function isMinimumTimeBefore(DateTimeInterface $now): bool
*/
public function isExpired(DateTimeInterface $now): bool
{
if (!$this->claims->has(RegisteredClaims::EXPIRATION_TIME)) {
if (! $this->claims->has(RegisteredClaims::EXPIRATION_TIME)) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Token/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function fromEmptyData(): self

public function __construct(string $hash, string $encoded)
{
$this->hash = $hash;
$this->hash = $hash;
$this->encoded = $encoded;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Validation/Constraint/IdentifiedBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(string $id)
*/
public function assert(Token $token): void
{
if (!$token->isIdentifiedBy($this->id)) {
if (! $token->isIdentifiedBy($this->id)) {
throw new ConstraintViolationException(
'The token is not identified with the expected ID'
);
Expand Down
2 changes: 1 addition & 1 deletion src/Validation/Constraint/IssuedBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(string ...$issuers)
*/
public function assert(Token $token): void
{
if (!$token->hasBeenIssuedBy(...$this->issuers)) {
if (! $token->hasBeenIssuedBy(...$this->issuers)) {
throw new ConstraintViolationException(
'The token was not issued by the given issuers'
);
Expand Down
2 changes: 1 addition & 1 deletion src/Validation/Constraint/PermittedFor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(string $audience)
*/
public function assert(Token $token): void
{
if (!$token->isPermittedFor($this->audience)) {
if (! $token->isPermittedFor($this->audience)) {
throw new ConstraintViolationException(
'The token is not allowed to be used by this audience'
);
Expand Down
2 changes: 1 addition & 1 deletion src/Validation/Constraint/RelatedTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(string $subject)
*/
public function assert(Token $token): void
{
if (!$token->isRelatedTo($this->subject)) {
if (! $token->isRelatedTo($this->subject)) {
throw new ConstraintViolationException(
'The token is not related to the expected subject'
);
Expand Down
6 changes: 3 additions & 3 deletions src/Validation/Constraint/SignedWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ final class SignedWith implements Constraint
public function __construct(Signer $signer, Signer\Key $key)
{
$this->signer = $signer;
$this->key = $key;
$this->key = $key;
}

/**
* {@inheritdoc}
*/
public function assert(Token $token): void
{
if (!$token instanceof Token\Plain) {
if (! $token instanceof Token\Plain) {
throw new ConstraintViolationException('You should pass a plain token');
}

if ($token->headers()->get('alg') !== $this->signer->getAlgorithmId()) {
throw new ConstraintViolationException('Token signer mismatch');
}

if (!$this->signer->verify($token->signature()->hash(), $token->payload(), $this->key)) {
if (! $this->signer->verify($token->signature()->hash(), $token->payload(), $this->key)) {
throw new ConstraintViolationException('Token signature mismatch');
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Validation/InvalidTokenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class InvalidTokenException extends Exception

public static function fromViolations(ConstraintViolationException ...$violations): self
{
$exception = new self(self::buildMessage($violations));
$exception = new self(self::buildMessage($violations));
$exception->violations = $violations;

return $exception;
Expand All @@ -38,7 +38,7 @@ function (ConstraintViolationException $violation): string {
$violations
);

$message = "The token violates some mandatory constraints, details:\n";
$message = "The token violates some mandatory constraints, details:\n";
$message .= implode("\n", $violations);

return $message;
Expand Down
6 changes: 3 additions & 3 deletions test/functional/EcdsaTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function builderShouldRaiseExceptionWhenKeyIsNotEcdsaCompatible(): void
*/
public function builderCanGenerateAToken(): Token
{
$user = ['name' => 'testing', 'email' => 'testing@abc.com'];
$user = ['name' => 'testing', 'email' => 'testing@abc.com'];
$builder = $this->config->createBuilder();

$token = $builder->identifiedBy('1')
Expand Down Expand Up @@ -316,7 +316,7 @@ public function signatureValidationShouldSucceedWhenKeyIsRight(Token $token): vo
public function everythingShouldWorkWithAKeyWithParams(): void
{
$builder = $this->config->createBuilder();
$signer = $this->config->getSigner();
$signer = $this->config->getSigner();

$token = $builder->identifiedBy('1')
->permittedFor('http://client.abc.com')
Expand Down Expand Up @@ -365,7 +365,7 @@ public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs(): void
. 'mZudf1zCUZ8/4eodlHU=' . PHP_EOL
. '-----END PUBLIC KEY-----';

$token = $this->config->getParser()->parse((string) $data);
$token = $this->config->getParser()->parse((string) $data);
$constraint = new SignedWith(Sha512::create(), new Key($key));

self::assertTrue($this->config->getValidator()->validate($token, $constraint));
Expand Down
4 changes: 2 additions & 2 deletions test/functional/HmacTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function createConfiguration(): void
*/
public function builderCanGenerateAToken(): Token
{
$user = ['name' => 'testing', 'email' => 'testing@abc.com'];
$user = ['name' => 'testing', 'email' => 'testing@abc.com'];
$builder = $this->config->createBuilder();

$token = $builder->identifiedBy('1')
Expand Down Expand Up @@ -191,7 +191,7 @@ public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs(): void
$data = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJoZWxsbyI6IndvcmxkIn0.Rh'
. '7AEgqCB7zae1PkgIlvOpeyw9Ab8NGTbeOH7heHO0o';

$token = $this->config->getParser()->parse((string) $data);
$token = $this->config->getParser()->parse((string) $data);
$constraint = new SignedWith($this->config->getSigner(), $this->config->getVerificationKey());

self::assertTrue($this->config->getValidator()->validate($token, $constraint));
Expand Down
2 changes: 1 addition & 1 deletion test/functional/MaliciousTamperingPreventionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function preventRegressionsThatAllowsMaliciousTampering(): void
*/
private function createMaliciousToken(string $token): string
{
$dec = new Parser();
$dec = new Parser();
$asplode = explode('.', $token);

// The user is lying; we insist that we're using HMAC-SHA512, with the
Expand Down
4 changes: 2 additions & 2 deletions test/functional/RsaTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function builderShouldRaiseExceptionWhenKeyIsNotRsaCompatible(): void
*/
public function builderCanGenerateAToken(): Token
{
$user = ['name' => 'testing', 'email' => 'testing@abc.com'];
$user = ['name' => 'testing', 'email' => 'testing@abc.com'];
$builder = $this->config->createBuilder();

$token = $builder->identifiedBy('1')
Expand Down Expand Up @@ -284,7 +284,7 @@ public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs(): void
. 'nJCupP-Lqh4TmIhftIimSCgLNmJg80wyrpUEfZYReE7hPuEmY0ClTqAGIMQoNS'
. '98ljwDxwhfbSuL2tAdbV4DekbTpWzspe3dOJ7RSzmPKVZ6NoezaIazKqyqkmHZfcMaHI1lQeGia6LTbHU1bp0gINi74Vw';

$token = $this->config->getParser()->parse((string) $data);
$token = $this->config->getParser()->parse((string) $data);
$constraint = new SignedWith($this->config->getSigner(), $this->config->getVerificationKey());

self::assertTrue($this->config->getValidator()->validate($token, $constraint));
Expand Down
Loading