-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathToken.php
50 lines (43 loc) · 1.22 KB
/
Token.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace KeycloakGuard;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
class Token
{
/**
* Decode a JWT token
*
* @param string $token
* @param string $publicKey
* @return mixed|null
*/
public static function decode(?string $token, string $publicKey, int $leeway = 0, string $algorithm = 'RS256')
{
JWT::$leeway = $leeway;
$publicKey = self::buildPublicKey($publicKey);
return $token ? JWT::decode($token, new Key($publicKey, $algorithm)) : null;
}
/**
* Build a valid public key from a string
*
* @param string $key
* @return mixed
*/
private static function buildPublicKey(string $key)
{
return "-----BEGIN PUBLIC KEY-----\n".wordwrap($key, 64, "\n", true)."\n-----END PUBLIC KEY-----";
}
/**
* Get the plain public key from a string
*
* @param string $key
* @return string
*/
public static function plainPublicKey(string $key): string
{
$string = str_replace('-----BEGIN PUBLIC KEY-----', '', $key);
$string = trim(str_replace('-----END PUBLIC KEY-----', '', $string));
$string = str_replace('\n', '', $string);
return $string;
}
}