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