1
|
|
|
<?php |
2
|
|
|
namespace LE_ACME2\Cache; |
3
|
|
|
|
4
|
|
|
use LE_ACME2\Connector; |
5
|
|
|
use LE_ACME2\Order; |
6
|
|
|
use LE_ACME2\Request; |
7
|
|
|
use LE_ACME2\Response; |
8
|
|
|
use LE_ACME2\Exception; |
9
|
|
|
use LE_ACME2\Utilities; |
10
|
|
|
use LE_ACME2\SingletonTrait; |
11
|
|
|
|
12
|
|
|
class OrderAuthorizationResponse extends AbstractKeyValuableCache { |
13
|
|
|
|
14
|
|
|
use SingletonTrait; |
15
|
|
|
|
16
|
|
|
private const _FILE_prefix = 'CacheAuthorizationResponse'; |
17
|
|
|
|
18
|
|
|
private $_responses = []; |
19
|
|
|
|
20
|
|
|
private function _getCacheFilePath(Order $order, string $authorizationUrl) : string { |
21
|
|
|
return $order->getKeyDirectoryPath() . self::_FILE_prefix . '-' . md5($authorizationUrl); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Order $order |
26
|
|
|
* @param string $authorizationUrl |
27
|
|
|
* @return Response\Authorization\Get |
28
|
|
|
* @throws Exception\ExpiredAuthorization |
29
|
|
|
* @throws Exception\InvalidResponse |
30
|
|
|
* @throws Exception\RateLimitReached |
31
|
|
|
*/ |
32
|
|
|
public function get(Order $order, string $authorizationUrl, string $challengeType): Response\Authorization\Get { |
33
|
|
|
|
34
|
|
|
$accountIdentifier = $this->_getObjectIdentifier($order->getAccount()); |
35
|
|
|
$orderIdentifier = $this->_getObjectIdentifier($order); |
36
|
|
|
|
37
|
|
|
if(!isset($this->_responses[$accountIdentifier][$orderIdentifier])) { |
38
|
|
|
$this->_responses[$accountIdentifier][$orderIdentifier] = []; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if(array_key_exists($authorizationUrl, $this->_responses[$accountIdentifier][$orderIdentifier])) { |
42
|
|
|
return $this->_responses[ $accountIdentifier ][ $orderIdentifier ][ $authorizationUrl ]; |
43
|
|
|
} |
44
|
|
|
$this->_responses[ $accountIdentifier ][ $orderIdentifier ][ $authorizationUrl ] = null; |
45
|
|
|
|
46
|
|
|
$cacheFile = $this->_getCacheFilePath($order, $authorizationUrl); |
47
|
|
|
|
48
|
|
|
if(file_exists($cacheFile)) { |
49
|
|
|
|
50
|
|
|
$rawResponse = Connector\RawResponse::getFromString(file_get_contents($cacheFile)); |
51
|
|
|
|
52
|
|
|
$response = new Response\Authorization\Get($rawResponse); |
53
|
|
|
if( |
54
|
|
|
($challenge = $response->getChallenge($challengeType)) && |
55
|
|
|
$challenge->status == Response\Authorization\Struct\Challenge::STATUS_PROGRESSING |
56
|
|
|
) { |
57
|
|
|
|
58
|
|
|
Utilities\Logger::getInstance()->add( |
|
|
|
|
59
|
|
|
Utilities\Logger::LEVEL_DEBUG, |
60
|
|
|
get_class() . '::' . __FUNCTION__ . ' (cache did not satisfy, status "' . $response->getStatus() . '")' |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$request = new Request\Authorization\Get($order->getAccount(), $authorizationUrl); |
64
|
|
|
$this->set($order, $authorizationUrl, $response = $request->getResponse()); |
65
|
|
|
return $response; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
Utilities\Logger::getInstance()->add( |
69
|
|
|
Utilities\Logger::LEVEL_DEBUG, |
70
|
|
|
get_class() . '::' . __FUNCTION__ . ' (from cache, status "' . $response->getStatus() . '")' |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$this->_responses[$accountIdentifier][$orderIdentifier][$authorizationUrl] = $response; |
74
|
|
|
|
75
|
|
|
return $response; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$request = new Request\Authorization\Get($order->getAccount(), $authorizationUrl); |
79
|
|
|
$this->set($order, $authorizationUrl, $response = $request->getResponse()); |
80
|
|
|
return $response; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function set(Order $order, string $authorizationUrl, Response\Authorization\Get $response = null) : void { |
84
|
|
|
|
85
|
|
|
$accountIdentifier = $this->_getObjectIdentifier($order->getAccount()); |
86
|
|
|
$orderIdentifier = $this->_getObjectIdentifier($order); |
87
|
|
|
|
88
|
|
|
$filePath = $this->_getCacheFilePath($order, $authorizationUrl); |
89
|
|
|
|
90
|
|
|
if($response === null) { |
91
|
|
|
|
92
|
|
|
unset($this->_responses[$accountIdentifier][$orderIdentifier][$authorizationUrl]); |
93
|
|
|
|
94
|
|
|
if(file_exists($filePath)) { |
95
|
|
|
unlink($filePath); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->_responses[$accountIdentifier][$orderIdentifier][$authorizationUrl] = $response; |
102
|
|
|
file_put_contents($filePath, $response->getRaw()->toString()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Clear the cache, when the next response could be different: |
107
|
|
|
* - Order has ended (certificate received) |
108
|
|
|
* - Authorization::Start |
109
|
|
|
* |
110
|
|
|
* @param Order $order |
111
|
|
|
* @throws Exception\ExpiredAuthorization |
112
|
|
|
* @throws Exception\InvalidResponse |
113
|
|
|
* @throws Exception\RateLimitReached |
114
|
|
|
*/ |
115
|
|
|
public function clear(Order $order) : void { |
116
|
|
|
|
117
|
|
|
$orderResponse = OrderResponse::getInstance()->get($order); |
|
|
|
|
118
|
|
|
foreach($orderResponse->getAuthorizations() as $authorization) { |
119
|
|
|
$this->set($order, $authorization, null); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |