Skip to content

Commit

Permalink
Merge pull request #1 from localzet/PHP-7.4
Browse files Browse the repository at this point in the history
Php 7.4
  • Loading branch information
localzet authored Jun 10, 2024
2 parents fd8f5e5 + 6d5282c commit 0187358
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 55 deletions.
2 changes: 0 additions & 2 deletions .idea/LWT.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
"source": "https://github.com/localzet/LWT"
},
"require": {
"php": "^8.0",
"php": "^7.4",
"ext-openssl": "*",
"ext-mbstring": "*"
"ext-mbstring": "*",
"ext-json": "*"
},
"suggest": {
"ext-sodium": "For EdDSA"
Expand Down
123 changes: 73 additions & 50 deletions src/LWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,31 @@ final class LWT
*/
protected static function getEncryption(): string
{
$encryption = match (self::getClaim('alg')) {
'HS1', 'HS256', 'HS256/64', 'HS384', 'HS512' => 'HMAC',
'RS1', 'RS256', 'RS384', 'RS512' => 'RSA-PKCS#1',
'ES256', 'ES256K', 'ES384', 'ES512' => 'ECDSA',
'EdDSA' => 'EdDSA',

default => throw new UnexpectedValueException('Недопустимый алгоритм шифрования'),
};

if (!$encryption) {
throw new RuntimeException('Ошибка получения алгоритма шифрования');
switch (self::getClaim('alg')) {
case 'HS1':
case 'HS256':
case 'HS256/64':
case 'HS384':
case 'HS512':
$encryption = 'HMAC';
break;
case 'RS1':
case 'RS256':
case 'RS384':
case 'RS512':
$encryption = 'RSA-PKCS#1';
break;
case 'ES256':
case 'ES256K':
case 'ES384':
case 'ES512':
$encryption = 'ECDSA';
break;
case 'EdDSA':
$encryption = 'EdDSA';
break;
default:
throw new UnexpectedValueException('Недопустимый алгоритм шифрования');
}

return $encryption;
Expand All @@ -182,43 +196,52 @@ protected static function getEncryption(): string
*/
protected static function getHashAlgorithm(): string
{
$hashAlgorithm = match (self::getClaim('alg')) {
'HS1', 'RS1' => 'SHA1',
'HS256', 'RS256', 'ES256',
'ES256K', 'HS256/64', 'EdDSA' => 'SHA256',
'HS384', 'RS384', 'ES384' => 'SHA384',
'HS512', 'RS512', 'ES512' => 'SHA512',

default => throw new UnexpectedValueException('Недопустимый алгоритм шифрования'),
};

if (!$hashAlgorithm) {
throw new RuntimeException('Ошибка получения алгоритма хеширования');
switch (self::getClaim('alg')) {
case 'HS1':
case 'RS1':
$hashAlgorithm = 'SHA1';
break;
case 'HS256':
case 'RS256':
case 'ES256':
case 'ES256K':
case 'HS256/64':
case 'EdDSA':
$hashAlgorithm = 'SHA256';
break;
case 'HS384':
case 'RS384':
case 'ES384':
$hashAlgorithm = 'SHA384';
break;
case 'HS512':
case 'RS512':
case 'ES512':
$hashAlgorithm = 'SHA512';
break;
default:
throw new UnexpectedValueException('Недопустимый алгоритм шифрования');
}

return $hashAlgorithm;
}

protected static function getClaim($claim): ?string
{
return match ($claim) {
// Утверждения заголовка
'typ' => self::TYPE,
'cty' => self::$DATA_KEY ? 'LZX' : 'JWS',
'alg' => self::$ALGORITHM,
'kid' => self::$CLAIM_KID,
'enc' => self::$DATA_KEY ? self::$DATA_SYMMETRIC_ENCRYPTION . '+' . self::DATA_ASYMMETRIC_ENCRYPTION : null,

// Утверждения полезной нагрузки
// 'iss' => 'Issuer',
// 'sub' => 'Subject',
// 'aud' => 'Audience',
// 'nbf' => 'Not Before',
// 'iat' => 'Issued At',
// 'jti' => 'JWT ID',

default => throw new UnexpectedValueException('Незарегистрированное утверждение JWT')
};
switch ($claim) {
case 'typ':
return self::TYPE;
case 'cty':
return self::$DATA_KEY ? 'LZX' : 'JWS';
case 'alg':
return self::$ALGORITHM;
case 'kid':
return self::$CLAIM_KID;
case 'enc':
return self::$DATA_KEY ? self::$DATA_SYMMETRIC_ENCRYPTION . '+' . self::DATA_ASYMMETRIC_ENCRYPTION : null;
default:
throw new UnexpectedValueException('Незарегистрированное утверждение JWT');
}
}

/**
Expand All @@ -237,10 +260,10 @@ protected static function getClaim($claim): ?string
* @throws Exception
*/
public static function encode(
mixed $lwtTokenData,
$lwtTokenData,
string $signatureKey = null,
string $tokenEncryption = null,
string $encryptionKey = null,
string $encryptionKey = null
): string
{
self::$ALGORITHM = $tokenEncryption;
Expand Down Expand Up @@ -289,8 +312,8 @@ public static function decode(
string $encodedToken,
string $signatureKey = null,
string $tokenEncryption = null,
string $encryptionKey = null,
): mixed
string $encryptionKey = null
)
{
self::$ALGORITHM = $tokenEncryption;
self::$SIGN_KEY = $signatureKey;
Expand Down Expand Up @@ -407,7 +430,7 @@ protected static function verifyHeaderSegment(string $lwtTokenHeaderSegment): vo
* @see https://www.php.net/manual/en/function.openssl-cipher-iv-length.php
* @see https://www.php.net/manual/en/function.openssl-encrypt.php
*/
protected static function generatePayloadSegment(mixed $lwtTokenData): string
protected static function generatePayloadSegment($lwtTokenData): string
{
// Кодируем данные в формате JSON
$payloadData = self::jsonEncode($lwtTokenData);
Expand Down Expand Up @@ -487,7 +510,7 @@ protected static function generatePayloadSegment(mixed $lwtTokenData): string
* @see https://www.php.net/manual/en/function.openssl-cipher-iv-length.php
* @see https://www.php.net/manual/en/function.openssl-decrypt.php
*/
protected static function verifyPayloadSegment(string $lwtTokenPayloadSegment): mixed
protected static function verifyPayloadSegment(string $lwtTokenPayloadSegment)
{
// Декодируем тело из base64url
$payloadData = self::base64UrlDecode($lwtTokenPayloadSegment);
Expand Down Expand Up @@ -727,7 +750,7 @@ protected static function generateHmacKeyFromSignKey(): string
*
* @see https://www.php.net/manual/en/function.base64-encode.php
*/
public static function base64UrlEncode(mixed $inputData): string
public static function base64UrlEncode($inputData): string
{
// Кодируем данные в формате base64
$base64EncodedData = base64_encode($inputData);
Expand Down Expand Up @@ -799,7 +822,7 @@ public static function base64UrlDecode(string $inputData): string
* @see https://www.php.net/manual/en/function.json-decode.php
* @see https://www.php.net/manual/en/function.json-last-error.php
*/
protected static function jsonDecode(string $jsonString): mixed
protected static function jsonDecode(string $jsonString)
{
// Декодируем JSON-строку с использованием указанных флагов
$decodedData = json_decode($jsonString, true, self::JSON_MAX_DEPTH, JSON_BIGINT_AS_STRING);
Expand Down Expand Up @@ -849,7 +872,7 @@ protected static function jsonDecode(string $jsonString): mixed
* @see https://www.php.net/manual/en/function.json-encode.php
* @see https://www.php.net/manual/en/function.json-last-error.php
*/
protected static function jsonEncode(mixed $inputData): string
protected static function jsonEncode($inputData): string
{
// Кодируем данные в формате JSON с использованием указанных флагов
$encodedData = json_encode($inputData, JSON_UNESCAPED_SLASHES);
Expand Down

0 comments on commit 0187358

Please sign in to comment.