Skip to content

Commit 91e028b

Browse files
committed
add getAuthCode
1 parent 65e43d1 commit 91e028b

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

src/Domains/Registrar.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,14 @@ public function transfer(string $domain, string $authCode, array $contacts, arra
7070
{
7171
return $this->adapter->transfer($domain, $authCode, $contacts, $nameservers);
7272
}
73+
74+
public function sendAuthCode(string $domain): array
75+
{
76+
return $this->adapter->sendAuthCode($domain);
77+
}
78+
79+
public function getAuthCode(string $domain): string
80+
{
81+
return $this->adapter->getAuthCode($domain);
82+
}
7383
}

src/Domains/Registrar/Adapter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,12 @@ abstract public function transfer(string $domain, string $authCode, array $conta
9595
* @return array
9696
*/
9797
abstract public function sendAuthCode(string $domain): array;
98+
99+
/**
100+
* Get the authorization code for an EPP domain
101+
*
102+
* @param string $domain
103+
* @return string
104+
*/
105+
abstract public function getAuthCode(string $domain): string;
98106
}

src/Domains/Registrar/Mock.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,23 @@ public function sendAuthCode(string $domain): array
475475
];
476476
}
477477

478+
/**
479+
* Get the authorization code for an EPP domain
480+
*
481+
* @param string $domain
482+
* @return string
483+
* @throws DomainsException
484+
*/
485+
public function getAuthCode(string $domain): string
486+
{
487+
if (!in_array($domain, $this->purchasedDomains)) {
488+
throw new DomainsException("Domain {$domain} not found in mock registry", self::RESPONSE_CODE_NOT_FOUND);
489+
}
490+
491+
// Generate a mock auth code based on the domain name for consistency
492+
return 'mock_' . substr(md5($domain), 0, 8);
493+
}
494+
478495
/**
479496
* Validate contacts
480497
*

src/Domains/Registrar/OpenSRS.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,42 @@ public function sendAuthCode(string $domain): array
686686
}
687687
}
688688

689+
/**
690+
* Get the authorization code for an EPP domain
691+
*
692+
* @param string $domain The EPP domain name for which to retrieve the auth code
693+
* @return string The authorization code
694+
* @throws DomainsException When the domain does not use EPP protocol or other errors occur
695+
*/
696+
public function getAuthCode(string $domain): string
697+
{
698+
try {
699+
$message = [
700+
'object' => 'domain',
701+
'action' => 'get',
702+
'domain' => $domain,
703+
'attributes' => [
704+
'type' => 'domain_auth_info',
705+
'limit' => 10,
706+
],
707+
];
708+
709+
$result = $this->send($message);
710+
$result = $this->sanitizeResponse($result);
711+
712+
$xpath = '//body/data_block/dt_assoc/item[@key="attributes"]/dt_assoc/item[@key="domain_auth_info"]';
713+
$elements = $result->xpath($xpath);
714+
715+
if (empty($elements)) {
716+
throw new DomainsException('Auth code not found in response', 404);
717+
}
718+
719+
return (string) $elements[0];
720+
} catch (Exception $e) {
721+
throw new DomainsException('Failed to get auth code: ' . $e->getMessage(), $e->getCode(), $e);
722+
}
723+
}
724+
689725
private function response(string $xml): array
690726
{
691727
$doc = $this->sanitizeResponse($xml);

tests/MockTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,40 @@ public function testSendAuthCode(): void
503503
$this->assertEquals('Message sent', $result['text']);
504504
}
505505

506+
public function testSendAuthCodeNotFound(): void
507+
{
508+
$this->expectException(DomainsException::class);
509+
$this->expectExceptionMessage('Domain notfound.com not found in mock registry');
510+
511+
$this->adapter->sendAuthCode('notfound.com');
512+
}
513+
514+
public function testGetAuthCode(): void
515+
{
516+
// Purchase a domain first
517+
$domain = 'testdomain.com';
518+
$this->adapter->purchase($domain, $this->createContact());
519+
520+
// Test getting auth code
521+
$authCode = $this->adapter->getAuthCode($domain);
522+
523+
$this->assertIsString($authCode);
524+
$this->assertNotEmpty($authCode);
525+
$this->assertStringStartsWith('mock_', $authCode);
526+
527+
// Test that the same domain always returns the same auth code
528+
$authCode2 = $this->adapter->getAuthCode($domain);
529+
$this->assertEquals($authCode, $authCode2);
530+
}
531+
532+
public function testGetAuthCodeNotFound(): void
533+
{
534+
$this->expectException(DomainsException::class);
535+
$this->expectExceptionMessage('Domain notfound.com not found in mock registry');
536+
537+
$this->adapter->getAuthCode('notfound.com');
538+
}
539+
506540
private function createContact(): Contact
507541
{
508542
return new Contact(

tests/OpenSRSTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,14 @@ public function testSendAuthCode(): void
331331
$this->assertEquals(200, $result['code']);
332332
}
333333

334+
public function testGetAuthCode(): void
335+
{
336+
$authCode = $this->client->getAuthCode($this->domain);
337+
338+
$this->assertIsString($authCode);
339+
$this->assertNotEmpty($authCode);
340+
}
341+
334342
private static function purchaseContact(string $suffix = ''): array
335343
{
336344
$contact = new Contact(

0 commit comments

Comments
 (0)