1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LE_ACME2\Authorizer; |
4
|
|
|
|
5
|
|
|
use LE_ACME2\Response; |
6
|
|
|
|
7
|
|
|
use LE_ACME2\Cache; |
8
|
|
|
use LE_ACME2\Utilities; |
9
|
|
|
use LE_ACME2\Exception; |
10
|
|
|
|
11
|
|
|
use LE_ACME2\Account; |
12
|
|
|
use LE_ACME2\Order; |
13
|
|
|
|
14
|
|
|
abstract class AbstractAuthorizer { |
15
|
|
|
|
16
|
|
|
protected $_account; |
17
|
|
|
protected $_order; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* AbstractAuthorizer constructor. |
21
|
|
|
* |
22
|
|
|
* @throws Exception\InvalidResponse |
23
|
|
|
* @throws Exception\RateLimitReached |
24
|
|
|
* @throws Exception\ExpiredAuthorization |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Account $account, Order $order) { |
27
|
|
|
|
28
|
|
|
$this->_account = $account; |
29
|
|
|
$this->_order = $order; |
30
|
|
|
|
31
|
|
|
$this->_fetchAuthorizationResponses(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** @var Response\Authorization\Get[] $_authorizationResponses */ |
35
|
|
|
protected $_authorizationResponses = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @throws Exception\InvalidResponse |
39
|
|
|
* @throws Exception\RateLimitReached |
40
|
|
|
* @throws Exception\ExpiredAuthorization |
41
|
|
|
*/ |
42
|
|
|
protected function _fetchAuthorizationResponses() { |
43
|
|
|
|
44
|
|
|
if(!file_exists($this->_order->getKeyDirectoryPath() . 'private.pem')) { |
45
|
|
|
|
46
|
|
|
Utilities\Logger::getInstance()->add( |
|
|
|
|
47
|
|
|
Utilities\Logger::LEVEL_DEBUG, |
48
|
|
|
static::class . '::' . __FUNCTION__ . ' result suppressed (Order has finished already)', |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$orderResponse = Cache\OrderResponse::getInstance()->get($this->_order); |
|
|
|
|
55
|
|
|
|
56
|
|
|
foreach($orderResponse->getAuthorizations() as $authorization) { |
57
|
|
|
|
58
|
|
|
$this->_authorizationResponses[] = Cache\OrderAuthorizationResponse::getInstance() |
59
|
|
|
->get($this->_order, $authorization, $this->_getChallengeType()); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function _hasValidAuthorizationResponses() : bool { |
64
|
|
|
|
65
|
|
|
return count($this->_authorizationResponses) > 0; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function shouldStartAuthorization() : bool { |
69
|
|
|
|
70
|
|
|
foreach($this->_authorizationResponses as $response) { |
71
|
|
|
|
72
|
|
|
$challenge = $response->getChallenge($this->_getChallengeType()); |
73
|
|
|
if($challenge && $challenge->status == Response\Authorization\Struct\Challenge::STATUS_PENDING) { |
74
|
|
|
|
75
|
|
|
Utilities\Logger::getInstance()->add( |
76
|
|
|
Utilities\Logger::LEVEL_DEBUG, |
77
|
|
|
static::class . '::' . __FUNCTION__ . ' "Pending challenge found', |
78
|
|
|
[get_class($challenge) => $challenge] |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
abstract protected function _getChallengeType() : string; |
88
|
|
|
|
89
|
|
|
private $_progressed = false; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @throws Exception\AuthorizationInvalid |
93
|
|
|
* @throws Exception\InvalidResponse |
94
|
|
|
* @throws Exception\RateLimitReached |
95
|
|
|
* @throws Exception\ExpiredAuthorization |
96
|
|
|
*/ |
97
|
|
|
public function progress() { |
98
|
|
|
|
99
|
|
|
if($this->_progressed) { |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->_progressed = true; |
104
|
|
|
|
105
|
|
|
if(!$this->_hasValidAuthorizationResponses()) |
106
|
|
|
return; |
107
|
|
|
|
108
|
|
|
$existsNotValidChallenges = false; |
109
|
|
|
|
110
|
|
|
foreach($this->_authorizationResponses as $authorizationResponse) { |
111
|
|
|
|
112
|
|
|
$challenge = $authorizationResponse->getChallenge($this->_getChallengeType()); |
113
|
|
|
|
114
|
|
|
if($challenge && $this->_existsNotValidChallenges($challenge, $authorizationResponse)) { |
115
|
|
|
$existsNotValidChallenges = true; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->_finished = !$existsNotValidChallenges; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @throws Exception\AuthorizationInvalid |
124
|
|
|
*/ |
125
|
|
|
protected function _existsNotValidChallenges(Response\Authorization\Struct\Challenge $challenge, |
126
|
|
|
Response\Authorization\Get $authorizationResponse |
|
|
|
|
127
|
|
|
) : bool { |
128
|
|
|
|
129
|
|
|
if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_PENDING) { |
130
|
|
|
|
131
|
|
|
Utilities\Logger::getInstance()->add( |
132
|
|
|
Utilities\Logger::LEVEL_DEBUG, |
133
|
|
|
static::class . '::' . __FUNCTION__ . ' "Non valid challenge found', |
134
|
|
|
[get_class($challenge) => $challenge] |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
return true; |
138
|
|
|
} |
139
|
|
|
else if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_PROGRESSING) { |
140
|
|
|
|
141
|
|
|
// Should come back later |
142
|
|
|
return true; |
143
|
|
|
} |
144
|
|
|
else if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_VALID) { |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
else if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_INVALID) { |
148
|
|
|
throw new Exception\AuthorizationInvalid( |
149
|
|
|
'Received status "' . Response\Authorization\Struct\Challenge::STATUS_INVALID . '" while challenge should be verified' |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
else { |
153
|
|
|
|
154
|
|
|
throw new \RuntimeException('Challenge status "' . $challenge->status . '" is not implemented'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
protected $_finished = false; |
161
|
|
|
|
162
|
|
|
public function hasFinished() : bool { |
163
|
|
|
|
164
|
|
|
Utilities\Logger::getInstance()->add( |
165
|
|
|
Utilities\Logger::LEVEL_DEBUG, |
166
|
|
|
get_called_class() . '::' . __FUNCTION__, |
167
|
|
|
['finished' => $this->_finished] |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
return $this->_finished; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|