Skip to content

Commit e23f08d

Browse files
Criação do CertKeyService.php.
Criação de recurso para leitura da chave no cache (por interface). Criação de recurso para gravação de chave no cache (por interface). Criação de ConfigurationDiscoveryService.php para descobrir a configuração do OpenID Connect.
1 parent 5ed5a93 commit e23f08d

20 files changed

+691
-10
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"type": "library",
55
"require": {
66
"zendframework/zend-mvc": "^3.1",
7-
"lcobucci/jwt": "^3.3"
7+
"lcobucci/jwt": "^3.3",
8+
"ext-curl": "*",
9+
"ext-json": "*"
810
},
911
"require-dev": {
1012
"zendframework/zend-test": "^3.2",

src/Auth/Authorizator.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22

33
namespace Zend\Mvc\OIDC\Auth;
44

5+
use Exception;
56
use Zend\Http\Request;
67
use Zend\Mvc\OIDC\Common\Configuration;
8+
use Zend\Mvc\OIDC\Common\Enum\ValidationTokenResultEnum;
9+
use Zend\Mvc\OIDC\Common\Exceptions\AudienceConfigurationException;
710
use Zend\Mvc\OIDC\Common\Exceptions\BasicAuthorizationException;
11+
use Zend\Mvc\OIDC\Common\Exceptions\CertificateKeyException;
12+
use Zend\Mvc\OIDC\Common\Exceptions\InvalidAuthorizationTokenException;
13+
use Zend\Mvc\OIDC\Common\Exceptions\JwkRecoveryException;
14+
use Zend\Mvc\OIDC\Common\Exceptions\OidcConfigurationDiscoveryException;
815
use Zend\Mvc\OIDC\Common\Exceptions\RealmConfigurationException;
916
use Zend\Mvc\OIDC\Common\Exceptions\ServiceUrlConfigurationException;
1017
use Zend\Mvc\OIDC\Common\Model\Token;
1118
use Zend\Mvc\OIDC\Common\Parse\ConfigurationParser;
19+
use Zend\Mvc\OIDC\OpenIDConnect\CertKeyService;
20+
use Zend\Mvc\OIDC\OpenIDConnect\ConfigurationDiscoveryService;
1221
use Zend\ServiceManager\ServiceLocatorInterface;
1322
use Zend\ServiceManager\ServiceManager;
1423

@@ -45,6 +54,16 @@ class Authorizator
4554
*/
4655
private $routesConfig;
4756

57+
/**
58+
* @var ConfigurationDiscoveryService
59+
*/
60+
private $configurationDiscoveryService;
61+
62+
/**
63+
* @var CertKeyService
64+
*/
65+
private $certKeyService;
66+
4867
/**
4968
* Authorizator constructor.
5069
*
@@ -53,6 +72,7 @@ class Authorizator
5372
*
5473
* @throws RealmConfigurationException
5574
* @throws ServiceUrlConfigurationException
75+
* @throws AudienceConfigurationException
5676
*/
5777
public function __construct(array $moduleConfig, ServiceLocatorInterface $serviceManager)
5878
{
@@ -62,6 +82,10 @@ public function __construct(array $moduleConfig, ServiceLocatorInterface $servic
6282

6383
$this->serviceManager = $serviceManager;
6484

85+
$this->configurationDiscoveryService = new ConfigurationDiscoveryService();
86+
87+
$this->certKeyService = new CertKeyService();
88+
6589
$this->configurationParser = new ConfigurationParser();
6690
$this->configuration = $this->configurationParser->parse($moduleConfig);
6791
}
@@ -71,21 +95,49 @@ public function __construct(array $moduleConfig, ServiceLocatorInterface $servic
7195
*
7296
* @return bool
7397
* @throws BasicAuthorizationException
98+
* @throws CertificateKeyException
99+
* @throws InvalidAuthorizationTokenException
100+
* @throws JwkRecoveryException
101+
* @throws OidcConfigurationDiscoveryException
102+
* @throws Exception
74103
*/
75104
public function authorize(Request $request): bool
76105
{
77106
$this->token = new Token($this->getAuthorizationToken($request));
78107

79108
$authorizeConfig = $this->getAuthorizeConfiguration($request);
80109

110+
$certKey = $this->certKeyService->resolveCertificate($this->configuration, $this->serviceManager);
111+
112+
if (!is_null($certKey)) {
113+
$this->configuration->setPublicKey($certKey);
114+
$result = $this->token->validate($this->configuration);
115+
116+
if ($result == ValidationTokenResultEnum::INVALID) {
117+
throw new InvalidAuthorizationTokenException('Invalid authorization token.');
118+
} else if ($result == ValidationTokenResultEnum::EXPIRED) {
119+
throw new InvalidAuthorizationTokenException('Expired authorization token.');
120+
}
121+
} else {
122+
throw new CertificateKeyException('Failed to retrieve the token certificate key.');
123+
}
124+
81125
return $this->isAuthorized($authorizeConfig);
82126
}
83127

128+
/**
129+
* @return array
130+
*/
84131
public function getTokenClaims(): array
85132
{
86133
return $this->token->getClaims();
87134
}
88135

136+
/**
137+
* @param array $authorizeConfig
138+
*
139+
* @return bool
140+
*/
89141
private function isAuthorized(array $authorizeConfig): bool
90142
{
91143
$result = false;
@@ -101,6 +153,11 @@ private function isAuthorized(array $authorizeConfig): bool
101153
return $result;
102154
}
103155

156+
/**
157+
* @param Request $request
158+
*
159+
* @return array
160+
*/
104161
private function getAuthorizeConfiguration(Request $request): array
105162
{
106163
$url = $request->getUriString();

src/Common/Enum/ConfigurationEnum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ interface ConfigurationEnum
1414
const REALM_ID = 'realmId';
1515
const CLIENT_ID = 'client_id';
1616
const PUBLIC_KEY = 'public_key';
17+
const AUDIENCE = 'audience';
1718
}

src/Common/Enum/ServiceEnum.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Zend\Mvc\OIDC\Common\Enum;
4+
5+
interface ServiceEnum
6+
{
7+
const CERT_KEY_CACHE_READER = 'Zend\Mvc\OIDC\Custom\CertKeyCacheReaderInterface';
8+
const CERT_KEY_CACHE_WRITER = 'Zend\Mvc\OIDC\Custom\CertKeyCacheWriterInterface';
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Zend\Mvc\OIDC\Common\Exceptions;
4+
5+
/**
6+
* Class AudienceConfigurationException
7+
*
8+
* @package Zend\Mvc\OIDC\Common\Exceptions
9+
*/
10+
class AudienceConfigurationException extends \Exception
11+
{
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Zend\Mvc\OIDC\Common\Exceptions;
4+
5+
/**
6+
* Class CertificateKeyException
7+
*
8+
* @package Zend\Mvc\OIDC\Common\Exceptions
9+
*/
10+
class CertificateKeyException extends \Exception
11+
{
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Zend\Mvc\OIDC\Common\Exceptions;
4+
5+
/**
6+
* Class InvalidAuthorizationTokenException
7+
*
8+
* @package Zend\Mvc\OIDC\Common\Exceptions
9+
*/
10+
class InvalidAuthorizationTokenException extends \Exception
11+
{
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Zend\Mvc\OIDC\Common\Exceptions;
4+
5+
/**
6+
* Class JwkRecoveryException
7+
*
8+
* @package Zend\Mvc\OIDC\Common\Exceptions
9+
*/
10+
class JwkRecoveryException extends \Exception
11+
{
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Zend\Mvc\OIDC\Common\Exceptions;
4+
5+
/**
6+
* Class OidcConfigurationDiscoveryException
7+
*
8+
* @package Zend\Mvc\OIDC\Common\Exceptions
9+
*/
10+
class OidcConfigurationDiscoveryException extends \Exception
11+
{
12+
13+
}

src/Common/Infra/HttpClient.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Zend\Mvc\OIDC\Common\Infra;
4+
5+
/**
6+
* Class HttpClient
7+
*
8+
* @package Zend\Mvc\OIDC\Common\Infra
9+
*/
10+
class HttpClient
11+
{
12+
13+
public function sendRequest(
14+
string $baseUrl,
15+
string $method = 'GET',
16+
string $path = '/',
17+
array $headers = array(),
18+
string $data = ''
19+
) {
20+
$method = strtoupper($method);
21+
$url = $baseUrl . $path;
22+
23+
// Initiate HTTP request
24+
$request = curl_init();
25+
26+
curl_setopt($request, CURLOPT_URL, $url);
27+
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
28+
29+
if ($method === 'POST') {
30+
curl_setopt($request, CURLOPT_POST, true);
31+
curl_setopt($request, CURLOPT_POSTFIELDS, $data);
32+
array_push($headers, 'Content-Length: ' . strlen($data));
33+
}
34+
35+
curl_setopt($request, CURLOPT_HTTPHEADER, $headers);
36+
$response = curl_exec($request);
37+
$response_code = curl_getinfo($request, CURLINFO_HTTP_CODE);
38+
curl_close($request);
39+
40+
return array(
41+
'code' => $response_code,
42+
'body' => $response
43+
);
44+
}
45+
}

0 commit comments

Comments
 (0)