Skip to content

Commit

Permalink
Reauthenticate when the session provided by login timed out (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxBoeh authored May 9, 2024
1 parent 123c06c commit 8977b49
Showing 1 changed file with 31 additions and 74 deletions.
105 changes: 31 additions & 74 deletions src/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ class API {

private const API_ENDPOINT = 'https://ccp.netcup.net/run/webservice/servers/endpoint.php?JSON';

public const STATUS_CODE_SESSION_TIMEOUT = 4001;

private string $apiKey;
private string $customerId;
private string $apiSessionId;
private string $apiPassword;
private bool $logRequests = false;
private ?string $apiSessionId = null;

public function __construct(string $apiKey, string $apiPassword, int $customerId, bool $logRequests = false) {
$this->apiKey = $apiKey;
$this->apiPassword = $apiPassword;
$this->customerId = $customerId;
$this->logRequests = $logRequests;
$this->login($apiPassword);
}

public function isLoggedIn(): bool {
Expand All @@ -43,9 +46,6 @@ public function isLoggedIn(): bool {
* @throws NetcupException
*/
public function ackPoll(int $apiLogId): Response {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
return $this->request('ackpoll', ['apilogid' => $apiLogId]);
}

Expand All @@ -61,9 +61,6 @@ public function ackPoll(int $apiLogId): Response {
* @throws NetcupException
*/
public function cancelDomain(string $domainName): Response {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
return $this->request('cancelDomain', ['domainname' => $domainName]);
}

Expand All @@ -80,9 +77,6 @@ public function cancelDomain(string $domainName): Response {
* @untested
*/
public function changeOwnerDomain(int $newHandleId, string $domainName): Response {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
return $this->request('changeOwnerDomain', [
'new_handle_id' => $newHandleId,
'domainname' => $domainName
Expand All @@ -102,9 +96,6 @@ public function changeOwnerDomain(int $newHandleId, string $domainName): Respons
* @untested
*/
public function createDomain(string $domainName, $contacts, $nameservers): Response {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
return $this->request('createDomain', [
'domainname' => $domainName,
'contacts' => $contacts,
Expand Down Expand Up @@ -142,9 +133,6 @@ public function createHandle(
string $type = 'person',
string $organisation = '',
): Handle {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
$response = $this->request('createHandle', [
'type' => $type,
'name' => $name,
Expand Down Expand Up @@ -173,9 +161,6 @@ public function createHandle(
* @throws NetcupException
*/
public function deleteHandle(int $handleId): Response {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
return $this->request('deleteHandle', [
'handle_id' => $handleId
]);
Expand All @@ -192,9 +177,6 @@ public function deleteHandle(int $handleId): Response {
* @untested
*/
public function getAuthcodeDomain(string $domainName): Response {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
return $this->request('getAuthcodeDomain', [
'domainname' => $domainName
]);
Expand All @@ -210,9 +192,6 @@ public function getAuthcodeDomain(string $domainName): Response {
* @throws NetcupException
*/
public function infoDnsRecords(string $domainName): array {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
$response = $this->request('infoDnsRecords', [
'domainname' => $domainName
]);
Expand Down Expand Up @@ -243,9 +222,6 @@ public function infoDnsRecords(string $domainName): array {
* @untested
*/
public function infoDnsZone(string $domainName): Response {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
return $this->request('infoDnsZone', [
'domainname' => $domainName
]);
Expand All @@ -262,9 +238,6 @@ public function infoDnsZone(string $domainName): Response {
* @throws NotRegisteredAtNetcupException
*/
public function infoDomain(string $domainName): Domain {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
try {
$response = $this->request('infoDomain', ['domainname' => $domainName]);
if(!$response->wasSuccessful()) {
Expand All @@ -290,9 +263,6 @@ public function infoDomain(string $domainName): Domain {
* @throws NotLoggedInException
*/
public function infoHandle(int $handleId): Handle {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
$response = $this->request('infoHandle', [
'handle_id' => $handleId
]);
Expand All @@ -311,9 +281,6 @@ public function infoHandle(int $handleId): Handle {
* @throws NetcupException
*/
public function listAllDomains(): array {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
$response = $this->request('listallDomains');
if(!$response->wasSuccessful()) {
throw new NetcupException($response);
Expand All @@ -335,10 +302,6 @@ public function listAllDomains(): array {
* @throws NetcupException
*/
public function listAllHandle(): array {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}

$response = $this->request('listallHandle');
if(!$response->wasSuccessful()) {
throw new NetcupException($response);
Expand All @@ -352,28 +315,28 @@ public function listAllHandle(): array {
}

/**
* @param string $apiPassword
* @return bool
* @throws GuzzleException
* @throws NotLoggedInException
*/
private function login(string $apiPassword): bool {
private function login(): void {
$client = new Client();
$response = $client->post(self::API_ENDPOINT, [
'json' => [
'action' => 'login',
'param' => [
'apikey' => $this->apiKey,
'apipassword' => $apiPassword,
'apipassword' => $this->apiPassword,
'customernumber' => $this->customerId
]
]
]);
$json = json_decode($response->getBody());
if(isset($json->responsedata->apisessionid)) {
$this->apiSessionId = $json->responsedata->apisessionid;
return true;

if (!isset($json->responsedata->apisessionid)) {
throw new NotLoggedInException();
}
return false;

$this->apiSessionId = $json->responsedata->apisessionid;
}

/**
Expand All @@ -382,9 +345,6 @@ private function login(string $apiPassword): bool {
* @throws NetcupException
*/
public function logout(): bool {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
$response = $this->request('logout');
if(!$response->wasSuccessful()) {
throw new NetcupException($response);
Expand All @@ -407,10 +367,6 @@ public function logout(): bool {
* @throws NetcupException
*/
public function poll(int $messageCount = 10): Response {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}

return $this->request('poll', [
'messagecount' => $messageCount
]);
Expand All @@ -430,9 +386,6 @@ public function poll(int $messageCount = 10): Response {
* @untested
*/
public function priceTopleveldomain(string $topLevelDomain): Response {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
return $this->request('priceTopleveldomain', [
'topleveldomain' => $topLevelDomain
]);
Expand All @@ -458,9 +411,6 @@ public function transferDomain(): stdClass {
* @throws NetcupException
*/
public function updateDnsRecords(string $domainName, $dnsRecordSet): Response {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
return $this->request('updateDnsRecords', [
'domainname' => $domainName,
'dnsrecordset' => $dnsRecordSet
Expand All @@ -479,9 +429,6 @@ public function updateDnsRecords(string $domainName, $dnsRecordSet): Response {
* @untested
*/
public function updateDnsZone(string $domainName, $dnsZone): Response {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
return $this->request('updateDnsZone', [
'domainname' => $domainName,
'dnszone' => $dnsZone
Expand All @@ -504,9 +451,6 @@ public function updateDnsZone(string $domainName, $dnsZone): Response {
* @untested
*/
public function updateDomain(string $domainName, $contacts, $nameservers, $keepDnsSecRecords, $dnsSecEntries): Response {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
return $this->request('updateDomain', [
'domainname' => $domainName,
'contacts' => $contacts,
Expand Down Expand Up @@ -548,9 +492,6 @@ public function updateHandle(
string $telephone,
string $email
): Handle {
if(!$this->isLoggedIn()) {
throw new NotLoggedInException();
}
$response = $this->request('updateHandle', [
'handle_id' => $handleId,
'type' => $type,
Expand All @@ -575,8 +516,10 @@ public function updateHandle(
* @return Response
* @throws NetcupException
*/
private function request(string $action, array $param = []): Response {

private function request(string $action, array $param = [], int $remainingRetries = 3): Response {
if (! $this->isLoggedIn()) {
$this->login();
}
$requestId = uniqid();
$payload = [
'action' => $action,
Expand All @@ -598,6 +541,20 @@ private function request(string $action, array $param = []): Response {
$guzzleResponse = $client->post(self::API_ENDPOINT, ['json' => $payload]);
$response = new Response(json_decode($guzzleResponse->getBody()));
$this->writeToLog($requestId, 'Received "' . $response->getStatus() . '" with message "' . $response->getShortMessage() . '"');
if (
!$response->wasSuccessful()
&& $response->getStatusCode() === self::STATUS_CODE_SESSION_TIMEOUT
) {
// Other response details will be:
// - "shortmessage" = "Api session id in invalid format"
// - "longmessage" = "The session id is not ina valid format."

$this->apiSessionId = null;
if ($remainingRetries > 0) {
$this->request($action, $param, --$remainingRetries);
}
}

return $response;
} catch(GuzzleException) {
throw new NetcupException();
Expand Down

0 comments on commit 8977b49

Please sign in to comment.