first commit
This commit is contained in:
101
libraries/vendor/web-token/jwt-signature-algorithm-eddsa/EdDSA.php
vendored
Normal file
101
libraries/vendor/web-token/jwt-signature-algorithm-eddsa/EdDSA.php
vendored
Normal file
@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jose\Component\Signature\Algorithm;
|
||||
|
||||
use function extension_loaded;
|
||||
use function in_array;
|
||||
use InvalidArgumentException;
|
||||
use function is_string;
|
||||
use Jose\Component\Core\JWK;
|
||||
use ParagonIE\ConstantTime\Base64UrlSafe;
|
||||
use RuntimeException;
|
||||
|
||||
final class EdDSA implements SignatureAlgorithm
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
if (! extension_loaded('sodium')) {
|
||||
throw new RuntimeException('The extension "sodium" is not available. Please install it to use this method');
|
||||
}
|
||||
}
|
||||
|
||||
public function allowedKeyTypes(): array
|
||||
{
|
||||
return ['OKP'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-empty-string
|
||||
*/
|
||||
public function sign(JWK $key, string $input): string
|
||||
{
|
||||
$this->checkKey($key);
|
||||
if (! $key->has('d')) {
|
||||
throw new InvalidArgumentException('The EC key is not private');
|
||||
}
|
||||
$d = $key->get('d');
|
||||
if (! is_string($d) || $d === '') {
|
||||
throw new InvalidArgumentException('Invalid "d" parameter.');
|
||||
}
|
||||
if (! $key->has('x')) {
|
||||
$x = sodium_crypto_sign_publickey_from_secretkey($d);
|
||||
} else {
|
||||
$x = $key->get('x');
|
||||
}
|
||||
if (! is_string($x) || $x === '') {
|
||||
throw new InvalidArgumentException('Invalid "x" parameter.');
|
||||
}
|
||||
/** @var non-empty-string $x */
|
||||
$x = Base64UrlSafe::decode($x);
|
||||
/** @var non-empty-string $d */
|
||||
$d = Base64UrlSafe::decode($d);
|
||||
$secret = $d . $x;
|
||||
|
||||
return match ($key->get('crv')) {
|
||||
'Ed25519' => sodium_crypto_sign_detached($input, $secret),
|
||||
default => throw new InvalidArgumentException('Unsupported curve'),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param non-empty-string $signature
|
||||
*/
|
||||
public function verify(JWK $key, string $input, string $signature): bool
|
||||
{
|
||||
$this->checkKey($key);
|
||||
$x = $key->get('x');
|
||||
if (! is_string($x)) {
|
||||
throw new InvalidArgumentException('Invalid "x" parameter.');
|
||||
}
|
||||
|
||||
/** @var non-empty-string $public */
|
||||
$public = Base64UrlSafe::decode($x);
|
||||
|
||||
return match ($key->get('crv')) {
|
||||
'Ed25519' => sodium_crypto_sign_verify_detached($signature, $input, $public),
|
||||
default => throw new InvalidArgumentException('Unsupported curve'),
|
||||
};
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return 'EdDSA';
|
||||
}
|
||||
|
||||
private function checkKey(JWK $key): void
|
||||
{
|
||||
if (! in_array($key->get('kty'), $this->allowedKeyTypes(), true)) {
|
||||
throw new InvalidArgumentException('Wrong key type.');
|
||||
}
|
||||
foreach (['x', 'crv'] as $k) {
|
||||
if (! $key->has($k)) {
|
||||
throw new InvalidArgumentException(sprintf('The key parameter "%s" is missing.', $k));
|
||||
}
|
||||
}
|
||||
if ($key->get('crv') !== 'Ed25519') {
|
||||
throw new InvalidArgumentException('Unsupported curve.');
|
||||
}
|
||||
}
|
||||
}
|
||||
21
libraries/vendor/web-token/jwt-signature-algorithm-eddsa/LICENSE
vendored
Normal file
21
libraries/vendor/web-token/jwt-signature-algorithm-eddsa/LICENSE
vendored
Normal 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.
|
||||
Reference in New Issue
Block a user