1
|
|
|
<?php |
2
|
|
|
namespace Zwartpet\PHPCertificateToolbox; |
3
|
|
|
|
4
|
|
|
use Zwartpet\PHPCertificateToolbox\Exception\RuntimeException; |
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* LetsEncrypt Authorization class, getting LetsEncrypt authorization data associated with a LetsEncrypt Order instance. |
9
|
|
|
* |
10
|
|
|
* @author Youri van Weegberg <[email protected]> |
11
|
|
|
* @copyright 2018 Youri van Weegberg |
12
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
13
|
|
|
*/ |
14
|
|
|
class LEAuthorization |
15
|
|
|
{ |
16
|
|
|
private $connector; |
17
|
|
|
|
18
|
|
|
public $authorizationURL; |
19
|
|
|
public $identifier; |
20
|
|
|
public $status; |
21
|
|
|
public $expires; |
22
|
|
|
public $challenges; |
23
|
|
|
|
24
|
|
|
/** @var LoggerInterface */ |
25
|
|
|
private $log; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Initiates the LetsEncrypt Authorization class. Child of a LetsEncrypt Order instance. |
29
|
|
|
* |
30
|
|
|
* @param LEConnector $connector The LetsEncrypt Connector instance to use for HTTP requests. |
31
|
|
|
* @param LoggerInterface $log PSR-3 logger |
32
|
|
|
* @param string $authorizationURL The URL of the authorization, given by a LetsEncrypt order request. |
33
|
|
|
*/ |
34
|
56 |
|
public function __construct($connector, LoggerInterface $log, $authorizationURL) |
35
|
|
|
{ |
36
|
56 |
|
$this->connector = $connector; |
37
|
56 |
|
$this->log = $log; |
38
|
56 |
|
$this->authorizationURL = $authorizationURL; |
39
|
|
|
|
40
|
56 |
|
$sign = $this->connector->signRequestKid( |
41
|
56 |
|
null, |
42
|
56 |
|
$this->connector->accountURL, |
43
|
56 |
|
$this->authorizationURL |
44
|
|
|
); |
45
|
|
|
|
46
|
56 |
|
$post = $this->connector->post($this->authorizationURL, $sign); |
47
|
56 |
|
if ($post['status'] === 200) { |
48
|
56 |
|
$this->identifier = $post['body']['identifier']; |
49
|
56 |
|
$this->status = $post['body']['status']; |
50
|
56 |
|
$this->expires = $post['body']['expires']; |
51
|
56 |
|
$this->challenges = $post['body']['challenges']; |
52
|
|
|
} else { |
53
|
|
|
//@codeCoverageIgnoreStart |
54
|
|
|
$this->log->error("LEAuthorization::__construct cannot find authorization $authorizationURL"); |
55
|
|
|
//@codeCoverageIgnoreEnd |
56
|
|
|
} |
57
|
56 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Updates the data associated with the current LetsEncrypt Authorization instance. |
61
|
|
|
*/ |
62
|
|
|
|
63
|
2 |
|
public function updateData() |
64
|
|
|
{ |
65
|
2 |
|
$sign = $this->connector->signRequestKid( |
66
|
2 |
|
null, |
67
|
2 |
|
$this->connector->accountURL, |
68
|
2 |
|
$this->authorizationURL |
69
|
|
|
); |
70
|
|
|
|
71
|
2 |
|
$post = $this->connector->post($this->authorizationURL, $sign); |
72
|
2 |
|
if ($post['status'] === 200) { |
73
|
2 |
|
$this->identifier = $post['body']['identifier']; |
74
|
2 |
|
$this->status = $post['body']['status']; |
75
|
2 |
|
$this->expires = $post['body']['expires']; |
76
|
2 |
|
$this->challenges = $post['body']['challenges']; |
77
|
|
|
} else { |
78
|
|
|
//@codeCoverageIgnoreStart |
79
|
|
|
$this->log->error("LEAuthorization::updateData cannot find authorization " . $this->authorizationURL); |
80
|
|
|
//@codeCoverageIgnoreEnd |
81
|
|
|
} |
82
|
2 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets the challenge of the given $type for this LetsEncrypt Authorization instance. |
86
|
|
|
* Throws a Runtime Exception if the given $type is not found in this LetsEncrypt Authorization instance. |
87
|
|
|
* |
88
|
|
|
* @param string $type The type of verification. |
89
|
|
|
* Supporting LEOrder::CHALLENGE_TYPE_HTTP and LEOrder::CHALLENGE_TYPE_DNS. |
90
|
|
|
* |
91
|
|
|
* @return array Returns an array with the challenge of the requested $type. |
92
|
|
|
*/ |
93
|
6 |
|
public function getChallenge($type) |
94
|
|
|
{ |
95
|
6 |
|
foreach ($this->challenges as $challenge) { |
96
|
6 |
|
if ($challenge['type'] == $type) { |
97
|
6 |
|
return $challenge; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
//@codeCoverageIgnoreStart |
101
|
|
|
throw new RuntimeException( |
102
|
|
|
'No challenge found for type \'' . $type . '\' and identifier \'' . $this->identifier['value'] . '\'.' |
103
|
|
|
); |
104
|
|
|
//@codeCoverageIgnoreEnd |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|