Skip to content

Commit

Permalink
Squashed commit of the pr/18:
Browse files Browse the repository at this point in the history
commit 230886b
commit 78432a8
  • Loading branch information
jalukse committed Nov 25, 2019
1 parent a4ea07c commit 3c53267
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
language: php

php:
- 5.6
- 7.0.7

before_script:
- composer install

# bacause used in composer
script:
- vendor/bin/phpunit
- vendor/bin/phpunit
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [1.5] - RELEASE_DATE_HERE

### Added
- Http public key pinning [(pull request)](https://github.com/SK-EID/smart-id-php-client/pull/18)

### Fixed
- Poller did not use specified network interface [#4](https://github.com/SK-EID/smart-id-php-client/issues/4)
- Add exception message when user is not found [(commit)](https://github.com/SK-EID/smart-id-php-client/commit/053fe5f3b4bd715be305481e764d95aeddbe9d93)

### Changed
- php version 5.6 to 7.0.7
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The Smart-ID PHP client can be used for easy integration of the Smart-ID solutio
## Features
* Simple interface for user authentication

Smart-ID PHP client works with PHP 5.6 or later.
Smart-ID PHP client works with PHP 7.2 or later.

**This PHP client cannot be used to create digitally signed containers because PHP does not have a library like DigiDoc4J..**

Expand All @@ -20,6 +20,55 @@ The recommended way to install Smart-ID PHP Client is through [Composer]:
composer require sk-id-solutions/smart-id-php-client "~1.0"
```

## Https pinning

The client automatically trusts sk demo and live env public keys

Examples of configuring

When not specified the client will trust SK live and demo env keys

```PHP
$this->client = new Client();
$this->client
->setRelyingPartyUUID( "YOUR UUID" )
->setRelyingPartyName( "YOUR RP NAME" )
->setHostUrl("HOST_URL");

```

Trusting only live env public keys

```PHP
$this->client = new Client();
$this->client
->setRelyingPartyUUID( "YOUR UUID" )
->setRelyingPartyName( "YOUR RP NAME" )
->setHostUrl("HOST_URL")
->useOnlyLivePublicKey();
```

Trusting only demo env public keys

```PHP
$this->client = new Client();
$this->client
->setRelyingPartyUUID( "YOUR UUID" )
->setRelyingPartyName( "YOUR RP NAME" )
->setHostUrl("HOST_URL")
->useOnlyDemoPublicKey();
```

Trusting custom public keys

```PHP
$this->client = new Client();
$this->client
->setRelyingPartyUUID( "YOUR UUID" )
->setRelyingPartyName( "YOUR RP NAME" )
->setHostUrl("HOST_URL")
->setPublicSslKeys("sha256//QLZIaH7Qx9Rjq3gyznQuNsvwMQb7maC5L4SLu/z5qNU=;sha256//R8b8SIj92sylUdok0DqfxJJN0yW2O3epE0B+5vpo2eM=);
```
## How to use it
Take a look at the [examples](https://github.com/SK-EID/smart-id-php-client/wiki/Examples-of-using-it)

Expand Down
1 change: 1 addition & 0 deletions src/Sk/SmartId/Api/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Authentication extends AbstractApi
public function createAuthentication()
{
$connector = new SmartIdRestConnector( $this->client->getHostUrl() );
$connector->setPublicSslKeys($this->client->getPublicSslKeys());
$sessionStatusPoller = $this->createSessionStatusPoller( $connector );
$builder = new AuthenticationRequestBuilder( $connector, $sessionStatusPoller );
$this->populateBuilderFields( $builder );
Expand Down
9 changes: 9 additions & 0 deletions src/Sk/SmartId/Api/SmartIdRestConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class SmartIdRestConnector implements SmartIdConnector
*/
private $curl;

private $publicSslKeys;

/**
* @param string $endpointUrl
*/
Expand Down Expand Up @@ -154,6 +156,7 @@ private function postAuthenticationRequest( $url, AuthenticationSessionRequest $
private function postRequest( $url, array $params, $responseType )
{
$this->curl = new Curl();
$this->curl->setPublicSslKeys($this->publicSslKeys);
$this->setNetworkInterface( $params );
$this->curl->curlPost( $url, array(), json_encode( $params ) );
$this->curl->setCurlParam( CURLOPT_HTTPHEADER, array('content-type: application/json',) );
Expand All @@ -170,6 +173,7 @@ private function postRequest( $url, array $params, $responseType )
private function getRequest( $url, array $params, $responseType )
{
$this->curl = new Curl();
$this->curl->setPublicSslKeys($this->publicSslKeys);
$this->setNetworkInterface( $params );
$this->curl->curlGet( $url, $params );
return $this->request( $url, $responseType );
Expand Down Expand Up @@ -232,4 +236,9 @@ private function setNetworkInterface( array &$params )
unset( $params[ 'networkInterface' ] );
}
}

public function setPublicSslKeys(string $sslKeys)
{
$this->publicSslKeys = $sslKeys;
}
}
41 changes: 40 additions & 1 deletion src/Sk/SmartId/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@

class Client
{
const VERSION = '5.0';
const
DEMO_SID_PUBLIC_KEY = "sha256//QLZIaH7Qx9Rjq3gyznQuNsvwMQb7maC5L4SLu/z5qNU=",
RP_API_PUBLIC_KEY_VALID_FROM_2016_12_20_TO_2020_01_19 = "sha256//R8b8SIj92sylUdok0DqfxJJN0yW2O3epE0B+5vpo2eM=",
RP_API_PUBLIC_KEY_VALID_FROM_2019_11_01_TO_2021_11_05 = "sha256//l2uvq6ftLN4LZ+8Un+71J2vH1BT9wTbtrE5+Fj3Vc5g=",
VERSION = '5.0';

/**
* @var array
Expand All @@ -55,6 +59,11 @@ class Client
*/
private $hostUrl;

/**
* @var string
*/
private $sslKeys;

/**
* @param string $apiName
* @throws InvalidArgumentException
Expand Down Expand Up @@ -145,4 +154,34 @@ public function getHostUrl()
{
return $this->hostUrl;
}

public function setPublicSslKeys(string $sslKeys)
{
$this->sslKeys = $sslKeys;

return $this;
}

public function useOnlyDemoPublicKey()
{
$this->sslKeys = self::DEMO_SID_PUBLIC_KEY;

return $this;
}

public function useOnlyLivePublicKey()
{
$this->sslKeys = self::RP_API_PUBLIC_KEY_VALID_FROM_2016_12_20_TO_2020_01_19.";".self::RP_API_PUBLIC_KEY_VALID_FROM_2019_11_01_TO_2021_11_05;

return $this;
}

public function getPublicSslKeys()
{
if($this->sslKeys === null)
{
$this->sslKeys = self::DEMO_SID_PUBLIC_KEY.";".self::RP_API_PUBLIC_KEY_VALID_FROM_2016_12_20_TO_2020_01_19.";".self::RP_API_PUBLIC_KEY_VALID_FROM_2019_11_01_TO_2021_11_05;
}
return $this->sslKeys;
}
}
13 changes: 11 additions & 2 deletions src/Sk/SmartId/Util/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
* #L%
*/
namespace Sk\SmartId\Util;
defined('CURLOPT_PINNEDPUBLICKEY') || define('CURLOPT_PINNEDPUBLICKEY', 10230);

use Exception;

class Curl
{

const
GET = 1,
POST = 2,
Expand All @@ -44,7 +46,8 @@ class Curl
$requestMethod = self::GET,
$importCookies = false,
$includeHeaders = false,
$curlTimeout = 600;
$curlTimeout = 600,
$publicSslKeys;

/**
* @throws Exception
Expand Down Expand Up @@ -195,6 +198,7 @@ protected function sendRequest()
curl_setopt( $this->curl, CURLOPT_FOLLOWLOCATION, $this->followLocation );
curl_setopt( $this->curl, CURLOPT_TIMEOUT, $this->curlTimeout );
curl_setopt( $this->curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $this->curl, CURLOPT_PINNEDPUBLICKEY, $this->publicSslKeys);

if ( self::POST === $this->requestMethod )
{
Expand Down Expand Up @@ -350,7 +354,12 @@ public function getError()
return false;
}

/**
public function setPublicSslKeys(string $public_keys)
{
$this->publicSslKeys = $public_keys;
}

/**
* @param int $option
* @return array|mixed
*/
Expand Down
1 change: 1 addition & 0 deletions src/resources/ssl_public_keys/demo.sid.public.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sha256//QLZIaH7Qx9Rjq3gyznQuNsvwMQb7maC5L4SLu/z5qNU=
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sha256//R8b8SIj92sylUdok0DqfxJJN0yW2O3epE0B+5vpo2eM=
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sha256//l2uvq6ftLN4LZ+8Un+71J2vH1BT9wTbtrE5+Fj3Vc5g=
1 change: 1 addition & 0 deletions tests/Sk/SmartId/Tests/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use PHPUnit\Framework\TestCase;
use Sk\SmartId\Client;
use Sk\SmartId\Tests\Api\DummyData;
use Sk\SmartId\Util\Curl;

class Setup extends TestCase
{
Expand Down
100 changes: 100 additions & 0 deletions tests/Sk/SmartId/Tests/SslTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Sk\SmartId\Tests;

use Sk\SmartId\Api\Data\AuthenticationHash;
use Sk\SmartId\Api\Data\AuthenticationSessionRequest;
use Sk\SmartId\Api\Data\DigestCalculator;
use Sk\SmartId\Api\Data\HashType;
use Sk\SmartId\Api\SmartIdRestConnector;
use Sk\SmartId\Exception\SmartIdException;
use Sk\SmartId\Tests\Api\DummyData;
use Sk\SmartId\Util\Curl;

class SslTest extends Setup
{
/**
* @test
*/
public function authenticate_demoEnv_success()
{
$this->client->authentication()
->createAuthentication()
->withCertificateLevel(DummyData::CERTIFICATE_LEVEL)
->withAuthenticationHash(new AuthenticationHash(DigestCalculator::calculateDigest( DummyData::SIGNABLE_TEXT, HashType::SHA512 )))
->withDocumentNumber(DummyData::VALID_DOCUMENT_NUMBER)
->authenticate();
}

/**
* @test
*/
public function authenticate_demoEnvUseDemoEnvPublicKeys_success()
{
$this->client->useOnlyDemoPublicKey()->authentication()
->createAuthentication()
->withCertificateLevel(DummyData::CERTIFICATE_LEVEL)
->withAuthenticationHash(new AuthenticationHash(DigestCalculator::calculateDigest( DummyData::SIGNABLE_TEXT, HashType::SHA512 )))
->withDocumentNumber(DummyData::VALID_DOCUMENT_NUMBER)
->authenticate();
}


/**
* @test
*/
public function authenticate_demoEnvUseLiveEnvPublicKeys_shouldThrowException()
{
$this->expectException(SmartIdException::class);

$this->client->useOnlyLivePublicKey()->authentication()
->createAuthentication()
->withCertificateLevel(DummyData::CERTIFICATE_LEVEL)
->withAuthenticationHash(new AuthenticationHash(DigestCalculator::calculateDigest( DummyData::SIGNABLE_TEXT, HashType::SHA512 )))
->withDocumentNumber(DummyData::VALID_DOCUMENT_NUMBER)
->authenticate();
}

/**
* @test
*/
public function authenticate_demoEnvSetPublicKeysFromArray_success()
{
$this->client->setPublicSslKeys("sha256//QLZIaH7Qx9Rjq3gyznQuNsvwMQb7maC5L4SLu/z5qNU=;sha256//R8b8SIj92sylUdok0DqfxJJN0yW2O3epE0B+5vpo2eM=")->authentication()
->createAuthentication()
->withCertificateLevel(DummyData::CERTIFICATE_LEVEL)
->withAuthenticationHash(new AuthenticationHash(DigestCalculator::calculateDigest( DummyData::SIGNABLE_TEXT, HashType::SHA512 )))
->withDocumentNumber(DummyData::VALID_DOCUMENT_NUMBER)
->authenticate();
}

/**
* @test
*/
public function authenticate_demoEnvSetPublicKeysFromEmptyString_throwsException()
{
$this->expectException(SmartIdException::class);
$this->client->setPublicSslKeys("")->authentication()
->createAuthentication()
->withCertificateLevel(DummyData::CERTIFICATE_LEVEL)
->withAuthenticationHash(new AuthenticationHash(DigestCalculator::calculateDigest( DummyData::SIGNABLE_TEXT, HashType::SHA512 )))
->withDocumentNumber(DummyData::VALID_DOCUMENT_NUMBER)
->authenticate();
}

/**
* @test
*/
public function makeRequestToGoogle_demoPublicKeys_shouldThrowException()
{
$this->expectException(SmartIdException::class);
$this->client
->setHostUrl("https://www.google.com")
->useOnlyDemoPublicKey()->authentication()
->createAuthentication()
->withCertificateLevel(DummyData::CERTIFICATE_LEVEL)
->withAuthenticationHash(new AuthenticationHash(DigestCalculator::calculateDigest( DummyData::SIGNABLE_TEXT, HashType::SHA512 )))
->withDocumentNumber(DummyData::VALID_DOCUMENT_NUMBER)
->authenticate();
}
}
1 change: 1 addition & 0 deletions tests/resources/ssl_public_keys/demo.sid.public.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sha256//QLZIaH7Qx9Rjq3gyznQuNsvwMQb7maC5L4SLu/z5qNU=
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sha256//R8b8SIj92sylUdok0DqfxJJN0yW2O3epE0B+5vpo2eM=
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sha256//l2uvq6ftLN4LZ+8Un+71J2vH1BT9wTbtrE5+Fj3Vc5g=
1 change: 1 addition & 0 deletions tests/resources/ssl_public_keys/wrong.public.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sha256//fqp7yWK7iGGKj+3unYdm2DA3VCPDkwtyX+DrdZYSC6o=

0 comments on commit 3c53267

Please sign in to comment.