|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System Framework package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace O2System\Security\Generators; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
use O2System\Security\Encryptions\Algorithm; |
|
19
|
|
|
use O2System\Security\Encryptions\Hmac; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class Signature |
|
23
|
|
|
* @package O2System\Security\Generators |
|
24
|
|
|
*/ |
|
25
|
|
|
class Signature |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Signature::generate |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $segments |
|
31
|
|
|
* @param string $key |
|
32
|
|
|
* @param string $algorithm |
|
33
|
|
|
* |
|
34
|
|
|
* @return bool|string |
|
35
|
|
|
* @throws \O2System\Spl\Exceptions\Logic\DomainException |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function generate(array $segments, $key, $algorithm = 'HS256') |
|
38
|
|
|
{ |
|
39
|
|
|
if (count($segments) == 2) { |
|
40
|
|
|
$data = implode('.', $segments); |
|
41
|
|
|
|
|
42
|
|
|
if (Algorithm::validate($algorithm)) { |
|
43
|
|
|
list($function, $algorithm) = Algorithm::map($algorithm); |
|
44
|
|
|
|
|
45
|
|
|
switch ($function) { |
|
46
|
|
|
case 'HMAC': |
|
47
|
|
|
return Hmac::hash($algorithm, $data, $key, true); |
|
48
|
|
|
case 'hash_hmac': |
|
49
|
|
|
return hash_hmac($algorithm, $data, $key, true); |
|
50
|
|
|
case 'openssl': |
|
51
|
|
|
return openssl_sign($data, $signature, $key, $algorithm); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// ------------------------------------------------------------------------ |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Signature::verify |
|
63
|
|
|
* |
|
64
|
|
|
* Verify token with signature. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $token |
|
67
|
|
|
* @param string $signature |
|
68
|
|
|
* @param string $key |
|
69
|
|
|
* @param string $algorithm |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function verify($token, $signature, $key, $algorithm = 'HS256') |
|
74
|
|
|
{ |
|
75
|
|
|
$segments = explode('.', $token); |
|
76
|
|
|
$segments = array_map('trim', $segments); |
|
77
|
|
|
|
|
78
|
|
|
if (count($segments) == 3) { |
|
79
|
|
|
array_pop($segments); |
|
80
|
|
|
$data = implode('.', $segments); |
|
81
|
|
|
|
|
82
|
|
|
if (Algorithm::validate($algorithm)) { |
|
83
|
|
|
list($function, $algorithm) = Algorithm::map($algorithm); |
|
84
|
|
|
|
|
85
|
|
|
switch ($function) { |
|
86
|
|
|
case 'HMAC': |
|
87
|
|
|
return Hmac::hash($algorithm, $data, $key, true) === $signature; |
|
88
|
|
|
case 'hash_hmac': |
|
89
|
|
|
return hash_hmac($algorithm, $data, $key, true) === $signature; |
|
90
|
|
|
case 'openssl': |
|
91
|
|
|
switch ($algorithm) { |
|
92
|
|
|
case 'RS256': |
|
93
|
|
|
return (bool)openssl_verify($data, $signature, $key, OPENSSL_ALGO_SHA1); |
|
94
|
|
|
case 'RS256': |
|
95
|
|
|
return (bool)openssl_verify($data, $signature, $key, OPENSSL_ALGO_SHA256); |
|
96
|
|
|
case 'RS384': |
|
97
|
|
|
return (bool)openssl_verify($data, $signature, $key, OPENSSL_ALGO_SHA384); |
|
98
|
|
|
case 'RS512': |
|
99
|
|
|
return (bool)openssl_verify($data, $signature, $key, OPENSSL_ALGO_SHA512); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
} |