added predis and eseye back in.

This commit is contained in:
2020-12-25 11:28:41 +00:00
parent 0ddd298350
commit 017f72b42e
670 changed files with 60992 additions and 10 deletions

View File

@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Jose\Component\Core\Tests;
use InvalidArgumentException;
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Core\AlgorithmManagerFactory;
use PHPUnit\Framework\TestCase;
use TypeError;
/**
* @group unit
* @group JWAManager
*
* @internal
*/
class AlgorithmManagerFactoryTest extends TestCase
{
/**
* @var null|AlgorithmManagerFactory
*/
private $algorithmManagerFactory;
/**
* @test
* @covers \Jose\Component\Core\AlgorithmManagerFactory
*/
public function iCanListSupportedAliases(): void
{
static::assertEquals(['foo'], $this->getAlgorithmManagerFactory()->aliases());
static::assertEquals(['foo'], array_keys($this->getAlgorithmManagerFactory()->all()));
}
/**
* @test
* @covers \Jose\Component\Core\AlgorithmManager
*/
public function iCannotCreateAnAlgorithmManagerWithABadArgument(): void
{
$this->expectException(TypeError::class);
new AlgorithmManager(['foo']);
}
/**
* @test
* @covers \Jose\Component\Core\AlgorithmManager
*/
public function iCannotGetAnAlgorithmThatDoesNotExist(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The algorithm "HS384" is not supported.');
$manager = new AlgorithmManager([new FooAlgorithm()]);
static::assertEquals(['foo'], $manager->list());
static::assertTrue($manager->has('foo'));
static::assertFalse($manager->has('HS384'));
$manager->get('HS384');
}
private function getAlgorithmManagerFactory(): AlgorithmManagerFactory
{
if (null === $this->algorithmManagerFactory) {
$this->algorithmManagerFactory = new AlgorithmManagerFactory();
$this->algorithmManagerFactory->add('foo', new FooAlgorithm());
}
return $this->algorithmManagerFactory;
}
}

View File

@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Jose\Component\Core\Tests;
use Jose\Component\Core\Algorithm;
class FooAlgorithm implements Algorithm
{
public function name(): string
{
return 'foo';
}
public function allowedKeyTypes(): array
{
return ['FOO'];
}
}

View File

@@ -0,0 +1,286 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Jose\Component\Core\Tests;
use function count;
use InvalidArgumentException;
use Jose\Component\Core\JWK;
use Jose\Component\Core\JWKSet;
use PHPUnit\Framework\TestCase;
/**
* @group unit
* @group JWKSet
*
* @internal
*/
class JWKSetTest extends TestCase
{
/**
* @test
*/
public function iCanSelectAKeyInAKeySet(): void
{
$jwkset = $this->getPublicKeySet();
$jwk = $jwkset->selectKey('enc');
static::assertInstanceOf(JWK::class, $jwk);
}
/**
* @test
*/
public function iCannotSelectAKeyFromAKeySetWithUnsupportedUsageParameter(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Allowed key types are "sig" or "enc".');
$jwkset = $this->getPublicKeySet();
$jwkset->selectKey('foo');
}
/**
* @test
*/
public function iCannotCreateAKeySetWithBadArguments(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid data.');
JWKSet::createFromKeyData(['keys' => true]);
}
/**
* @test
*/
public function iCanGetAllKeysInAKeySet(): void
{
$jwkset = $this->getPublicKeySet();
static::assertEquals(3, count($jwkset->all()));
}
/**
* @test
*/
public function iCanAddKeysInAKeySet(): void
{
$jwkset = $this->getPublicKeySet();
$new_jwkset = $jwkset->with(new JWK(['kty' => 'none']));
static::assertEquals(4, count($new_jwkset->all()));
static::assertNotSame($jwkset, $new_jwkset);
}
/**
* @test
*/
public function iCanSelectAKeyWithAlgorithm(): void
{
$jwkset = $this->getPublicKeySet();
$jwk = $jwkset->selectKey('enc', new FooAlgorithm());
static::assertInstanceOf(JWK::class, $jwk);
static::assertEquals(
[
'kid' => '71ee230371d19630bc17fb90ccf20ae632ad8cf8',
'kty' => 'FOO',
'alg' => 'foo',
'use' => 'enc',
],
$jwk->all()
);
}
/**
* @test
*/
public function iCanSelectAKeyWithAlgorithmAndKeyId(): void
{
$jwkset = $this->getPublicKeySet();
$jwk = $jwkset->selectKey('sig', new FooAlgorithm(), ['kid' => '02491f945c951adf156f370788e8ccdabf8877a8']);
static::assertInstanceOf(JWK::class, $jwk);
static::assertEquals(
[
'kid' => '02491f945c951adf156f370788e8ccdabf8877a8',
'kty' => 'FOO',
'alg' => 'foo',
'use' => 'sig',
],
$jwk->all()
);
}
/**
* @test
*/
public function iCanSelectAKeyWithWithKeyId(): void
{
$jwkset = $this->getPublicKeySet();
$jwk = $jwkset->selectKey('sig', null, ['kid' => '02491f945c951adf156f370788e8ccdabf8877a8']);
static::assertInstanceOf(JWK::class, $jwk);
static::assertEquals(
[
'kid' => '02491f945c951adf156f370788e8ccdabf8877a8',
'kty' => 'FOO',
'alg' => 'foo',
'use' => 'sig',
],
$jwk->all()
);
}
/**
* @test
*/
public function theKeySetDoesNotContainsSuitableAKeyThatFitsOnTheRequirements(): void
{
$jwkset = $this->getPublicKeySet();
$jwk = $jwkset->selectKey('enc', null, ['kid' => '02491f945c951adf156f370788e8ccdabf8877a8']);
static::assertNull($jwk);
}
/**
* @test
*/
public function iCanCreateAKeySetUsingValues(): void
{
$values = ['keys' => [[
'kid' => '71ee230371d19630bc17fb90ccf20ae632ad8cf8',
'kty' => 'FOO',
'alg' => 'foo',
'use' => 'sig',
]]];
$jwkset = JWKSet::createFromKeyData($values);
static::assertEquals(1, count($jwkset));
static::assertTrue($jwkset->has('71ee230371d19630bc17fb90ccf20ae632ad8cf8'));
static::assertFalse($jwkset->has(0));
}
/**
* @test
*/
public function keySet(): void
{
$jwk1 = new JWK([
'kty' => 'EC',
'crv' => 'P-256',
'x' => 'f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU',
'y' => 'x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0',
'use' => 'sign',
'key_ops' => ['sign'],
'alg' => 'ES256',
'kid' => '0123456789',
]);
$jwk2 = new JWK([
'kty' => 'EC',
'crv' => 'P-256',
'x' => 'f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU',
'y' => 'x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0',
'd' => 'jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI',
'use' => 'sign',
'key_ops' => ['verify'],
'alg' => 'ES256',
'kid' => '9876543210',
]);
$jwkset = new JWKSet([$jwk1]);
$jwkset = $jwkset->with($jwk2);
static::assertEquals('{"keys":[{"kty":"EC","crv":"P-256","x":"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU","y":"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0","use":"sign","key_ops":["sign"],"alg":"ES256","kid":"0123456789"},{"kty":"EC","crv":"P-256","x":"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU","y":"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0","d":"jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI","use":"sign","key_ops":["verify"],"alg":"ES256","kid":"9876543210"}]}', json_encode($jwkset));
static::assertEquals(2, count($jwkset));
static::assertEquals(2, $jwkset->count());
static::assertTrue($jwkset->has('0123456789'));
static::assertTrue($jwkset->has('9876543210'));
static::assertFalse($jwkset->has(0));
foreach ($jwkset as $key) {
static::assertEquals('EC', $key->get('kty'));
}
static::assertEquals('9876543210', $jwkset->get('9876543210')->get('kid'));
$jwkset = $jwkset->without('9876543210');
$jwkset = $jwkset->without('9876543210');
static::assertEquals(1, count($jwkset));
static::assertEquals(1, $jwkset->count());
$jwkset = $jwkset->without('0123456789');
static::assertEquals(0, $jwkset->count());
}
/**
* @test
*/
public function keySet2(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Undefined index.');
$jwk1 = new JWK([
'kty' => 'EC',
'crv' => 'P-256',
'x' => 'f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU',
'y' => 'x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0',
'use' => 'sign',
'key_ops' => ['sign'],
'alg' => 'ES256',
'kid' => '0123456789',
]);
$jwk2 = new JWK([
'kty' => 'EC',
'crv' => 'P-256',
'x' => 'f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU',
'y' => 'x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0',
'd' => 'jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI',
'use' => 'sign',
'key_ops' => ['verify'],
'alg' => 'ES256',
'kid' => '9876543210',
]);
$jwkset = new JWKSet([$jwk1, $jwk2]);
$jwkset->get(2);
}
private function getPublicKeySet(): JWKSet
{
$keys = ['keys' => [
[
'kid' => '71ee230371d19630bc17fb90ccf20ae632ad8cf8',
'kty' => 'FOO',
'alg' => 'foo',
'use' => 'enc',
],
[
'kid' => '02491f945c951adf156f370788e8ccdabf8877a8',
'kty' => 'FOO',
'alg' => 'foo',
'use' => 'sig',
],
[
'kty' => 'EC',
'crv' => 'P-256',
'x' => 'f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU',
'y' => 'x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0',
],
]];
return JWKSet::createFromKeyData($keys);
}
}

View File

@@ -0,0 +1,147 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Jose\Component\Core\Tests;
use InvalidArgumentException;
use Jose\Component\Core\JWK;
use PHPUnit\Framework\TestCase;
/**
* @group unit
* @group JWK
*
* @internal
*/
class JWKTest extends TestCase
{
/**
* @test
*/
public function aKeyContainsAllExpectedParameters(): void
{
$jwk = new JWK([
'kty' => 'EC',
'crv' => 'P-256',
'x' => 'f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU',
'y' => 'x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0',
'use' => 'sig',
'key_ops' => ['sign'],
'alg' => 'ES256',
'bar' => 'plic',
]);
static::assertEquals('EC', $jwk->get('kty'));
static::assertEquals('ES256', $jwk->get('alg'));
static::assertEquals('sig', $jwk->get('use'));
static::assertFalse($jwk->has('kid'));
static::assertEquals(['sign'], $jwk->get('key_ops'));
static::assertEquals('P-256', $jwk->get('crv'));
static::assertFalse($jwk->has('x5u'));
static::assertFalse($jwk->has('x5c'));
static::assertFalse($jwk->has('x5t'));
static::assertFalse($jwk->has('x5t#256'));
static::assertEquals('f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU', $jwk->get('x'));
static::assertEquals('x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0', $jwk->get('y'));
static::assertEquals('{"kty":"EC","crv":"P-256","x":"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU","y":"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0","use":"sig","key_ops":["sign"],"alg":"ES256","bar":"plic"}', json_encode($jwk));
static::assertEquals('oKIywvGUpTVTyxMQ3bwIIeQUudfr_CkLMjCE19ECD-U', $jwk->thumbprint('sha256'));
static::assertEquals('EMMMl6Rj75mqhcABihxxl_VCN9s', $jwk->thumbprint('sha1'));
static::assertEquals('dqwHnan4iJ1_eEll-o4Egw', $jwk->thumbprint('md5'));
}
/**
* @test
*/
public function iCannotGetTheThumbprintOfTheKeyWhenIUseAnUnsupportedHashingAlgorithm(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The hash algorithm "foo" is not supported.');
$jwk = new JWK([
'kty' => 'EC',
'crv' => 'P-256',
'x' => 'f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU',
'y' => 'x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0',
'use' => 'sig',
'key_ops' => ['sign'],
'alg' => 'ES256',
'bar' => 'plic',
]);
$jwk->thumbprint('foo');
}
/**
* @test
*/
public function iMustSetAtLeastTheKtyParameter(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The parameter "kty" is mandatory.');
new JWK([]);
}
/**
* @test
*/
public function iCannotGetAParameterThatDoesNotExist(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The value identified by "ABCD" does not exist.');
$jwk = new JWK([
'kty' => 'EC',
'crv' => 'P-256',
'x' => 'f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU',
'y' => 'x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0',
'use' => 'sign',
'key_ops' => ['sign'],
'alg' => 'ES256',
'bar' => 'plic',
]);
$jwk->get('ABCD');
}
/**
* @test
*/
public function iCanConvertAPrivateKeyIntoPublicKey(): void
{
$private = new JWK([
'kty' => 'EC',
'crv' => 'P-256',
'x' => 'f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU',
'y' => 'x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0',
'd' => 'jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI',
'use' => 'sign',
'key_ops' => ['verify'],
'alg' => 'ES256',
'kid' => '9876543210',
]);
$public = $private->toPublic();
static::assertEquals(json_encode([
'kty' => 'EC',
'crv' => 'P-256',
'x' => 'f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU',
'y' => 'x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0',
'use' => 'sign',
'key_ops' => ['verify'],
'alg' => 'ES256',
'kid' => '9876543210',
]), json_encode($public));
}
}

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Jose\Component\Core\Tests;
use Jose\Component\Core\Util\JsonConverter;
use PHPUnit\Framework\TestCase;
/**
* @group unit
* @group JsonConverter
*
* @internal
*/
class JsonConverterTest extends TestCase
{
/**
* @test
*/
public function iCanConvertAnObjectIntoAJsonString(): void
{
static::assertEquals('{"foo":"BAR"}', JsonConverter::encode(['foo' => 'BAR']));
static::assertEquals(['foo' => 'BAR'], JsonConverter::decode('{"foo":"BAR"}'));
}
}