/
DebugEndpointResourceTypeHandler.php
95 lines (84 loc) · 3.57 KB
/
DebugEndpointResourceTypeHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* This file is part of the authbucket/oauth2-php package.
*
* (c) Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace AuthBucket\OAuth2\ResourceType;
use AuthBucket\OAuth2\Exception\InvalidRequestException;
use AuthBucket\OAuth2\Exception\ServerErrorException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Token response type implementation.
*
* @author Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
*/
class DebugEndpointResourceTypeHandler extends AbstractResourceTypeHandler
{
public function handle(
$accessToken,
array $options = array()
)
{
$options = array_merge(array(
'debug_endpoint' => '',
'cache' => true,
), $options);
// Both options are required.
if (!$options['debug_endpoint']) {
throw new ServerErrorException(array(
'error_description' => 'The authorization server encountered an unexpected condition that prevented it from fulfilling the request.',
));
}
$accessTokenManager = $this->modelManagerFactory->getModelManager('access_token');
// Get cached access_token and return if exists.
if ($options['cache']) {
$stored = $accessTokenManager->readModelOneBy(array(
'accessToken' => $accessToken,
));
if ($stored !== null && $stored->getExpires() > new \DateTime()) {
return $stored;
}
}
// Fetch meta data of supplied access token by query debug endpoint.
if (strpos($options['debug_endpoint'], '/') === 0) {
// For relative URL, use Symfony test client to simulates and
// HTTP client like a browser and makes requests.
$client = new \Symfony\Component\HttpKernel\Client($this->httpKernel);
$crawler = $client->request('GET', $options['debug_endpoint'], array(), array(), array(
'HTTP_Authorization' => implode(' ', array('Bearer', $accessToken)),
));
$content = $client->getResponse()->getContent();
} else {
// For absolute URL, use Guzzle client to create request.
$client = new \Guzzle\Http\Client();
$crawler = $client->get($options['debug_endpoint'], array(), array(
'headers' => array('Authorization' => implode(' ', array('Bearer', $accessToken))),
'exceptions' => false,
));
$content = $crawler->send()->getBody();
}
$response = json_decode($content, true);
// Throw exception if error return.
if (isset($response['error'])) {
throw new InvalidRequestException(array(
'error_description' => 'The request includes an invalid parameter value.',
));
}
// Create a new access token with fetched meta data.
$class = $accessTokenManager->getClassName();
$accessTokenCached = new $class();
$accessTokenCached->setAccessToken($response['access_token'])
->setTokenType($response['token_type'])
->setClientId($response['client_id'])
->setUsername($response['username'])
->setExpires(new \DateTime('@'.$response['expires']))
->setScope($response['scope']);
$accessTokenCached = $accessTokenManager->createModel($accessTokenCached);
return $accessTokenCached;
}
}