1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\OAuth2\Service; |
4
|
|
|
|
5
|
|
|
use OAuth\Common\Consumer\CredentialsInterface; |
6
|
|
|
use OAuth\Common\Http\Client\ClientInterface; |
7
|
|
|
use OAuth\Common\Http\Exception\TokenResponseException; |
8
|
|
|
use OAuth\Common\Http\Uri\Uri; |
9
|
|
|
use OAuth\Common\Http\Uri\UriInterface; |
10
|
|
|
use OAuth\Common\Storage\TokenStorageInterface; |
11
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Box service. |
15
|
|
|
* |
16
|
|
|
* @author Antoine Corcy <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @see https://developers.box.com/oauth/ |
19
|
|
|
*/ |
20
|
|
|
class Box extends AbstractService |
21
|
|
|
{ |
22
|
|
|
public function __construct( |
23
|
|
|
CredentialsInterface $credentials, |
24
|
|
|
ClientInterface $httpClient, |
25
|
|
|
TokenStorageInterface $storage, |
26
|
|
|
$scopes = [], |
27
|
|
|
?UriInterface $baseApiUri = null |
28
|
|
|
) { |
29
|
|
|
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true); |
30
|
|
|
|
31
|
|
|
if (null === $baseApiUri) { |
32
|
|
|
$this->baseApiUri = new Uri('https://api.box.com/2.0/'); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function getAuthorizationEndpoint() |
40
|
|
|
{ |
41
|
|
|
return new Uri('https://www.box.com/api/oauth2/authorize'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function getAccessTokenEndpoint() |
48
|
|
|
{ |
49
|
|
|
return new Uri('https://www.box.com/api/oauth2/token'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
protected function getAuthorizationMethod() |
56
|
|
|
{ |
57
|
|
|
return static::AUTHORIZATION_METHOD_HEADER_BEARER; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
protected function parseAccessTokenResponse($responseBody) |
64
|
|
|
{ |
65
|
|
|
$data = json_decode($responseBody, true); |
66
|
|
|
|
67
|
|
|
if (null === $data || !is_array($data)) { |
68
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
69
|
|
|
} elseif (isset($data['error'])) { |
70
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$token = new StdOAuth2Token(); |
74
|
|
|
$token->setAccessToken($data['access_token']); |
75
|
|
|
$token->setLifeTime($data['expires_in']); |
76
|
|
|
|
77
|
|
|
if (isset($data['refresh_token'])) { |
78
|
|
|
$token->setRefreshToken($data['refresh_token']); |
79
|
|
|
unset($data['refresh_token']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
unset($data['access_token'], $data['expires_in']); |
83
|
|
|
|
84
|
|
|
$token->setExtraParams($data); |
85
|
|
|
|
86
|
|
|
return $token; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|