34 lines
959 B
PHP
34 lines
959 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\common\service\iam;
|
|
|
|
class IamSecurity
|
|
{
|
|
public static function signatureFor(string $secret, string $body): string
|
|
{
|
|
return 'sha256=' . hash_hmac('sha256', $body, $secret);
|
|
}
|
|
|
|
public static function verifySignature(string $secret, string $body, string $provided): bool
|
|
{
|
|
return $secret !== '' && hash_equals(self::signatureFor($secret, $body), trim($provided));
|
|
}
|
|
|
|
public static function base64UrlEncode(string $value): string
|
|
{
|
|
return rtrim(strtr(base64_encode($value), '+/', '-_'), '=');
|
|
}
|
|
|
|
public static function base64UrlDecode(string $value): string
|
|
{
|
|
$padding = (4 - strlen($value) % 4) % 4;
|
|
$decoded = base64_decode(strtr($value . str_repeat('=', $padding), '-_', '+/'), true);
|
|
if ($decoded === false) {
|
|
throw new IamHubException('Invalid base64url value');
|
|
}
|
|
return $decoded;
|
|
}
|
|
}
|