Skip to content

Commit

Permalink
Merge pull request #25 from VirgilSecurity/develop
Browse files Browse the repository at this point in the history
Add VirgilAgent (v5.1.0)
  • Loading branch information
PetrenkoAnton authored Jun 21, 2019
2 parents b3dd713 + ccd280c commit 4f820f1
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 95 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
vendor
.env
for-dev
49 changes: 16 additions & 33 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,24 @@ matrix:
include:
- os: linux
compiler: gcc
env: PHPUNIT_VERSION=7.5.1 PHP_VERSION=7.2
sudo: required
dist: trusty
dist: xenial
before_install:
- openssl aes-256-cbc -K $encrypted_e0df82fbaf3e_key -iv $encrypted_e0df82fbaf3e_iv -in .env.enc -out .env -d
- date -u
- uname -a
- if [[ "${TRAVIS_OS_NAME}" == "linux" ]]; then lscpu; fi
- openssl aes-256-cbc -K $encrypted_e0df82fbaf3e_key -iv $encrypted_e0df82fbaf3e_iv -in .env.enc -out .env -d
install:
- export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
- echo "Install PHP version ${PHP_VERSION} ..."
- travis_retry sudo apt-add-repository -y ppa:ondrej/php
- travis_retry sudo apt-get -qq update
- travis_retry sudo apt-get install -y -qq php${PHP_VERSION}
- travis_retry sudo apt-get install -y -qq php${PHP_VERSION}-cli
- travis_retry sudo apt-get install -y -qq php${PHP_VERSION}-dev
- travis_retry sudo apt-get install -y -qq php${PHP_VERSION}-mbstring
- travis_retry sudo apt-get install -y -qq php${PHP_VERSION}-curl
- echo "Install PHPUnit version ${PHPUNIT_VERSION} ..."
- travis_retry wget https://phar.phpunit.de/phpunit-${PHPUNIT_VERSION}.phar
- chmod +x phpunit-${PHPUNIT_VERSION}.phar
- sudo mv phpunit-${PHPUNIT_VERSION}.phar /usr/bin/phpunit
- echo "Install composer globally"
- curl -sS https://getcomposer.org/installer -o composer-setup.php
- sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
- php --version
- php -i
- phpunit --version
- php-config --extension-dir
- composer --version
- export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
- travis_retry sudo apt-get install software-properties-common
- travis_retry sudo add-apt-repository -y ppa:ondrej/php
- travis_retry sudo apt-get update
- travis_retry sudo apt install -y php7.2 php7.2-dev php7.2-cli
php7.2-mbstring php7.2-curl php7.2-xml php7.2-intl
- travis_retry wget https://phar.phpunit.de/phpunit-7.5.1.phar
- chmod +x phpunit-7.5.1.phar
- sudo mv phpunit-7.5.1.phar /usr/bin/phpunit
- curl -sS https://getcomposer.org/installer -o composer-setup.php
- sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
before_script:
- sudo cp $TRAVIS_BUILD_DIR/tests/extension/virgil_crypto_php.so /usr/lib/php/20170718
- cd /usr/lib/php/20170718
- ls -a
- sudo sh -c "echo 'extension=virgil_crypto_php.so' >> /etc/php/7.2/cli/php.ini"
- cd $TRAVIS_BUILD_DIR
- composer install
- sudo cp $TRAVIS_BUILD_DIR/tests/extension/php7.2/virgil_crypto_php.so /usr/lib/php/20170718
- sudo sh -c "echo 'extension=virgil_crypto_php.so' >> /etc/php/7.2/cli/php.ini"
- composer install
script: phpunit --configuration phpunit.xml
4 changes: 1 addition & 3 deletions Virgil/Http/Curl/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace Virgil\Http\Curl;


use Virgil\Http\AbstractHttpClient;

use Virgil\Http\Constants\RequestMethods;

use Virgil\Http\Responses\HttpResponse;
use Virgil\Http\Responses\HttpStatusCode;
use Virgil\Http\VirgilAgent\HttpVirgilAgent;

/**
* Class CurlClient
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (C) 2015-2019 Virgil Security Inc.
* Copyright (C) 2015-2019 Virgil Security Inc.
*
* All rights reserved.
*
Expand Down Expand Up @@ -35,25 +35,73 @@
* Lead Maintainer: Virgil Security Inc. <[email protected]>
*/

namespace Virgil\Http\VirgilAgent;

/**
* @return string
* Class HttpVirgilAgent
* @package Virgil\Http\Curl
*/
function extension_helper()
class HttpVirgilAgent
{
$params = [
// php -v
'PHP Version' => PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION,
// php-config --extension-dir
'PHP Extension Dir' => PHP_EXTENSION_DIR,
// php -i | grep System
'OS Version' => PHP_OS,
];

foreach ($params as $key => $value) {
echo $key . ": " . $value . "\n";
/**
* @var string
*/
private $name = 'Virgil-agent';
/**
* @var string
*/
private $product = 'sdk';
/**
* @var string
*/
private $family = 'php';

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

return true;
}
/**
* @return string
*/
public function getFormatString()
{
$os = strtolower(php_uname('s'));
return $this->product . ";" . $this->family . ";" . $os . ";" . $this->getVersion();
}

/**
* @return string
*/
private function getVersion()
{
$composerLock = 'composer.lock';
$packageName = 'virgil/crypto';
$version = 'unknown';

$path = __DIR__.DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR;

extension_helper();
if(!is_dir($path) || !in_array($composerLock, scandir($path)))
return $version;

$composerLockFile = file_get_contents($path.$composerLock);
$composerLockFileToArray = json_decode($composerLockFile);

$packages = $composerLockFileToArray->packages;

foreach ($packages as $package) {
if($packageName == $package->name) {
$version = $package->version;
if('v'==$version[0]) {
$version = ltrim($version, 'v');
}
break;
}
}

return $version;
}
}
10 changes: 9 additions & 1 deletion Virgil/Sdk/CardManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

use Virgil\CryptoApi\CardCrypto;

use Virgil\Http\VirgilAgent\HttpVirgilAgent;
use Virgil\Sdk\Signer\ModelSigner;

use Virgil\Sdk\Verification\CardVerifier;
Expand Down Expand Up @@ -89,6 +90,10 @@ class CardManager
*/
private $cardClient;

/**
* @var HttpVirgilAgent
*/
private $httpVirgilAgent;

public function __construct(
CardCrypto $cardCrypto,
Expand All @@ -97,8 +102,11 @@ public function __construct(
CardClient $cardClient = null,
callable $signCallback = null
) {
$this->httpVirgilAgent = new HttpVirgilAgent();

if ($cardClient == null) {
$cardClient = new CardClient();
$cardClient->setHttpVirgilAgent($this->httpVirgilAgent);
}

if ($cardVerifier == null) {
Expand Down Expand Up @@ -368,7 +376,7 @@ private function publishRawSignedModelWithToken(RawSignedModel $model, AccessTok
$model = $signCallback($model);
}

$responseModel = $this->cardClient->publishCard($model, (string)$token);
$responseModel = $this->cardClient->publishCard($model, (string)$token, $this->httpVirgilAgent);

if ($responseModel instanceof ErrorResponseModel) {
throw new CardClientException(
Expand Down
55 changes: 34 additions & 21 deletions Virgil/Sdk/Web/CardClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

use Virgil\Http\Requests\GetHttpRequest;
use Virgil\Http\Requests\PostHttpRequest;
use Virgil\Http\VirgilAgent\HttpVirgilAgent;


/**
Expand All @@ -64,12 +65,15 @@ class CardClient
*/
private $serviceUrl;

/**
* @var HttpVirgilAgent
*/
private $httpVirgilAgent;

/**
* CardClient constructor.
*
* @param string $serviceUrl
* @param HttpClientInterface $httpClient
* @param string $serviceUrl
* @param HttpClientInterface|null $httpClient
*/
public function __construct($serviceUrl = self::API_SERVICE_URL, HttpClientInterface $httpClient = null)
{
Expand All @@ -87,21 +91,29 @@ public function __construct($serviceUrl = self::API_SERVICE_URL, HttpClientInter
$this->serviceUrl = rtrim($serviceUrl, "/");
}

/**
* @param HttpVirgilAgent $httpVirgilAgent
*/
public function setHttpVirgilAgent(HttpVirgilAgent $httpVirgilAgent)
{
$this->httpVirgilAgent = $httpVirgilAgent;
}

/**
* @param RawSignedModel $model
* @param string $token
*
* @return RawSignedModel|ErrorResponseModel
*
* @param $token
* @return ErrorResponseModel|RawSignedModel
*/
public function publishCard(RawSignedModel $model, $token)
{
$httpResponse = $this->httpClient->send(
new PostHttpRequest(
$this->serviceUrl . "/card/v5",
json_encode($model, JSON_UNESCAPED_SLASHES),
["Authorization" => sprintf("Virgil %s", $token)]
[
"Authorization" => sprintf("Virgil %s", $token),
$this->httpVirgilAgent->getName() => $this->httpVirgilAgent->getFormatString(),
]
)
);

Expand All @@ -113,21 +125,21 @@ public function publishCard(RawSignedModel $model, $token)
return RawSignedModel::RawSignedModelFromJson($httpResponse->getBody());
}


/**
* @param string $cardID
* @param string $token
*
* @return ResponseModel|ErrorResponseModel
*
* @param $cardID
* @param $token
* @return ErrorResponseModel|ResponseModel
*/
public function getCard($cardID, $token)
{
$httpResponse = $this->httpClient->send(
new GetHttpRequest(
sprintf("%s/card/v5/%s", $this->serviceUrl, $cardID),
null,
["Authorization" => sprintf("Virgil %s", $token)]
[
"Authorization" => sprintf("Virgil %s", $token),
$this->httpVirgilAgent->getName() => $this->httpVirgilAgent->getFormatString(),
]
)
);

Expand All @@ -141,20 +153,21 @@ public function getCard($cardID, $token)
return new ResponseModel($httpResponse->getHeaders(), $rawSignedModel);
}


/**
* @param string $identity
* @param string $token
*
* @return RawSignedModel[]|ErrorResponseModel
* @param $identity
* @param $token
* @return array|ErrorResponseModel
*/
public function searchCards($identity, $token)
{
$httpResponse = $this->httpClient->send(
new PostHttpRequest(
$this->serviceUrl . "/card/v5/actions/search",
json_encode(["identity" => $identity], JSON_UNESCAPED_SLASHES),
["Authorization" => sprintf("Virgil %s", $token)]
[
"Authorization" => sprintf("Virgil %s", $token),
$this->httpVirgilAgent->getName() => $this->httpVirgilAgent->getFormatString(),
]
)
);

Expand Down
15 changes: 15 additions & 0 deletions _help/virgil-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

$virgil_crypto_php = 'virgil_crypto_php';

$result = [
'VIRGIL_CRYPTO_PHP_EXTENSION' => $virgil_crypto_php,
'IS_VIRGIL_CRYPTO_PHP_EXTENSION_LOADED' => extension_loaded($virgil_crypto_php),
'OS' => PHP_OS,
'PHP' => PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION,
'PATH_TO_EXTENSIONS_DIR' => PHP_EXTENSION_DIR,
'PATH_TO_PHP.INI' => php_ini_loaded_file(),
];

var_dump($result);
exit();
File renamed without changes.
14 changes: 9 additions & 5 deletions tests/src/IntegrationBaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use Virgil\CryptoImpl\VirgilAccessTokenSigner;
use Virgil\CryptoImpl\VirgilCardCrypto;
use Virgil\CryptoImpl\VirgilCrypto;
use Virgil\Http\VirgilAgent\HttpVirgilAgent;
use Virgil\Sdk\CardManager;
use Virgil\Sdk\Verification\CardVerifier;
use Virgil\Sdk\Verification\VirgilCardVerifier;
Expand Down Expand Up @@ -111,9 +112,9 @@ public function setUp()
{
parent::setUp();

(new Dotenv(__DIR__."/../.."))->load();
(new Dotenv(__DIR__ . "/../.."))->load();

defined('VIRGIL_FIXTURE_PATH') or define('VIRGIL_FIXTURE_PATH', __DIR__.'/../fixtures/');
defined('VIRGIL_FIXTURE_PATH') or define('VIRGIL_FIXTURE_PATH', __DIR__ . '/../fixtures/');

$this->serviceAddress = $_ENV['SERVICE_ADDRESS'];
$this->serviceKey = $_ENV['SERVICE_KEY'];
Expand All @@ -132,17 +133,20 @@ public function setUp()
$virgilAccessTokenSigner = new VirgilAccessTokenSigner();
$this->accessTokenProvider = new GeneratorJwtProvider(
new JwtGenerator($apiKey, $this->testApiKeyId, $virgilAccessTokenSigner, $this->testAppId, $_ENV['TTL']),
'default-identity'.date('Y-m-d-H-i-s')
'default-identity' . date('Y-m-d-H-i-s')
);
$this->cardVerifier = new VirgilCardVerifier($this->cardCrypto, true, true, [], $this->serviceKey);
$this->cardClient = new CardClient($this->serviceAddress);
$this->cardClient = new CardClient();
$this->cardClient->setHttpVirgilAgent(new HttpVirgilAgent());
}


protected function getCardManager()
{
return new CardManager(
$this->cardCrypto, $this->accessTokenProvider, $this->cardVerifier, $this->cardClient, $this->signCallback
$this->cardCrypto, $this->accessTokenProvider, $this->cardVerifier,
$this->cardClient,
$this->signCallback
);
}

Expand Down
Loading

0 comments on commit 4f820f1

Please sign in to comment.