Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the nameserver of the zone for local DNS validation #43

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"ext-mbstring": "*",
"ext-openssl": "*",
"psr/log": "^3.0",
"spatie/dns": "^2.5",
"spatie/laravel-data": "^3.9"
},
"require-dev": {
Expand Down
31 changes: 27 additions & 4 deletions src/Support/LocalChallengeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace Rogierw\RwAcme\Support;

use Exception;
use Rogierw\RwAcme\Exceptions\DomainValidationException;
use Rogierw\RwAcme\Interfaces\HttpClientInterface;
use Spatie\Dns\Dns;

class LocalChallengeTest
{
private const DEFAULT_NAMESERVER = 'dns.google.com';

public static function http(string $domain, string $token, string $keyAuthorization, HttpClientInterface $httpClient): void
{
$response = $httpClient->get($domain . '/.well-known/acme-challenge/' . $token, maxRedirects: 1);
Expand All @@ -29,10 +33,29 @@ public static function http(string $domain, string $token, string $keyAuthorizat

public static function dns(string $domain, string $name, string $value): void
{
$response = @dns_get_record(sprintf('%s.%s', $name, $domain), DNS_TXT);

if (!in_array($value, array_column($response, 'txt'), true)) {
throw DomainValidationException::localDnsChallengeTestFailed($domain);
try {
$dnsResolver = new Dns();

// Get the nameserver.
$soaRecord = $dnsResolver->getRecords($domain, DNS_SOA);

$nameserver = empty($soaRecord)
? self::DEFAULT_NAMESERVER
: $soaRecord[0]->mname();

$records = $dnsResolver
->useNameserver($nameserver)
->getRecords(sprintf('%s.%s', $name, $domain), DNS_TXT);

foreach ($records as $record) {
if ($record->txt() === $value) {
return;
}
}
} catch (Exception $exception) {
// An exception can be thrown by the Dns class when a lookup fails.
}

throw DomainValidationException::localDnsChallengeTestFailed($domain);
}
}