Skip to content

Commit

Permalink
+ add OpenId->buildLogoutUrl() method (#25)
Browse files Browse the repository at this point in the history
* + add OpenId->buildLogoutUrl() method

* * add `-engine gost` to command line to make gost key/certificates work without openssl.cnf changes

* * camelCase
- fix description

* Revert "* add `-engine gost` to command line to make gost key/certificates work without openssl.cnf changes"

This reverts commit ac9352d.

* + add config test

* - typo after previous commit with camel case

* + add 2 more tests
  • Loading branch information
paulish authored and fr05t1k committed Dec 17, 2019
1 parent 85f7313 commit c07c1e4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Esia/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Config
private $tokenUrlPath = 'aas/oauth2/te';
private $codeUrlPath = 'aas/oauth2/ac';
private $personUrlPath = 'rs/prns';
private $logoutUrlPath = 'idp/ext/Logout';
private $privateKeyPassword = '';

/**
Expand Down Expand Up @@ -71,6 +72,7 @@ public function __construct(array $config = [])
$this->tokenUrlPath = $config['tokenUrlPath'] ?? $this->tokenUrlPath;
$this->codeUrlPath = $config['codeUrlPath'] ?? $this->codeUrlPath;
$this->personUrlPath = $config['personUrlPath'] ?? $this->personUrlPath;
$this->logoutUrlPath = $config['logoutUrlPath'] ?? $this->logoutUrlPath;
$this->privateKeyPassword = $config['privateKeyPassword'] ?? $this->privateKeyPassword;
$this->oid = $config['oid'] ?? $this->oid;
$this->scope = $config['scope'] ?? $this->scope;
Expand Down Expand Up @@ -186,4 +188,12 @@ public function getPersonUrl(): string
}
return $this->portalUrl . $this->personUrlPath . '/' . $this->oid;
}

/**
* Return an url for logout
*/
public function getLogoutUrl(): string
{
return $this->portalUrl . $this->logoutUrlPath;
}
}
21 changes: 21 additions & 0 deletions src/Esia/OpenId.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,27 @@ public function buildUrl()
return sprintf($url, $request);
}

/**
* Return an url for logout
* @param string $redirectUrl
* @return string
*/
public function buildLogoutUrl(string $redirectUrl = null): string
{
$url = $this->config->getLogoutUrl() . '?%s';
$params = [
'client_id' => $this->config->getClientId(),
];

if ($redirectUrl) {
$params['redirect_url'] = $redirectUrl;
}

$request = http_build_query($params);

return sprintf($url, $request);
}

/**
* Method collect a token with given code
*
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function dataProviderForConstructor(): array
'tokenUrlPath' => 'test',
'codeUrlPath' => 'test',
'personUrlPath' => 'test',
'logoutUrlPath' => 'test',
'privateKeyPassword' => 'test',
'oid' => 'test',
'responseType' => 'test',
Expand Down Expand Up @@ -205,4 +206,21 @@ public function testGetPersonUrlWithoutOid(): void
$this->expectException(InvalidConfigurationException::class);
$this->assertSame('https://google.com/test/test', $config->getPersonUrl());
}
/**
* @throws InvalidConfigurationException
*/
public function testGetLogoutUrl(): void
{
$config = new Config([
'clientId' => 'test',
'redirectUrl' => 'http://google.com',
'privateKeyPath' => '/tmp',
'certPath' => '/tmp',
'portalUrl' => 'https://google.com/',
'logoutUrlPath' => 'test',
'scope' => ['test', 'test2', 'test3'],
]);

$this->assertSame('https://google.com/test', $config->getLogoutUrl());
}
}
25 changes: 25 additions & 0 deletions tests/unit/OpenIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,31 @@ public function testGetDocInfo(): void
$this->assertSame([['phone' => '555 555 555'], ['email' => 'test@gmail.com']], $info);
}

/**
* @throws \Esia\Exceptions\InvalidConfigurationException
*/
public function testBuildLogoutUrl(): void
{
$config = $this->openId->getConfig();

$url = $config->getLogoutUrl() . '?client_id=' . $config->getClientId();
$logoutUrl = $this->openId->buildLogoutUrl();
$this->assertSame($url, $logoutUrl);
}

/**
* @throws \Esia\Exceptions\InvalidConfigurationException
*/
public function testBuildLogoutUrlWithRedirect(): void
{
$config = $this->openId->getConfig();

$redirectUrl = 'test.example.com';
$url = $config->getLogoutUrl() . '?client_id=' . $config->getClientId() . '&redirect_url=' . $redirectUrl;
$logoutUrl = $this->openId->buildLogoutUrl($redirectUrl);
$this->assertSame($url, $logoutUrl);
}

/**
* Client with prepared responses
*
Expand Down

0 comments on commit c07c1e4

Please sign in to comment.