Skip to content

Commit

Permalink
Update Client.php
Browse files Browse the repository at this point in the history
Refactored code in view to PHP 8 features.
Added signMultiple() method.
  • Loading branch information
JanSlabon committed Nov 17, 2022
1 parent 82abc0b commit 532a600
Showing 1 changed file with 64 additions and 60 deletions.
124 changes: 64 additions & 60 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace setasign\SetaPDF\Signer\Module\GlobalTrustTrust2Go;

use Exception;
use InvalidArgumentException;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
Expand All @@ -13,40 +12,13 @@

class Client
{
/**
* @var ClientInterface
*/
protected $httpClient;

/**
* @var RequestFactoryInterface
*/
protected $requestFactory;

/**
* @var StreamFactoryInterface
*/
protected $streamFactory;

/**
* @var string
*/
protected $apiUrl;

/**
* @var string
*/
protected $username;

/**
* @var string
*/
protected $transportPin;

/**
* @var string
*/
protected $language;
protected ClientInterface $httpClient;
protected RequestFactoryInterface $requestFactory;
protected StreamFactoryInterface $streamFactory;
protected string $apiUrl;
protected string $username;
protected string $transportPin;
protected string $language;

public function __construct(
ClientInterface $httpClient,
Expand Down Expand Up @@ -74,6 +46,7 @@ public function __construct(
* @return mixed
* @throws Exception
* @throws ClientExceptionInterface
* @throws \JsonException
* @see https://t2gtest.globaltrust.eu/trust2go/swagger-ui/index.html#/certificate-controller/getCertificates
*/
public function getCertificates(bool $activeonly = true, bool $useronly = false): array
Expand All @@ -97,13 +70,14 @@ public function getCertificates(bool $activeonly = true, bool $useronly = false)
$responseBody
));
}
return \json_decode($responseBody, true);
return \json_decode($responseBody, true, 512, JSON_THROW_ON_ERROR);
}

/**
* @param string $certificateSerialNumber
* @return array
* @throws ClientExceptionInterface
* @throws Exception
* @deprecated Use getCertificateBySerialNumber() instead.
* @see getCertificateBySerialNumber()
*/
Expand All @@ -117,6 +91,7 @@ public function getCertificatesBySerialNumber(string $certificateSerialNumber):
* @return array{certificate: string, chain: array<string>}
* @throws ClientExceptionInterface
* @throws Exception
* @throws \JsonException
*/
public function getCertificateBySerialNumber(string $certificateSerialNumber): array
{
Expand Down Expand Up @@ -166,27 +141,46 @@ static function ($certificate) use ($certificateSerialNumber) {
* @return string
* @throws ClientExceptionInterface
* @throws Exception
* @throws \JsonException
*/
public function sign(
string $certificateSerialNumber,
string $requestId,
string $hashAlgorithm,
string $hash
): string {
// todo multiple hashes
return $this->signMultiple($certificateSerialNumber, $requestId, $hashAlgorithm, [$hash])[0];
}

/**
* @param string $certificateSerialNumber The serial number of the certificate to be used
* @param string $requestId A requestID generated by the client to identify this signature operation (6 alphanumeric characters)
* @param string $hashAlgorithm
* @param array $hashes
* @return array
* @throws ClientExceptionInterface
* @throws Exception
* @throws \JsonException
*/
public function signMultiple(
string $certificateSerialNumber,
string $requestId,
string $hashAlgorithm,
array $hashes
): array {
$response = $this->httpClient->sendRequest(
$this->requestFactory->createRequest('POST', $this->apiUrl . '/api/v1/signers/usernames/sign')
->withHeader('Authorization', 'Basic ' . \base64_encode($this->username . ':' . $this->transportPin))
->withHeader('Accept', 'application/json')
->withHeader('Content-Type', 'application/json')
->withBody($this->streamFactory->createStream(\json_encode([
'language' => $this->language,
'requestId' => $requestId,
"certificateSerialNumber" => $certificateSerialNumber,
// allowed "sha224, sha256, sha384, sha512, sha3-224, sha3-256, sha3-384, sha3-512, ripemd128, ripemd160, ripemd256"
"hashAlgorithm" => $hashAlgorithm,
"hashes" => [$hash],
])))
->withHeader('Authorization', 'Basic ' . \base64_encode($this->username . ':' . $this->transportPin))
->withHeader('Accept', 'application/json')
->withHeader('Content-Type', 'application/json')
->withBody($this->streamFactory->createStream(\json_encode([
'language' => $this->language,
'requestId' => $requestId,
"certificateSerialNumber" => $certificateSerialNumber,
// allowed "sha224, sha256, sha384, sha512, sha3-224, sha3-256, sha3-384, sha3-512, ripemd128, ripemd160, ripemd256"
"hashAlgorithm" => $hashAlgorithm,
"hashes" => $hashes,
], JSON_THROW_ON_ERROR)))
);
$responseBody = $response->getBody()->getContents();
if ($response->getStatusCode() !== 200) {
Expand All @@ -197,23 +191,32 @@ public function sign(
));
}

$content = \json_decode($responseBody, true);
if ($content['signedHashes'][0]['hash'] !== $hash) {
throw new Exception('Hash mismatch');
$content = \json_decode($responseBody, true, 512, JSON_THROW_ON_ERROR);
$signatures = [];
foreach ($content['signedHashes'] as $key => $hashResult) {
if ($hashResult['statusMessage'] !== 'OK') {
throw new Exception('Status is NOT OK: ' . $hashResult['statusMessage']);
}
if ($hashResult['hash'] !== $hashes[$key]) {
throw new Exception('Hash mismatch for hash #' . $key . ' (' . $hashes[$key] . ' != ' . $hashResult['hash']);
}

$signatures[] = \base64_decode($hashResult['signedHash']);
}

return \base64_decode($content['signedHashes'][0]['signedHash']);
return $signatures;
}

/**
* @see https://t2gtest.globaltrust.eu/trust2go/swagger-ui/index.html#/sign-requests-controller/confirmJson
* @param string $requestId
* @param string $tan
* @return mixed
* @return array
* @throws ClientExceptionInterface
* @throws Exception
* @throws \JsonException
*/
public function smsConfirm(string $requestId, string $tan)
public function smsConfirm(string $requestId, string $tan): array
{
$response = $this->httpClient->sendRequest(
$this->requestFactory->createRequest('POST', $this->apiUrl . '/api/v1/signers/signrequests/confirm/json')
Expand All @@ -224,7 +227,7 @@ public function smsConfirm(string $requestId, string $tan)
'language' => $this->language,
'requestId' => $requestId,
'tan' => $tan,
])))
], JSON_THROW_ON_ERROR)))
);
$responseBody = $response->getBody()->getContents();
if ($response->getStatusCode() !== 200) {
Expand All @@ -235,17 +238,18 @@ public function smsConfirm(string $requestId, string $tan)
));
}

return \json_decode($responseBody, true);
return \json_decode($responseBody, true, 512, JSON_THROW_ON_ERROR);
}

/**
* @see https://t2gtest.globaltrust.eu/trust2go/swagger-ui/index.html#/sign-requests-controller/cancelJson
* @param string $requestId
* @return mixed
* @return array
* @throws ClientExceptionInterface
* @throws Exception
* @throws \JsonException
*/
public function smsCancel(string $requestId)
public function smsCancel(string $requestId): array
{
$response = $this->httpClient->sendRequest(
$this->requestFactory->createRequest('POST', $this->apiUrl . '/api/v1/signers/signrequests/cancel/json')
Expand All @@ -255,7 +259,7 @@ public function smsCancel(string $requestId)
->withBody($this->streamFactory->createStream(\json_encode([
'language' => $this->language,
'requestId' => $requestId,
])))
], JSON_THROW_ON_ERROR)))
);
$responseBody = $response->getBody()->getContents();
if ($response->getStatusCode() !== 200) {
Expand All @@ -266,6 +270,6 @@ public function smsCancel(string $requestId)
));
}

return \json_decode($responseBody, true);
return \json_decode($responseBody, true, 512, JSON_THROW_ON_ERROR);
}
}

0 comments on commit 532a600

Please sign in to comment.