Skip to content

Commit

Permalink
Merge pull request #833 from Slamdunk/no_empty_key
Browse files Browse the repository at this point in the history
Key: permit empty keys only with `::empty()` factory method
  • Loading branch information
Ocramius committed Apr 7, 2022
2 parents 62a0bf1 + c3c65d8 commit ae3aac8
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/*.yml export-ignore
/CONTRIBUTING.md export-ignore
/*.dist export-ignore
/phpstan-baseline.neon export-ignore
/phpbench.json export-ignore
/composer.lock export-ignore
/README.md export-ignore
Expand Down
12 changes: 12 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#1 \\$contents of class Lcobucci\\\\JWT\\\\Signer\\\\Key\\\\InMemory constructor expects non\\-empty\\-string, string given\\.$#"
count: 3
path: src/Signer/Key/InMemory.php

-
message: "#^Strict comparison using \\=\\=\\= between non\\-empty\\-string and '' will always evaluate to false\\.$#"
count: 1
path: src/Signer/Key/InMemory.php

3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
includes:
- phpstan-baseline.neon

parameters:
level: 8 # not yet ready for all the `mixed` checks
paths:
Expand Down
5 changes: 5 additions & 0 deletions src/Signer/InvalidKeyProvided.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public static function incompatibleKey(): self
{
return new self('This key is not compatible with this signer');
}

public static function cannotBeEmpty(): self
{
return new self('Key cannot be empty');
}
}
12 changes: 11 additions & 1 deletion src/Signer/Key/InMemory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Lcobucci\JWT\Signer\Key;

use Lcobucci\JWT\Signer\InvalidKeyProvided;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\SodiumBase64Polyfill;
use SplFileObject;
Expand All @@ -16,15 +17,24 @@ final class InMemory implements Key
private string $contents;
private string $passphrase;

/** @param non-empty-string $contents */
private function __construct(string $contents, string $passphrase)
{
if ($contents === '') {
throw InvalidKeyProvided::cannotBeEmpty();
}

$this->contents = $contents;
$this->passphrase = $passphrase;
}

public static function empty(): self
{
return new self('', '');
$emptyKey = new self('empty', 'empty');
$emptyKey->contents = '';
$emptyKey->passphrase = '';

return $emptyKey;
}

public static function plainText(string $contents, string $passphrase = ''): self
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function forSymmetricSignerShouldConfigureSignerAndBothKeys(): void
*/
public function forUnsecuredSignerShouldConfigureSignerAndBothKeys(): void
{
$key = InMemory::plainText('');
$key = InMemory::empty();
$config = Configuration::forUnsecuredSigner();

self::assertInstanceOf(None::class, $config->signer());
Expand Down
25 changes: 21 additions & 4 deletions test/unit/Signer/Key/InMemoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Lcobucci\JWT\Signer\Key;

use Lcobucci\JWT\Encoding\CannotDecodeContent;
use Lcobucci\JWT\Signer\InvalidKeyProvided;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -135,12 +136,28 @@ public function passphraseShouldReturnConfiguredData(): void
*
* @covers ::__construct
* @covers ::plainText
* @covers ::passphrase
* @covers \Lcobucci\JWT\Signer\InvalidKeyProvided::cannotBeEmpty
*/
public function passphraseShouldReturnAnEmptyStringWhenNothingWasConfigured(): void
public function emptyPlainTextContentShouldRaiseException(): void
{
$key = InMemory::plainText('testing');
$this->expectException(InvalidKeyProvided::class);

self::assertSame('', $key->passphrase());
InMemory::plainText('');
}

/**
* @test
*
* @covers ::__construct
* @covers ::base64Encoded
* @covers \Lcobucci\JWT\Signer\InvalidKeyProvided::cannotBeEmpty
*
* @uses \Lcobucci\JWT\SodiumBase64Polyfill::base642bin
*/
public function emptyBase64ContentShouldRaiseException(): void
{
$this->expectException(InvalidKeyProvided::class);

InMemory::base64Encoded('');
}
}

0 comments on commit ae3aac8

Please sign in to comment.