@@ -9,6 +9,9 @@ class Lescript
9
9
public $ license = 'https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf ' ;
10
10
public $ countryCode = 'CZ ' ;
11
11
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")
12
15
13
16
private $ certificatesDir ;
14
17
private $ webRootDir ;
@@ -18,12 +21,12 @@ class Lescript
18
21
private $ client ;
19
22
private $ accountKeyPath ;
20
23
21
- public function __construct ($ certificatesDir , $ webRootDir , $ logger = null )
24
+ public function __construct ($ certificatesDir , $ webRootDir , $ logger = null , ClientInterface $ client = null )
22
25
{
23
26
$ this ->certificatesDir = $ certificatesDir ;
24
27
$ this ->webRootDir = $ webRootDir ;
25
28
$ this ->logger = $ logger ;
26
- $ this ->client = new Client ($ this ->ca );
29
+ $ this ->client = $ client ? $ client : new Client ($ this ->ca );
27
30
$ this ->accountKeyPath = $ certificatesDir . '/_account/private.pem ' ;
28
31
}
29
32
@@ -72,9 +75,9 @@ public function signDomains(array $domains, $reuseCsr = false)
72
75
throw new \RuntimeException ("HTTP Challenge for $ domain is not available. Whole response: " .json_encode ($ response ));
73
76
}
74
77
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 );
78
81
});
79
82
if (!$ challenge ) throw new \RuntimeException ("HTTP Challenge for $ domain is not available. Whole response: " . json_encode ($ response ));
80
83
@@ -123,7 +126,7 @@ public function signDomains(array $domains, $reuseCsr = false)
123
126
$ challenge ['uri ' ],
124
127
array (
125
128
"resource " => "challenge " ,
126
- "type " => " http-01 " ,
129
+ "type " => $ this -> challenge ,
127
130
"keyAuthorization " => $ payload ,
128
131
"token " => $ challenge ['token ' ]
129
132
)
@@ -247,9 +250,14 @@ private function postNewReg()
247
250
{
248
251
$ this ->log ('Sending registration to letsencrypt server ' );
249
252
253
+ $ data = array ('resource ' => 'new-reg ' , 'agreement ' => $ this ->license );
254
+ if (!$ this ->contact ) {
255
+ $ data ['contact ' ] = $ this ->contact ;
256
+ }
257
+
250
258
return $ this ->signedRequest (
251
259
'/acme/new-reg ' ,
252
- array ( ' resource ' => ' new-reg ' , ' agreement ' => $ this -> license )
260
+ $ data
253
261
);
254
262
}
255
263
@@ -379,7 +387,59 @@ protected function log($message)
379
387
}
380
388
}
381
389
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
383
443
{
384
444
private $ lastCode ;
385
445
private $ lastHeader ;
0 commit comments