Skip to content

Commit

Permalink
Smart ID
Browse files Browse the repository at this point in the history
Added possibility to sign with smart id
Fixed comments
  • Loading branch information
kullarkert committed May 25, 2021
1 parent 787b4bd commit a7caadc
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 5 deletions.
111 changes: 109 additions & 2 deletions src/Hashcode/HashcodeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,57 @@ public function addContainer(string $container) : HashcodeContainer

return $this;
}

/**
* Checks if given container is hashcode container or not
*
* @return boolean
*/
public function isHashcodeContainer() : bool
{
$zip = new \ZipArchive();
$zip->open($this->filename);

return $zip->locateName('META-INF/hashcodes-sha256.xml') !== false;
}

/**
* Get hashcode container without data files
*
* @return HashcodeContainer
*/
public function getContainerWithoutFiles() : HashcodeContainer
{
$zip = new \ZipArchive();
$zip->open($this->filename);

foreach ($this->files as $file) {
$zip->deleteName($file->getName());
}

$zip->close();

return $this;
}

/**
* Get container datafiles list
*
* @return array
*/
public function getDataFiles() : array
{
$this->loadFilesFromContainer();

return $this->files;
}

/**
* Load container datafiles to files variable
*
* @return array
*/
public function loadFilesFromContainer() : HashcodeContainer
{
$zip = new \ZipArchive();
$zip->open($this->filename);
Expand All @@ -77,7 +121,7 @@ public function getDataFiles() : array

$zip->close();

return $this->files;
return $this;
}

/**
Expand Down Expand Up @@ -144,6 +188,55 @@ public function addFiles(array $files) : HashcodeContainer
return $this;
}

/**
* Add hascode files to container
*
* @return HashcodeContainer
*/
public function addHashcodeFiles() : HashcodeContainer
{
$zip = new \ZipArchive();
$zip->open($this->filename);

$zip->addFromString('META-INF/hashcodes-sha256.xml', $this->createHashcodeXml(256));
$zip->addFromString('META-INF/hashcodes-sha512.xml', $this->createHashcodeXml(512));

$zip->close();

return $this;
}

/**
* Create hashcode XML contents
*
* @param integer $hashFormat Hascode format 256 or 512
*
* @return string XML file content
*/
private function createHashcodeXml(int $hashFormat) : string
{
$xml = new \DOMDocument("1.0", "UTF-8");

$hashcodes = $xml->createElement('hashcodes');
$hashcodes = $xml->appendChild($hashcodes);

foreach ($this->files as $file) {
$entry = $xml->createElement('file-entry');

$entry->setAttribute('full-path', $file->getName());
$entry->setAttribute('size', $file->getSize());

if ($hashFormat == 256) {
$entry->setAttribute('hash', $file->getHashSha256());
} elseif ($hashFormat == 512) {
$entry->setAttribute('hash', $file->getHashSha512());
}
$hashcodes->appendChild($entry);
}

return $xml->saveXML();
}

/**
* Save container to file system
*
Expand All @@ -167,7 +260,7 @@ public function saveContainer() : bool
*
* @return bool Was successful or not
*/
public function saveContainerWithFiles() :bool
public function saveContainerWithFiles() : bool
{
if (!$this->saveContainer()) {
throw new SigaException("Could not save container to filesystem");
Expand All @@ -187,4 +280,18 @@ public function saveContainerWithFiles() :bool

return $return;
}

/**
* Convert datafile container to hashcode container
*
* @return string
*/
public function convertToHashcodeContainer() : bool
{
$this->loadFilesFromContainer()
->addHashcodeFiles()
;

return true;
}
}
20 changes: 20 additions & 0 deletions src/Hashcode/HashcodeDataFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ public function getContent() : string
{
return $this->fileData;
}

/**
* Get file sha256 hash
*
* @return string File hash
*/
public function getHashSha256() : string
{
return $this->fileHash256;
}

/**
* Get file sha512 hash
*
* @return string File hash
*/
public function getHashSha512() : string
{
return $this->fileHash512;
}

/**
* Convert File hashcode to SiGa format
Expand Down
74 changes: 74 additions & 0 deletions src/Service/SigaApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,78 @@ public function getLastResponse() : ?Response
{
return $this->lastResponse;
}

/**
* Get Smart-ID certificate choice
*
* @param string $containerId Container Id
* @param array $requestParams Smart-ID request params.
*
* @return array Response
*/
public function getSmartIdCertificateChoice(string $containerId, array $requestParams): array
{
$requestResponse = $this->client->post(
$this->getSigaApiUri(self::HASHCODE_ENDPOINT, [$containerId, 'smartidsigning', 'certificatechoice']),
['json' => $requestParams]
);

return $this->decodeResponse($requestResponse);
}

/**
* Get Smart ID certificate choice status
*
* @param string $containerId Container Id
* @param string $certificate Certificate
*
* @return array Response
*/
public function getSmartIdCertificateChoiceStatus(string $containerId, string $certificate): array
{
$requestResponse = $this->client->get(
$this->getSigaApiUri(self::HASHCODE_ENDPOINT, [$containerId, 'smartidsigning', 'certificatechoice', $certificate, 'status'])
);

return $this->decodeResponse($requestResponse);
}

/**
* Start Smart-ID signing process
*
* @link @https://github.com/open-eid/SiGa/wiki/Hashcode-API-description#smart-id-signing
*
* @param string $containerId Container Id
* @param array $requestParams Mobile Id request params.
*
* @return array Response
*/
public function startSmartIdSigning(string $containerId, array $requestParams) : array
{
$requestParams['signatureProfile'] = self::SIGNATURE_PROFILE_LT;

$requestResponse = $this->client->post(
$this->getSigaApiUri(self::HASHCODE_ENDPOINT, [$containerId, 'smartidsigning']),
['json' => $requestParams]
);

return $this->decodeResponse($requestResponse);
}

/**
* Get Smart-ID signing status
*
* @param string $containerId Container Id
* @param string $signatureId Signature Id
*
* @return array Response
*/
public function getSmartIdSigningStatus(string $containerId, string $signatureId) : array
{
$requestResponse = $this->client->get(
$this->getSigaApiUri(self::HASHCODE_ENDPOINT, [$containerId, 'smartidsigning', $signatureId, 'status'])
);

return $this->decodeResponse($requestResponse);
}
}
72 changes: 69 additions & 3 deletions src/SigaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,13 @@ private function deleteContainer() : void
/**
* Upload hashcode container
*
* @param string $fileString Base 64 encoded container
* @param string $fileString Container data
*
* @return string Container Id
*/
public function uploadHashcodeContainer(string $fileString) : string
public function uploadHashcodeContainer(string $fileContent) : string
{
$response = $this->sigaApiClient->uploadContainer(base64_encode($fileString));
$response = $this->sigaApiClient->uploadContainer(base64_encode($fileContent));

return $this->containerId = $response['containerId'];
}
Expand Down Expand Up @@ -362,4 +362,70 @@ public function getSignatureInfo(string $signatureId)
{
return $this->sigaApiClient->getSignatureInfo($this->containerId, $signatureId);
}

/**
* Get Smart-ID certificate choice
*
* @param array $requestParams Request params
*
* @return array Response
*/
public function getSmartIdCertificateChoice(array $requestParams) : array
{
if (!$this->containerId) {
throw new ContainerIdException();
}

return $this->sigaApiClient->getSmartIdCertificateChoice($this->containerId, $requestParams);
}

/**
* Get Smart ID certificate choice status
*
* @param string $certificate Certificate
*
* @return array Response
*/
public function getSmartIdCertificateStatus(string $certificate): array
{
if (!$this->containerId) {
throw new ContainerIdException();
}

return $this->sigaApiClient->getSmartIdCertificateChoiceStatus($this->containerId, $certificate);
}

/**
* Start Smart-ID signing process
*
* @link @https://github.com/open-eid/SiGa/wiki/Hashcode-API-description#smart-id-signing
*
* @param array $requestParams Request params
*
* @return array Response
*/
public function prepareSmartIdSigning(array $requestParams) : array
{
if (!$this->containerId) {
throw new ContainerIdException();
}

return $this->sigaApiClient->startSmartIdSigning($this->containerId, $requestParams);
}

/**
* Get Smart-ID signing status
*
* @param string $signatureId Signature Id
*
* @return array Response
*/
public function getSmartIdSigningStatus(string $signatureId) : array
{
if (!$this->containerId) {
throw new ContainerIdException();
}

return $this->sigaApiClient->getSmartIdSigningStatus($this->containerId, $signatureId);
}
}

0 comments on commit a7caadc

Please sign in to comment.