Skip to content

Commit

Permalink
Merge branch 'feature/avoid-is-test' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
garex committed May 18, 2018
2 parents 0fae916 + 95fceb3 commit a4d02bb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ $provider = new EsiaProvider([
'clientId' => 'XXXXXX',
'redirectUri' => 'https://your-system.domain/auth/finish/',
'defaultScopes' => ['openid', 'fullname', '...'],
// 'isTest' => true, // Allows to work with test portal version
// For work with test portal version
// 'remoteUrl' => 'https://esia-portal1.test.gosuslugi.ru',
// 'remoteCertificatePath' => EsiaProvider::RESOURCES.'esia.test.cer',
], [
'signer' => new OpensslPkcs7('/path/to/public/certificate.cer', '/path/to/private.key')
]);
Expand Down
18 changes: 10 additions & 8 deletions src/Provider/EsiaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class EsiaProvider extends AbstractProvider implements ProviderInterface
{
use BearerAuthorizationTrait;

protected $isTest = false;
const RESOURCES = __DIR__.'/../../resources/';

protected $defaultScopes = ['openid', 'fullname'];

protected $remoteCertificatePath;
protected $remoteUrl = 'https://esia.gosuslugi.ru';

protected $remoteCertificatePath = self::RESOURCES.'esia.prod.cer';

/**
* @var SignerInterface
Expand All @@ -40,9 +42,11 @@ class EsiaProvider extends AbstractProvider implements ProviderInterface
public function __construct(array $options = [], array $collaborators = [])
{
parent::__construct($options, $collaborators);
if (empty($this->remoteCertificatePath)) {
$this->remoteCertificatePath = __DIR__.'/../../resources/';
$this->remoteCertificatePath .= $this->isTest ? 'esia.test.cer' : 'esia.prod.cer';
if (!filter_var($this->remoteUrl, FILTER_VALIDATE_URL)) {
throw new InvalidArgumentException('Remote URL is not provided!');
}
if (!file_exists($this->remoteCertificatePath)) {
throw new InvalidArgumentException('Remote certificate is not provided!');
}

if (isset($collaborators['signer']) && $collaborators['signer'] instanceof SignerInterface) {
Expand Down Expand Up @@ -153,9 +157,7 @@ private function getResourceOwnerEmbeds(ScopedTokenInterface $token)

private function getUrl($path)
{
$host = $this->isTest ? 'esia-portal1.test.gosuslugi.ru' : 'esia.gosuslugi.ru';

return 'https://'.$host.$path;
return $this->remoteUrl.$path;
}

protected function getDefaultScopes()
Expand Down
3 changes: 2 additions & 1 deletion tests/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Bramus\Monolog\Formatter\ColoredLineFormatter;
use Bramus\Monolog\Formatter\ColorSchemes\TrafficLight;
use Ekapusta\OAuth2Esia\Provider\EsiaProvider;
use Ekapusta\OAuth2Esia\Token\EsiaAccessToken;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Key;
Expand All @@ -15,7 +16,7 @@

class Factory
{
const KEYS = __DIR__.'/../resources/';
const KEYS = EsiaProvider::RESOURCES;

/**
* @return LoggerInterface
Expand Down
21 changes: 20 additions & 1 deletion tests/Provider/EsiaProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ protected function setUp()
$this->provider = new EsiaProvider([
'clientId' => 'EKAP01',
'redirectUri' => $this->redirectUri,
'isTest' => true,
'remoteUrl' => 'https://esia-portal1.test.gosuslugi.ru',
'remoteCertificatePath' => EsiaProvider::RESOURCES.'esia.test.cer',
'defaultScopes' => [
// needed for authenticating
'openid',
Expand Down Expand Up @@ -142,6 +143,24 @@ public function testSignerIsRequired()
new EsiaProvider();
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Remote URL is not provided!
*/
public function testRemoteUrlIsRequired()
{
new EsiaProvider(['remoteUrl' => ''], ['signer' => $this->signer]);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Remote certificate is not provided!
*/
public function testRemoteCertificateIsRequired()
{
new EsiaProvider(['remoteCertificatePath' => ''], ['signer' => $this->signer]);
}

/**
* @depends testAccessTokenRequested
*/
Expand Down

0 comments on commit a4d02bb

Please sign in to comment.