Skip to content

Commit

Permalink
Expose authorization url, rename answerChallenge to finalizeChallenge
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Jul 3, 2021
1 parent 916c2c7 commit 3890a4d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/AcmeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ public function newOrder(
}

/**
* Answers a challenge and signals that the CA should validate it.
* Finalizes a challenge and signals that the CA should validate it.
*
* @param UriInterface $url URI of the challenge
*
* @return Promise<Challenge>
*/
public function answerChallenge(UriInterface $url): Promise
public function finalizeChallenge(UriInterface $url): Promise
{
return call(function () use ($url) {
/** @var Response $response */
Expand Down Expand Up @@ -164,7 +164,7 @@ public function getAuthorization(UriInterface $url): Promise
$response = yield $this->client->post($url, []);

try {
return Authorization::fromResponse(yield $response->getBody()->buffer());
return Authorization::fromResponse($url, yield $response->getBody()->buffer());
} catch (\Throwable $_) {
throw $this->generateException($response, yield $response->getBody()->buffer());
}
Expand Down
21 changes: 19 additions & 2 deletions src/Protocol/Authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@

namespace Kelunik\Acme\Protocol;

use Kelunik\Acme\AcmeException;
use League\Uri\Http;
use Psr\Http\Message\UriInterface;

final class Authorization
{
public static function fromResponse(string $payload): Authorization
public static function fromResponse(?string $url, string $payload): Authorization
{
return new self(...parseResponse($payload, [
if ($url === null) {
throw new AcmeException('Missing authorization URL');
}

return new self(Http::createFromString($url), ...parseResponse($payload, [
'identifier' => identifier(),
'status' => enum(AuthorizationStatus::getAll()),
'expires' => dateTime(),
Expand All @@ -22,6 +30,8 @@ public static function fromResponse(string $payload): Authorization
]));
}

private UriInterface $url;

/**
* @var Identifier The subjective identifier.
*/
Expand Down Expand Up @@ -53,19 +63,26 @@ public static function fromResponse(string $payload): Authorization
* @param Challenge[] $challenges
*/
public function __construct(
UriInterface $url,
Identifier $identifier,
string $status,
\DateTimeImmutable $expires,
array $challenges = [],
?bool $wildcard = false
) {
$this->url = $url;
$this->identifier = $identifier;
$this->status = $status;
$this->expires = $expires;
$this->challenges = $challenges;
$this->wildcard = $wildcard ?? false;
}

public function getUrl(): UriInterface
{
return $this->url;
}

public function isWildcard(): bool
{
return $this->wildcard;
Expand Down
5 changes: 3 additions & 2 deletions test/AcmeResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Kelunik\Acme\Protocol\Authorization;
use Kelunik\Acme\Protocol\Challenge;
use Kelunik\Acme\Protocol\Identifier;
use League\Uri\Http;
use PHPUnit\Framework\TestCase;
use function Kelunik\Acme\Protocol\identifier;

Expand Down Expand Up @@ -62,7 +63,7 @@ public function failsParseChallengeObject(): void
*/
public function parseAuthorizationObject(): void
{
$authorization = Authorization::fromResponse('{
$authorization = Authorization::fromResponse(Http::createFromString('http://example.com/'), '{
"status": "valid",
"expires": "2018-09-09T14:09:01.13Z",
Expand Down Expand Up @@ -97,7 +98,7 @@ public function failsParseIdentifierForAuthorizationObject(): void
$this->expectException(AcmeException::class);
$this->expectExceptionMessage('Invalid response');

Authorization::fromResponse('{
Authorization::fromResponse(Http::createFromString('http://example.com/'), '{
"status": "valid",
"expires": "2018-09-09T14:09:01.13Z",
Expand Down

0 comments on commit 3890a4d

Please sign in to comment.