Skip to content

Commit

Permalink
Merge pull request #3 from shellrent/apicache
Browse files Browse the repository at this point in the history
Caching implementation for /directory API call
  • Loading branch information
svdigital-development authored Aug 23, 2024
2 parents fb8e543 + 2d4c7c3 commit b8baa76
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
16 changes: 14 additions & 2 deletions src/LEClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ class LEClient
* @param array $certificateKeys Optional array containing location of all certificate files. Required paths are public_key, private_key, order and certificate/fullchain_certificate (you can use both or only one of them)
* @param string $accountKeys The directory in which the account keys are stored. Is a subdir inside $certificateKeys. Defaults to '__account/'.(optional)
* @param array $accountKeys Optional array containing location of account private and public keys. Required paths are private_key, public_key.
* @param array $leDirectoryConfig Optional array containing URLs obtained by "/directory" API
*/
public function __construct($email, $acmeURL = LEClient::LE_PRODUCTION, $log = LEClient::LOG_OFF, $certificateKeys = 'keys/', $accountKeys = '__account/')
public function __construct($email, $acmeURL = LEClient::LE_PRODUCTION, $log = LEClient::LOG_OFF, $certificateKeys = 'keys/', $accountKeys = '__account/', $leDirectoryConfig = null)
{
$this->log = $log;

Expand Down Expand Up @@ -152,7 +153,7 @@ public function __construct($email, $acmeURL = LEClient::LE_PRODUCTION, $log = L
throw LEClientException::InvalidArgumentException('accountKeys must be string or array.');
}

$this->connector = new LEConnector($this->log, $this->baseURL, $this->accountKeys);
$this->connector = new LEConnector($this->log, $this->baseURL, $this->accountKeys, $leDirectoryConfig);
$this->account = new LEAccount($this->connector, $this->log, $email, $this->accountKeys);

if($this->log instanceof \Psr\Log\LoggerInterface)
Expand All @@ -173,6 +174,17 @@ public function getAccount()
return $this->account;
}


/**
* Returns the LetsEncrypt connector used in the current client.
*
* @return LEConnector
*/
public function getConnector()
{
return $this->connector;
}

/**
* Returns a LetsEncrypt order. If an order exists, this one is returned. If not, a new order is created and returned.
*
Expand Down
31 changes: 21 additions & 10 deletions src/LEConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,42 @@ class LEConnector

/**
* Initiates the LetsEncrypt Connector class.
*
* @param int $log The level of logging. Defaults to no logging. LOG_OFF, LOG_STATUS, LOG_DEBUG accepted.
* @param string $baseURL The LetsEncrypt server URL to make requests to.
* @param array $accountKeys Array containing location of account keys files.
* @param array $leDirectoryConfig Optional array containing URLs obtained by "/directory" API
*/
public function __construct($log, $baseURL, $accountKeys)
public function __construct($log, $baseURL, $accountKeys, $leDirectoryConfig = null)
{
$this->baseURL = $baseURL;
$this->accountKeys = $accountKeys;
$this->log = $log;
$this->getLEDirectory();
$this->getLEDirectory( $leDirectoryConfig );
$this->getNewNonce();
}

/**
* Requests the LetsEncrypt Directory and stores the necessary URLs in this LetsEncrypt Connector instance.
* @param array $leDirectoryConfig Optional array containing URLs obtained by "/directory" API
*/
private function getLEDirectory()
private function getLEDirectory( $leDirectoryConfig = null )
{
$req = $this->get('/directory');
$this->keyChange = $req['body']['keyChange'];
$this->newAccount = $req['body']['newAccount'];
$this->newNonce = $req['body']['newNonce'];
$this->newOrder = $req['body']['newOrder'];
$this->revokeCert = $req['body']['revokeCert'];
if( $leDirectoryConfig ) {
$this->keyChange = $leDirectoryConfig['keyChange'];
$this->newAccount = $leDirectoryConfig['newAccount'];
$this->newNonce = $leDirectoryConfig['newNonce'];
$this->newOrder = $leDirectoryConfig['newOrder'];
$this->revokeCert = $leDirectoryConfig['revokeCert'];

} else {
$req = $this->get('/directory');

$this->keyChange = $req['body']['keyChange'];
$this->newAccount = $req['body']['newAccount'];
$this->newNonce = $req['body']['newNonce'];
$this->newOrder = $req['body']['newOrder'];
$this->revokeCert = $req['body']['revokeCert'];
}
}

/**
Expand Down

0 comments on commit b8baa76

Please sign in to comment.