1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Boris Guéry <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Bgy\OAuth2; |
7
|
|
|
|
8
|
|
|
class AuthorizationServerConfigurationBuilder |
9
|
|
|
{ |
10
|
|
|
private $defaultOptions = [ |
11
|
|
|
'access_token_generator' => null, |
12
|
|
|
'always_require_a_client' => false, |
13
|
|
|
'access_token_length' => 32, |
14
|
|
|
'access_token_ttl' => 3600, |
15
|
|
|
'always_generate_a_refresh_token' => false, |
16
|
|
|
'revoke_refresh_token_when_used' => true, |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
private $options = [ |
20
|
|
|
'access_token_generator' => null, |
21
|
|
|
'always_require_a_client' => false, |
22
|
|
|
'access_token_length' => 32, |
23
|
|
|
'access_token_ttl' => 3600, |
24
|
|
|
'always_generate_a_refresh_token' => false, |
25
|
|
|
'revoke_refresh_token_when_used' => true, |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
private $built = false; |
29
|
|
|
|
30
|
|
|
private $configuration; |
31
|
|
|
|
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function alwaysRequireAClient($flag) |
37
|
|
|
{ |
38
|
|
|
$this->options['always_require_a_client'] = (bool) $flag; |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function shouldRevokeRefreshTokenWhenUsed($flag) |
44
|
|
|
{ |
45
|
|
|
$this->options['revoke_refresh_token_when_used'] = (bool) $flag; |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function alwaysGenerateARefreshToken($flag) |
51
|
|
|
{ |
52
|
|
|
$this->options['always_generate_a_refresh_token'] = (bool) $flag; |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function setAccessTokenTTL($ttlInSeconds) |
58
|
|
|
{ |
59
|
|
|
$this->options['access_token_ttl'] = (int) $ttlInSeconds; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function setAccessTokenGenerator($accessTokenGenerator) |
66
|
|
|
{ |
67
|
|
|
$this->options['access_token_generator'] = $accessTokenGenerator; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function build() |
73
|
|
|
{ |
74
|
|
|
// all options without a default value (not null) are required |
75
|
|
|
$missingOptions = array_filter($this->options, function ($value, $key) { |
76
|
|
|
return (null === $value); |
77
|
|
|
}, ARRAY_FILTER_USE_BOTH); |
78
|
|
|
|
79
|
|
|
if (count($missingOptions) > 0) { |
80
|
|
|
|
81
|
|
|
throw new \RuntimeException(sprintf( |
82
|
|
|
'You must configure the following options: %s', |
83
|
|
|
rtrim(implode(", ", array_keys($missingOptions)), ", ") |
84
|
|
|
)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->built = true; |
88
|
|
|
|
89
|
|
|
$this->configuration = new AuthorizationServerConfiguration( |
90
|
|
|
$this->options['access_token_generator'], |
91
|
|
|
[ |
92
|
|
|
'always_require_a_client' => $this->options['always_require_a_client'], |
93
|
|
|
'access_token_ttl' => $this->options['access_token_ttl'], |
94
|
|
|
'access_token_length' => $this->options['access_token_length'], |
95
|
|
|
'always_generate_a_refresh_token' => $this->options['always_generate_a_refresh_token'], |
96
|
|
|
] |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function reset() |
103
|
|
|
{ |
104
|
|
|
$this->options = $this->defaultOptions; |
105
|
|
|
|
106
|
|
|
$this->built = false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getAuthorizationServerConfiguration() |
110
|
|
|
{ |
111
|
|
|
if (!$this->built) { |
112
|
|
|
|
113
|
|
|
throw new \LogicException('You must build() the configuration before to get it.'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->configuration; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|