|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LE_ACME2\Authorizer; |
|
4
|
|
|
|
|
5
|
|
|
use LE_ACME2\Request; |
|
6
|
|
|
use LE_ACME2\Response; |
|
7
|
|
|
|
|
8
|
|
|
use LE_ACME2\Struct\ChallengeAuthorizationKey; |
|
9
|
|
|
use LE_ACME2\Utilities; |
|
10
|
|
|
use LE_ACME2\Exception; |
|
11
|
|
|
|
|
12
|
|
|
use LE_ACME2\Order; |
|
13
|
|
|
|
|
14
|
|
|
class HTTP extends AbstractAuthorizer { |
|
15
|
|
|
|
|
16
|
|
|
const TEST_TOKEN = 'test-token'; |
|
17
|
|
|
const TEST_CHALLENGE = 'test-challenge'; |
|
18
|
|
|
|
|
19
|
|
|
protected static $_directoryPath = null; |
|
20
|
|
|
|
|
21
|
|
|
public static function setDirectoryPath(string $directoryPath) { |
|
22
|
|
|
|
|
23
|
|
|
if(!file_exists($directoryPath)) { |
|
24
|
|
|
throw new \RuntimeException('HTTP authorization directory path does not exist'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
self::$_directoryPath = realpath($directoryPath) . DIRECTORY_SEPARATOR; |
|
28
|
|
|
|
|
29
|
|
|
if(!file_exists(self::$_directoryPath . 'test-token')) { |
|
30
|
|
|
file_put_contents(self::$_directoryPath . self::TEST_TOKEN, self::TEST_CHALLENGE); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public static function getDirectoryPath() : ?string { |
|
35
|
|
|
return self::$_directoryPath; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function _getChallengeType(): string { |
|
39
|
|
|
return Order::CHALLENGE_TYPE_HTTP; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param Response\Authorization\Struct\Challenge $challenge |
|
44
|
|
|
* @param Response\Authorization\Get $authorizationResponse |
|
45
|
|
|
* @return bool |
|
46
|
|
|
* |
|
47
|
|
|
* @throws Exception\AuthorizationInvalid |
|
48
|
|
|
* @throws Exception\ExpiredAuthorization |
|
49
|
|
|
* @throws Exception\InvalidResponse |
|
50
|
|
|
* @throws Exception\RateLimitReached |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function _existsNotValidChallenges(Response\Authorization\Struct\Challenge $challenge, |
|
53
|
|
|
Response\Authorization\Get $authorizationResponse |
|
54
|
|
|
) : bool { |
|
55
|
|
|
|
|
56
|
|
|
Utilities\Logger::getInstance()->add( |
|
|
|
|
|
|
57
|
|
|
Utilities\Logger::LEVEL_DEBUG, |
|
58
|
|
|
'Challenge "' . $challenge->token . '" has status:' . $challenge->status |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_PENDING) { |
|
62
|
|
|
|
|
63
|
|
|
$this->_writeToFile($challenge); |
|
64
|
|
|
if($this->_validateFile($authorizationResponse->getIdentifier()->value, $challenge)) { |
|
65
|
|
|
|
|
66
|
|
|
$request = new Request\Authorization\Start($this->_account, $this->_order, $challenge); |
|
67
|
|
|
/* $response = */ $request->getResponse(); |
|
68
|
|
|
} else { |
|
69
|
|
|
|
|
70
|
|
|
Utilities\Logger::getInstance()->add(Utilities\Logger::LEVEL_INFO, 'Could not validate HTTP Authorization file'); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_INVALID) { |
|
75
|
|
|
throw new Exception\HTTPAuthorizationInvalid( |
|
76
|
|
|
'Received status "' . Response\Authorization\Struct\Challenge::STATUS_INVALID . '" while challenge should be verified' |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return parent::_existsNotValidChallenges($challenge, $authorizationResponse); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function _writeToFile(Response\Authorization\Struct\Challenge $challenge) : void { |
|
84
|
|
|
|
|
85
|
|
|
file_put_contents( |
|
86
|
|
|
self::$_directoryPath . $challenge->token, |
|
87
|
|
|
(new ChallengeAuthorizationKey($this->_account))->get($challenge->token) |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $domain |
|
93
|
|
|
* @param Response\Authorization\Struct\Challenge $challenge |
|
94
|
|
|
* @return bool |
|
95
|
|
|
* |
|
96
|
|
|
* @throws Exception\HTTPAuthorizationInvalid |
|
97
|
|
|
*/ |
|
98
|
|
|
private function _validateFile(string $domain, Response\Authorization\Struct\Challenge $challenge) : bool { |
|
99
|
|
|
|
|
100
|
|
|
$challengeAuthorizationKey = new ChallengeAuthorizationKey($this->_account); |
|
101
|
|
|
|
|
102
|
|
|
$expectedResponse = $challengeAuthorizationKey->get($challenge->token); |
|
103
|
|
|
$response = Utilities\ChallengeHTTP::fetch($domain, $challenge->token); |
|
104
|
|
|
|
|
105
|
|
|
if($response != $expectedResponse) { |
|
106
|
|
|
|
|
107
|
|
|
throw new Exception\HTTPAuthorizationInvalid( |
|
108
|
|
|
'HTTP challenge for "' . $domain . '"": ' . |
|
109
|
|
|
$domain . '/.well-known/acme-challenge/' . $challenge->token . |
|
110
|
|
|
' tested, found invalid.' . PHP_EOL . |
|
111
|
|
|
'- Expected: ' . var_export($expectedResponse, true) . PHP_EOL . |
|
112
|
|
|
'- Response: ' . var_export($response, true) |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
return true; |
|
116
|
|
|
} |
|
117
|
|
|
} |