Skip to content

Commit 19c0ee2

Browse files
author
Stanislav Humplik
committed
Fix analogic#18 - make HTTP client exchangable
Fix analogic#19 - Allow Defining challenge type Add #12 - adding LE registration contact
1 parent 96f460b commit 19c0ee2

File tree

1 file changed

+68
-8
lines changed

1 file changed

+68
-8
lines changed

Lescript.php

+68-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class Lescript
99
public $license = 'https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf';
1010
public $countryCode = 'CZ';
1111
public $state = "Czech Republic";
12+
public $challenge = 'http-01'; // http-01 challange only
13+
public $contact = array(); // optional
14+
// public $contact = array("mailto:cert-admin@example.com", "tel:+12025551212")
1215

1316
private $certificatesDir;
1417
private $webRootDir;
@@ -18,12 +21,12 @@ class Lescript
1821
private $client;
1922
private $accountKeyPath;
2023

21-
public function __construct($certificatesDir, $webRootDir, $logger = null)
24+
public function __construct($certificatesDir, $webRootDir, $logger = null, ClientInterface $client = null)
2225
{
2326
$this->certificatesDir = $certificatesDir;
2427
$this->webRootDir = $webRootDir;
2528
$this->logger = $logger;
26-
$this->client = new Client($this->ca);
29+
$this->client = $client ? $client : new Client($this->ca);
2730
$this->accountKeyPath = $certificatesDir . '/_account/private.pem';
2831
}
2932

@@ -72,9 +75,9 @@ public function signDomains(array $domains, $reuseCsr = false)
7275
throw new \RuntimeException("HTTP Challenge for $domain is not available. Whole response: ".json_encode($response));
7376
}
7477

75-
// choose http-01 challange only
76-
$challenge = array_reduce($response['challenges'], function ($v, $w) {
77-
return $v ? $v : ($w['type'] == 'http-01' ? $w : false);
78+
$self = $this;
79+
$challenge = array_reduce($response['challenges'], function ($v, $w) use (&$self) {
80+
return $v ? $v : ($w['type'] == $self->challenge ? $w : false);
7881
});
7982
if (!$challenge) throw new \RuntimeException("HTTP Challenge for $domain is not available. Whole response: " . json_encode($response));
8083

@@ -123,7 +126,7 @@ public function signDomains(array $domains, $reuseCsr = false)
123126
$challenge['uri'],
124127
array(
125128
"resource" => "challenge",
126-
"type" => "http-01",
129+
"type" => $this->challenge,
127130
"keyAuthorization" => $payload,
128131
"token" => $challenge['token']
129132
)
@@ -247,9 +250,14 @@ private function postNewReg()
247250
{
248251
$this->log('Sending registration to letsencrypt server');
249252

253+
$data = array('resource' => 'new-reg', 'agreement' => $this->license);
254+
if(!$this->contact) {
255+
$data['contact'] = $this->contact;
256+
}
257+
250258
return $this->signedRequest(
251259
'/acme/new-reg',
252-
array('resource' => 'new-reg', 'agreement' => $this->license)
260+
$data
253261
);
254262
}
255263

@@ -379,7 +387,59 @@ protected function log($message)
379387
}
380388
}
381389

382-
class Client
390+
interface ClientInterface
391+
{
392+
/**
393+
* Constructor
394+
*
395+
* @param string $base the ACME API base all relative requests are sent to
396+
*/
397+
public function __construct($base);
398+
/**
399+
* Send a POST request
400+
*
401+
* @param string $url URL to post to
402+
* @param array $data fields to sent via post
403+
* @return array|string the parsed JSON response, raw response on error
404+
*/
405+
public function post($url, $data);
406+
/**
407+
* @param string $url URL to request via get
408+
* @return array|string the parsed JSON response, raw response on error
409+
*/
410+
public function get($url);
411+
/**
412+
* Returns the Replay-Nonce header of the last request
413+
*
414+
* if no request has been made, yet. A GET on $base/directory is done and the
415+
* resulting nonce returned
416+
*
417+
* @return mixed
418+
*/
419+
public function getLastNonce();
420+
/**
421+
* Return the Location header of the last request
422+
*
423+
* returns null if last request had no location header
424+
*
425+
* @return string|null
426+
*/
427+
public function getLastLocation();
428+
/**
429+
* Return the HTTP status code of the last request
430+
*
431+
* @return int
432+
*/
433+
public function getLastCode();
434+
/**
435+
* Get all Link headers of the last request
436+
*
437+
* @return string[]
438+
*/
439+
public function getLastLinks();
440+
}
441+
442+
class Client implements ClientInterface
383443
{
384444
private $lastCode;
385445
private $lastHeader;

0 commit comments

Comments
 (0)