b05e074456
Co-authored-by: ISMAIL MASSERAN <topaz@ISMAILs-Macbook.local> Co-authored-by: ISMAIL MASSERAN <topaz@Mac.dlinkrouter.local> Reviewed-on: #11
34 lines
865 B
PHP
34 lines
865 B
PHP
<?php
|
|
|
|
namespace Modules\ExternalSystem\Support;
|
|
|
|
class JwtSigner
|
|
{
|
|
/**
|
|
* @param array<string, mixed> $payload
|
|
*/
|
|
public static function sign(array $payload, string $secret): string
|
|
{
|
|
$header = [
|
|
'typ' => 'JWT',
|
|
'alg' => 'HS256',
|
|
];
|
|
|
|
$segments = [
|
|
self::base64UrlEncode(json_encode($header, JSON_THROW_ON_ERROR)),
|
|
self::base64UrlEncode(json_encode($payload, JSON_THROW_ON_ERROR)),
|
|
];
|
|
|
|
$signingInput = implode('.', $segments);
|
|
$signature = hash_hmac('sha256', $signingInput, $secret, true);
|
|
$segments[] = self::base64UrlEncode($signature);
|
|
|
|
return implode('.', $segments);
|
|
}
|
|
|
|
protected static function base64UrlEncode(string $data): string
|
|
{
|
|
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
|
|
}
|
|
}
|