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

Add support for transit/sign call #16

Merged
merged 1 commit into from
Aug 26, 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
100 changes: 100 additions & 0 deletions src/VaultPHP/SecretEngines/Engines/Transit/Request/SignDataRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace VaultPHP\SecretEngines\Engines\Transit\Request;

use VaultPHP\SecretEngines\Interfaces\ResourceRequestInterface;

final class SignDataRequest implements ResourceRequestInterface
{
const HASH_ALGORITHM_SHA1 = 'sha1';

const HASH_ALGORITHM_SHA2_224 = 'sha2-224';

const HASH_ALGORITHM_SHA2_256 = 'sha2-265';

const HASH_ALGORITHM_SHA2_384 = 'sha2-384';

const HASH_ALGORITHM_SHA2_512 = 'sha2-512';

const HASH_ALGORITHM_SHA3_224 = 'sha3-224';

const HASH_ALGORITHM_SHA3_256 = 'sha3-265';

const HASH_ALGORITHM_SHA3_384 = 'sha3-384';

const HASH_ALGORITHM_SHA3_512 = 'sha3-512';

const SIGNATURE_ALGORITHM_PSS = 'pss';

const SIGNATURE_ALGORITHM_PKCS1V15 = 'pkcs1v15';

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

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

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

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

/**
* @param string $key
* @param string $input
* @param string $signature_algorithm
*/
public function __construct($key, $hashAlgorithm, $input, $signature_algorithm = self::SIGNATURE_ALGORITHM_PSS)
{
$this->key = $key;
$this->hashAlgorithm = $hashAlgorithm;
$this->input = $input;
$this->signature_algorithm = $signature_algorithm;
}

/**
* @return string
*/
public function getKey()
{
return $this->key;
}

/**
* @return string
*/
public function getHashAlgorithm()
{
return $this->hashAlgorithm;
}

/**
* @return string
*/
public function getInput()
{
return $this->input;
}

/**
* @return string
*/
public function getSignatureAlgorithm()
{
return $this->signature_algorithm;
}

/**
* @return array
*/
public function toArray()
{
return [
'input' => $this->input,
'signature_algorithm' => $this->signature_algorithm
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace VaultPHP\SecretEngines\Engines\Transit\Response;

use VaultPHP\Response\EndpointResponse;

final class SignDataResponse extends EndpointResponse
{
/** @var string */
protected $signature = '';

/**
* @return string
*/
public function getSignature()
{
return $this->signature;
}
}
19 changes: 19 additions & 0 deletions src/VaultPHP/SecretEngines/Engines/Transit/Transit.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
use VaultPHP\SecretEngines\Engines\Transit\Request\DecryptData\DecryptDataRequest;
use VaultPHP\SecretEngines\Engines\Transit\Request\EncryptData\EncryptDataBulkRequest;
use VaultPHP\SecretEngines\Engines\Transit\Request\EncryptData\EncryptDataRequest;
use VaultPHP\SecretEngines\Engines\Transit\Request\SignDataRequest;
use VaultPHP\SecretEngines\Engines\Transit\Request\UpdateKeyConfigRequest;
use VaultPHP\SecretEngines\Engines\Transit\Response\CreateKeyResponse;
use VaultPHP\SecretEngines\Engines\Transit\Response\DecryptDataResponse;
use VaultPHP\SecretEngines\Engines\Transit\Response\DeleteKeyResponse;
use VaultPHP\SecretEngines\Engines\Transit\Response\EncryptDataResponse;
use VaultPHP\SecretEngines\Engines\Transit\Response\ListKeysResponse;
use VaultPHP\SecretEngines\Engines\Transit\Response\SignDataResponse;
use VaultPHP\SecretEngines\Engines\Transit\Response\UpdateKeyConfigResponse;

/**
Expand Down Expand Up @@ -187,4 +189,21 @@ public function updateKeyConfig(UpdateKeyConfigRequest $updateKeyConfigRequest)
$updateKeyConfigRequest
);
}

/**
* @param SignDataRequest $signDataRequest
* @return SignDataResponse
* @throws InvalidDataException
* @throws InvalidRouteException
* @throws VaultException
*/
public function sign(SignDataRequest $signDataRequest)
{
return $this->vaultClient->sendApiRequest(
'POST',
sprintf('/v1/%s/sign/%s/%s', $this->APIPath, urlencode($signDataRequest->getKey()), $signDataRequest->getHashAlgorithm()),
SignDataResponse::class,
$signDataRequest
);
}
}
43 changes: 43 additions & 0 deletions tests/VaultPHP/SecretEngines/Engines/Transit/SignDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Test\VaultPHP\SecretEngines\Engines\Transit;

use Test\VaultPHP\SecretEngines\AbstractSecretEngineTestCase;
use VaultPHP\SecretEngines\Engines\Transit\Request\SignDataRequest;
use VaultPHP\SecretEngines\Engines\Transit\Response\SignDataResponse;
use VaultPHP\SecretEngines\Engines\Transit\Transit;

/**
* Class SignDataTest
* @package Test\VaultPHP\SecretEngines\Transit
*/
final class SignDataTest extends AbstractSecretEngineTestCase
{
public function testApiCall()
{
$client = $this->createApiClient(
'POST',
'/v1/transit/sign/test/sha1',
[
'input' => 'some-input-to-sign',
'signature_algorithm' => 'pss'
],
[
'data' => [
'signature' => 'vault:v1:someHash',
]
]
);
$request = new SignDataRequest(
'test',
SignDataRequest::HASH_ALGORITHM_SHA1,
'some-input-to-sign',
SignDataRequest::SIGNATURE_ALGORITHM_PSS
);
$api = new Transit($client);
$response = $api->sign($request);

$this->assertInstanceOf(SignDataResponse::class, $response);
$this->assertEquals('vault:v1:someHash', $response->getSignature());
}
}
Loading