|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Schnittstabil\Csrf\TokenService; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* A TokenService. |
|
7
|
|
|
*/ |
|
8
|
|
|
class TokenService implements TokenServiceInterface |
|
9
|
|
|
{ |
|
10
|
|
|
protected $generator; |
|
11
|
|
|
protected $validator; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Create a new TokenService. |
|
15
|
|
|
* |
|
16
|
|
|
* `$ttl` is used for calculating the expiration time of the tokens, its default value (1440sec === 24min) |
|
17
|
|
|
* correspond to the default `session.gc_maxlifetime`. |
|
18
|
|
|
* |
|
19
|
|
|
* @see http://php.net/manual/en/session.configuration.php Documentation of `session.gc-maxlifetime` |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $key Shared secret key used for generating token signatures |
|
22
|
|
|
* @param int $ttl Default Time to Live in seconds |
|
23
|
|
|
* @param string $algo Name of hashing algorithm. See hash_algos() for a list of supported algorithms |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($key, $ttl = 1440, $algo = 'SHA512') |
|
26
|
|
|
{ |
|
27
|
|
|
$signatory = new TokenSignatory($key, $algo); |
|
28
|
|
|
$this->generator = new TokenGenerator($signatory, $ttl); |
|
29
|
|
|
$this->validator = new TokenValidator($signatory); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Generate a CSRF token. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $nonce Value used to associate a client session |
|
36
|
|
|
* @param int $iat The time that the token was issued, defaults to `time()` |
|
37
|
|
|
* @param int $exp The expiration time, defaults to `$iat + $this->ttl` |
|
38
|
|
|
* |
|
39
|
|
|
* @return string |
|
40
|
|
|
* |
|
41
|
|
|
* @throws \InvalidArgumentException For invalid $iat and $exp arguments |
|
42
|
|
|
*/ |
|
43
|
|
|
public function generate($nonce, $iat = null, $exp = null) |
|
44
|
|
|
{ |
|
45
|
|
|
$generator = $this->generator; |
|
46
|
|
|
|
|
47
|
|
|
return $generator($nonce, $iat, $exp); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Determine constraint violations of CSRF tokens. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $nonce Value used to associate a client session |
|
54
|
|
|
* @param string $token The token to validate |
|
55
|
|
|
* @param int $now The current time, defaults to `time()` |
|
56
|
|
|
* |
|
57
|
|
|
* @return InvalidArgumentException[] Constraint violations; if $token is valid, an empty array |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getConstraintViolations($nonce, $token, $now = null, $leeway = 0) |
|
60
|
|
|
{ |
|
61
|
|
|
$validator = $this->validator; |
|
62
|
|
|
|
|
63
|
|
|
return $validator($nonce, $token, $now, $leeway); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Validate a CSRF token. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $nonce Value used to associate a client session |
|
70
|
|
|
* @param string $token The token to validate |
|
71
|
|
|
* @param int $now The current time, defaults to `time()` |
|
72
|
|
|
* @param int $leeway The leeway in seconds |
|
73
|
|
|
* |
|
74
|
|
|
* @return bool true iff $token is valid |
|
75
|
|
|
*/ |
|
76
|
|
|
public function validate($nonce, $token, $now = null, $leeway = 0) |
|
77
|
|
|
{ |
|
78
|
|
|
return count($this->getConstraintViolations($nonce, $token, $now, $leeway)) === 0; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|