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,4 @@
# Contributing
This repository is a sub repository of [the JWT Framework](https://github.com/web-token/jwt-framework) project and is READ ONLY.
Please do not submit any Pull Requests here. It will be automatically closed.

View File

@@ -0,0 +1 @@
patreon: FlorentMorselli

View File

@@ -0,0 +1,3 @@
Please do not submit any Pull Requests here. It will be automatically closed.
You should submit it here: https://github.com/web-token/jwt-framework/pulls

View File

@@ -0,0 +1,61 @@
<?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\Signature\Algorithm;
use Base64Url\Base64Url;
use function in_array;
use InvalidArgumentException;
use function is_string;
use Jose\Component\Core\JWK;
abstract class HMAC implements MacAlgorithm
{
public function allowedKeyTypes(): array
{
return ['oct'];
}
public function verify(JWK $key, string $input, string $signature): bool
{
return hash_equals($this->hash($key, $input), $signature);
}
public function hash(JWK $key, string $input): string
{
$k = $this->getKey($key);
return hash_hmac($this->getHashAlgorithm(), $input, $k, true);
}
/**
* @throws InvalidArgumentException if the key is invalid
*/
protected function getKey(JWK $key): string
{
if (!in_array($key->get('kty'), $this->allowedKeyTypes(), true)) {
throw new InvalidArgumentException('Wrong key type.');
}
if (!$key->has('k')) {
throw new InvalidArgumentException('The key parameter "k" is missing.');
}
$k = $key->get('k');
if (!is_string($k)) {
throw new InvalidArgumentException('The key parameter "k" is invalid.');
}
return Base64Url::decode($k);
}
abstract protected function getHashAlgorithm(): string;
}

View File

@@ -0,0 +1,43 @@
<?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\Signature\Algorithm;
use InvalidArgumentException;
use Jose\Component\Core\JWK;
final class HS256 extends HMAC
{
public function name(): string
{
return 'HS256';
}
protected function getHashAlgorithm(): string
{
return 'sha256';
}
/**
* @throws InvalidArgumentException if the key is invalid
*/
protected function getKey(JWK $key): string
{
$k = parent::getKey($key);
if (mb_strlen($k, '8bit') < 32) {
throw new InvalidArgumentException('Invalid key length.');
}
return $k;
}
}

View File

@@ -0,0 +1,43 @@
<?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\Signature\Algorithm;
use InvalidArgumentException;
use Jose\Component\Core\JWK;
final class HS384 extends HMAC
{
public function name(): string
{
return 'HS384';
}
protected function getHashAlgorithm(): string
{
return 'sha384';
}
/**
* @throws InvalidArgumentException if the key is invalid
*/
protected function getKey(JWK $key): string
{
$k = parent::getKey($key);
if (mb_strlen($k, '8bit') < 48) {
throw new InvalidArgumentException('Invalid key length.');
}
return $k;
}
}

View File

@@ -0,0 +1,43 @@
<?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\Signature\Algorithm;
use InvalidArgumentException;
use Jose\Component\Core\JWK;
final class HS512 extends HMAC
{
public function name(): string
{
return 'HS512';
}
protected function getHashAlgorithm(): string
{
return 'sha512';
}
/**
* @throws InvalidArgumentException if the key is invalid
*/
protected function getKey(JWK $key): string
{
$k = parent::getKey($key);
if (mb_strlen($k, '8bit') < 64) {
throw new InvalidArgumentException('Invalid key length.');
}
return $k;
}
}

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014-2019 Spomky-Labs
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,15 @@
HMAC Based Signature Algorithms For JWT-Framework
=================================================
This repository is a sub repository of [the JWT Framework](https://github.com/web-token/jwt-framework) project and is READ ONLY.
**Please do not submit any Pull Request here.**
You should go to [the main repository](https://github.com/web-token/jwt-framework) instead.
# Documentation
The official documentation is available as https://web-token.spomky-labs.com/
# Licence
This software is release under [MIT licence](LICENSE).

View File

@@ -0,0 +1,317 @@
<?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\Signature\Algorithm\Tests;
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Core\JWK;
use Jose\Component\Signature\Algorithm\HS256;
use Jose\Component\Signature\JWSBuilder;
use Jose\Component\Signature\JWSVerifier;
use Jose\Component\Signature\Serializer;
use PHPUnit\Framework\TestCase;
/**
* @see https://tools.ietf.org/html/rfc7520#section-4.4
* @see https://tools.ietf.org/html/rfc7520#section-4.5
* @see https://tools.ietf.org/html/rfc7520#section-4.6
* @see https://tools.ietf.org/html/rfc7520#section-4.7
*
* @group HMAC
* @group RFC7520
*
* @internal
*/
class HMACFromRFC7520Test extends TestCase
{
/**
* @see https://tools.ietf.org/html/rfc7520#section-4.4
*
* @test
*/
public function hS256(): void
{
/*
* Payload
* Symmetric Key
* @see https://tools.ietf.org/html/rfc7520#section-3.5
* @see https://tools.ietf.org/html/rfc7520#section-4.4.1
*/
$payload = "It\xe2\x80\x99s a dangerous business, Frodo, going out your door. You step onto the road, and if you don't keep your feet, there\xe2\x80\x99s no knowing where you might be swept off to.";
$key = new JWK([
'kty' => 'oct',
'kid' => '018c0ae5-4d9b-471b-bfd6-eef314bc7037',
'use' => 'sig',
'alg' => 'HS256',
'k' => 'hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg',
]);
/*
* Header
* @see https://tools.ietf.org/html/rfc7520#section-4.4.2
*/
$header = [
'alg' => 'HS256',
'kid' => '018c0ae5-4d9b-471b-bfd6-eef314bc7037',
];
$jwsBuilder = new JWSBuilder(
new AlgorithmManager([new HS256()])
);
$jwsVerifier = new JWSVerifier(
new AlgorithmManager([new HS256()])
);
$compactSerializer = new Serializer\CompactSerializer(
);
$jsonFlattenedSerializer = new Serializer\JSONFlattenedSerializer(
);
$jsonGeneralSerializer = new Serializer\JSONGeneralSerializer(
);
$jws = $jwsBuilder
->create()->withPayload($payload)
->addSignature($key, $header)
->build()
;
/*
* Header
* @see https://tools.ietf.org/html/rfc7520#section-4.4.3
*/
$expected_compact_json = 'eyJhbGciOiJIUzI1NiIsImtpZCI6IjAxOGMwYWU1LTRkOWItNDcxYi1iZmQ2LWVlZjMxNGJjNzAzNyJ9.SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IHlvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBkb24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcmUgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4.s0h6KThzkfBBBkLspW1h84VsJZFTsPPqMDA7g1Md7p0';
$expected_flattened_json = '{"payload":"SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IHlvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBkb24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcmUgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4","protected":"eyJhbGciOiJIUzI1NiIsImtpZCI6IjAxOGMwYWU1LTRkOWItNDcxYi1iZmQ2LWVlZjMxNGJjNzAzNyJ9","signature":"s0h6KThzkfBBBkLspW1h84VsJZFTsPPqMDA7g1Md7p0"}';
$expected_json = '{"payload":"SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IHlvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBkb24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcmUgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4","signatures":[{"protected":"eyJhbGciOiJIUzI1NiIsImtpZCI6IjAxOGMwYWU1LTRkOWItNDcxYi1iZmQ2LWVlZjMxNGJjNzAzNyJ9","signature":"s0h6KThzkfBBBkLspW1h84VsJZFTsPPqMDA7g1Md7p0"}]}';
static::assertEquals($expected_compact_json, $compactSerializer->serialize($jws, 0));
// We decode the json to compare the 2 arrays otherwise the test may fail as the order may be different
static::assertEquals(json_decode($expected_flattened_json, true), json_decode($jsonFlattenedSerializer->serialize($jws, 0), true));
static::assertEquals(json_decode($expected_json, true), json_decode($jsonGeneralSerializer->serialize($jws, 0), true));
$loaded_compact_json = $compactSerializer->unserialize($expected_compact_json);
static::assertTrue($jwsVerifier->verifyWithKey($loaded_compact_json, $key, 0));
$loaded_flattened_json = $jsonFlattenedSerializer->unserialize($expected_flattened_json);
static::assertTrue($jwsVerifier->verifyWithKey($loaded_flattened_json, $key, 0));
$loaded_json = $jsonGeneralSerializer->unserialize($expected_json);
static::assertTrue($jwsVerifier->verifyWithKey($loaded_json, $key, 0));
}
/**
* @see https://tools.ietf.org/html/rfc7520#section-4.5
*
* @test
*/
public function hS256WithDetachedPayload(): void
{
/*
* Payload
* Symmetric Key
* @see https://tools.ietf.org/html/rfc7520#section-3.5
* @see https://tools.ietf.org/html/rfc7520#section-4.5.1
*/
$payload = "It\xe2\x80\x99s a dangerous business, Frodo, going out your door. You step onto the road, and if you don't keep your feet, there\xe2\x80\x99s no knowing where you might be swept off to.";
$key = new JWK([
'kty' => 'oct',
'kid' => '018c0ae5-4d9b-471b-bfd6-eef314bc7037',
'use' => 'sig',
'alg' => 'HS256',
'k' => 'hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg',
]);
/*
* Header
* @see https://tools.ietf.org/html/rfc7520#section-4.5.2
*/
$header = [
'alg' => 'HS256',
'kid' => '018c0ae5-4d9b-471b-bfd6-eef314bc7037',
];
$jwsBuilder = new JWSBuilder(
new AlgorithmManager([new HS256()])
);
$jwsVerifier = new JWSVerifier(
new AlgorithmManager([new HS256()])
);
$compactSerializer = new Serializer\CompactSerializer(
);
$jsonFlattenedSerializer = new Serializer\JSONFlattenedSerializer(
);
$jsonGeneralSerializer = new Serializer\JSONGeneralSerializer(
);
$jws = $jwsBuilder
->create()->withPayload($payload, true)
->addSignature($key, $header)
->build()
;
/*
* Header
* @see https://tools.ietf.org/html/rfc7520#section-4.5.3
*/
$expected_compact_json = 'eyJhbGciOiJIUzI1NiIsImtpZCI6IjAxOGMwYWU1LTRkOWItNDcxYi1iZmQ2LWVlZjMxNGJjNzAzNyJ9..s0h6KThzkfBBBkLspW1h84VsJZFTsPPqMDA7g1Md7p0';
$expected_flattened_json = '{"protected":"eyJhbGciOiJIUzI1NiIsImtpZCI6IjAxOGMwYWU1LTRkOWItNDcxYi1iZmQ2LWVlZjMxNGJjNzAzNyJ9","signature":"s0h6KThzkfBBBkLspW1h84VsJZFTsPPqMDA7g1Md7p0"}';
$expected_json = '{"signatures":[{"protected":"eyJhbGciOiJIUzI1NiIsImtpZCI6IjAxOGMwYWU1LTRkOWItNDcxYi1iZmQ2LWVlZjMxNGJjNzAzNyJ9","signature":"s0h6KThzkfBBBkLspW1h84VsJZFTsPPqMDA7g1Md7p0"}]}';
static::assertEquals($expected_compact_json, $compactSerializer->serialize($jws, 0));
// We decode the json to compare the 2 arrays otherwise the test may fail as the order may be different
static::assertEquals(json_decode($expected_flattened_json, true), json_decode($jsonFlattenedSerializer->serialize($jws, 0), true));
static::assertEquals(json_decode($expected_json, true), json_decode($jsonGeneralSerializer->serialize($jws, 0), true));
$loaded_compact_json = $compactSerializer->unserialize($expected_compact_json);
static::assertTrue($jwsVerifier->verifyWithKey($loaded_compact_json, $key, 0, $payload));
$loaded_flattened_json = $jsonFlattenedSerializer->unserialize($expected_flattened_json);
static::assertTrue($jwsVerifier->verifyWithKey($loaded_flattened_json, $key, 0, $payload));
$loaded_json = $jsonGeneralSerializer->unserialize($expected_json);
static::assertTrue($jwsVerifier->verifyWithKey($loaded_json, $key, 0, $payload));
}
/**
* @see https://tools.ietf.org/html/rfc7520#section-4.6
*
* @test
*/
public function hS256WithUnprotectedHeader(): void
{
/*
* Payload
* Symmetric Key
* @see https://tools.ietf.org/html/rfc7520#section-3.5
* @see https://tools.ietf.org/html/rfc7520#section-4.6.1
*/
$payload = "It\xe2\x80\x99s a dangerous business, Frodo, going out your door. You step onto the road, and if you don't keep your feet, there\xe2\x80\x99s no knowing where you might be swept off to.";
$key = new JWK([
'kty' => 'oct',
'kid' => '018c0ae5-4d9b-471b-bfd6-eef314bc7037',
'use' => 'sig',
'alg' => 'HS256',
'k' => 'hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg',
]);
/*
* Header
* @see https://tools.ietf.org/html/rfc7520#section-4.6.2
*/
$protectedHeader = [
'alg' => 'HS256',
];
$unprotectedHeader = [
'kid' => '018c0ae5-4d9b-471b-bfd6-eef314bc7037',
];
$jwsBuilder = new JWSBuilder(
new AlgorithmManager([new HS256()])
);
$jwsVerifier = new JWSVerifier(
new AlgorithmManager([new HS256()])
);
$compactSerializer = new Serializer\CompactSerializer(
);
$jsonFlattenedSerializer = new Serializer\JSONFlattenedSerializer(
);
$jsonGeneralSerializer = new Serializer\JSONGeneralSerializer(
);
$jws = $jwsBuilder
->create()->withPayload($payload)
->addSignature($key, $protectedHeader, $unprotectedHeader)
->build()
;
/*
* Header
* @see https://tools.ietf.org/html/rfc7520#section-4.6.3
*/
$expected_flattened_json = '{"payload": "SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IHlvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBkb24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcmUgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4","protected": "eyJhbGciOiJIUzI1NiJ9","header": {"kid": "018c0ae5-4d9b-471b-bfd6-eef314bc7037"},"signature": "bWUSVaxorn7bEF1djytBd0kHv70Ly5pvbomzMWSOr20"}';
$expected_json = '{"payload":"SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IHlvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBkb24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcmUgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4","signatures":[{"protected":"eyJhbGciOiJIUzI1NiJ9","header":{"kid":"018c0ae5-4d9b-471b-bfd6-eef314bc7037"},"signature":"bWUSVaxorn7bEF1djytBd0kHv70Ly5pvbomzMWSOr20"}]}';
// We decode the json to compare the 2 arrays otherwise the test may fail as the order may be different
static::assertEquals(json_decode($expected_flattened_json, true), json_decode($jsonFlattenedSerializer->serialize($jws, 0), true));
static::assertEquals(json_decode($expected_json, true), json_decode($jsonGeneralSerializer->serialize($jws, 0), true));
$loaded_flattened_json = $jsonFlattenedSerializer->unserialize($expected_flattened_json);
static::assertTrue($jwsVerifier->verifyWithKey($loaded_flattened_json, $key, 0));
$loaded_json = $jsonGeneralSerializer->unserialize($expected_json);
static::assertTrue($jwsVerifier->verifyWithKey($loaded_json, $key, 0));
}
/**
* @see https://tools.ietf.org/html/rfc7520#section-4.7
*
* @test
*/
public function hS256WithoutProtectedHeader(): void
{
/*
* Payload
* Symmetric Key
* @see https://tools.ietf.org/html/rfc7520#section-3.5
* @see https://tools.ietf.org/html/rfc7520#section-4.7.1
*/
$payload = "It\xe2\x80\x99s a dangerous business, Frodo, going out your door. You step onto the road, and if you don't keep your feet, there\xe2\x80\x99s no knowing where you might be swept off to.";
$key = new JWK([
'kty' => 'oct',
'kid' => '018c0ae5-4d9b-471b-bfd6-eef314bc7037',
'use' => 'sig',
'alg' => 'HS256',
'k' => 'hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg',
]);
/*
* Header
* @see https://tools.ietf.org/html/rfc7520#section-4.7.2
*/
$unprotectedHeader = [
'alg' => 'HS256',
'kid' => '018c0ae5-4d9b-471b-bfd6-eef314bc7037',
];
$jwsBuilder = new JWSBuilder(
new AlgorithmManager([new HS256()])
);
$jwsVerifier = new JWSVerifier(
new AlgorithmManager([new HS256()])
);
$jsonFlattenedSerializer = new Serializer\JSONFlattenedSerializer(
);
$jsonGeneralSerializer = new Serializer\JSONGeneralSerializer(
);
$jws = $jwsBuilder
->create()->withPayload($payload)
->addSignature($key, [], $unprotectedHeader)
->build()
;
/*
* Header
* @see https://tools.ietf.org/html/rfc7520#section-4.7.3
*/
$expected_flattened_json = '{"payload":"SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IHlvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBkb24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcmUgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4","header":{"alg":"HS256","kid":"018c0ae5-4d9b-471b-bfd6-eef314bc7037"},"signature":"xuLifqLGiblpv9zBpuZczWhNj1gARaLV3UxvxhJxZuk"}';
$expected_json = '{"payload":"SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IHlvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBkb24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcmUgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4","signatures":[{"header":{"alg":"HS256","kid":"018c0ae5-4d9b-471b-bfd6-eef314bc7037"},"signature":"xuLifqLGiblpv9zBpuZczWhNj1gARaLV3UxvxhJxZuk"}]}';
// We decode the json to compare the 2 arrays otherwise the test may fail as the order may be different
static::assertEquals(json_decode($expected_flattened_json, true), json_decode($jsonFlattenedSerializer->serialize($jws, 0), true));
static::assertEquals(json_decode($expected_json, true), json_decode($jsonGeneralSerializer->serialize($jws, 0), true));
$loaded_flattened_json = $jsonFlattenedSerializer->unserialize($expected_flattened_json);
static::assertTrue($jwsVerifier->verifyWithKey($loaded_flattened_json, $key, 0));
$loaded_json = $jsonGeneralSerializer->unserialize($expected_json);
static::assertTrue($jwsVerifier->verifyWithKey($loaded_json, $key, 0));
}
}

View File

@@ -0,0 +1,116 @@
<?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\Signature\Algorithm\Tests;
use InvalidArgumentException;
use Jose\Component\Core\JWK;
use Jose\Component\Signature\Algorithm\HS256;
use Jose\Component\Signature\Algorithm\HS384;
use Jose\Component\Signature\Algorithm\HS512;
use PHPUnit\Framework\TestCase;
/**
* @group HMAC
* @group unit
*
* @internal
*/
class HMACSignatureTest extends TestCase
{
/**
* @test
*/
public function invalidKey(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Wrong key type.');
$key = new JWK([
'kty' => 'EC',
]);
$hmac = new HS256();
$data = 'Live long and Prosper.';
$hmac->hash($key, $data);
}
/**
* @test
*/
public function signatureHasBadBadLength(): void
{
$key = new JWK([
'kty' => 'oct',
'k' => 'foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo',
]);
$hmac = new HS256();
$data = 'Live long and Prosper.';
static::assertFalse($hmac->verify($key, $data, hex2bin('326eb338c465d3587f3349df0b96ba81')));
}
/**
* @test
*/
public function hS256SignAndVerify(): void
{
$key = new JWK([
'kty' => 'oct',
'k' => 'foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo',
]);
$hmac = new HS256();
$data = 'Live long and Prosper.';
$signature = $hmac->hash($key, $data);
static::assertEquals(hex2bin('7ed268ef179f530a4a1c56225c352a6782cf5379085c484b4f355b6744d6f19d'), $signature);
static::assertTrue($hmac->verify($key, $data, $signature));
}
/**
* @test
*/
public function hS384SignAndVerify(): void
{
$key = new JWK([
'kty' => 'oct',
'k' => 'foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo',
]);
$hmac = new HS384();
$data = 'Live long and Prosper.';
$signature = $hmac->hash($key, $data);
static::assertEquals(hex2bin('903ce2ef2878090d6117f88210d5a822d260fae66760186cb3326770748b9fa47c2d4531a4d5d868f99bcf7ea45c1ab4'), $signature);
static::assertTrue($hmac->verify($key, $data, $signature));
}
/**
* @test
*/
public function hS512SignAndVerify(): void
{
$key = new JWK([
'kty' => 'oct',
'k' => 'foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo',
]);
$hmac = new HS512();
$data = 'Live long and Prosper.';
$signature = $hmac->hash($key, $data);
static::assertEquals(hex2bin('e8b36712b6c6dc422eec77f31ce372ccac769450413238158bd702069630456a148d0c10dd3a661a774217fb90b0d5f94fa6c3c985438bade92ff975b9e4dc04'), $signature);
static::assertTrue($hmac->verify($key, $data, $signature));
}
}

View File

@@ -0,0 +1,41 @@
{
"name": "web-token/jwt-signature-algorithm-hmac",
"description": "HMAC Based Signature Algorithms the JWT Framework.",
"type": "library",
"license": "MIT",
"keywords": ["JWS", "JWT", "JWE", "JWA", "JWK", "JWKSet", "Jot", "Jose", "RFC7515", "RFC7516", "RFC7517", "RFC7518", "RFC7519", "RFC7520", "Bundle", "Symfony"],
"homepage": "https://github.com/web-token",
"authors": [
{
"name": "Florent Morselli",
"homepage": "https://github.com/Spomky"
},{
"name": "All contributors",
"homepage": "https://github.com/web-token/jwt-framework/contributors"
}
],
"autoload": {
"psr-4": {
"Jose\\Component\\Signature\\Algorithm\\": ""
}
},
"require": {
"web-token/jwt-signature": "^2.1"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
},
"extra": {
"branch-alias": {
"v1.0": "1.0.x-dev",
"v1.1": "1.1.x-dev",
"v1.2": "1.2.x-dev",
"v1.3": "1.3.x-dev",
"v2.0": "2.0.x-dev",
"v2.1": "2.1.x-dev"
}
},
"config": {
"sort-packages": true
}
}

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="true"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./</directory>
<exclude>
<directory>./vendor</directory>
<directory>./Tests</directory>
<directory suffix="Test.php">./src</directory>
</exclude>
</whitelist>
</filter>
</phpunit>