|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* SocialConnect project |
|
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace SocialConnect\Auth\Provider; |
|
8
|
|
|
|
|
9
|
|
|
use SocialConnect\Auth\Provider\Exception\InvalidAccessToken; |
|
10
|
|
|
use SocialConnect\Auth\Provider\Exception\InvalidResponse; |
|
11
|
|
|
use SocialConnect\OAuth2\AccessToken; |
|
12
|
|
|
use SocialConnect\Common\Entity\User; |
|
13
|
|
|
use SocialConnect\Common\Http\Client\Client; |
|
14
|
|
|
use SocialConnect\Common\Hydrator\ObjectMap; |
|
15
|
|
|
|
|
16
|
|
|
class DigitalOcean extends \SocialConnect\OAuth2\AbstractProvider |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritdoc} |
|
20
|
|
|
*/ |
|
21
|
|
|
public function getBaseUri() |
|
22
|
|
|
{ |
|
23
|
|
|
return 'https://api.digitalocean.com/v2/'; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getAuthorizeUri() |
|
30
|
|
|
{ |
|
31
|
|
|
return 'https://cloud.digitalocean.com/v1/oauth/authorize'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getRequestTokenUri() |
|
38
|
|
|
{ |
|
39
|
|
|
return 'https://cloud.digitalocean.com/v1/oauth/token'; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getName() |
|
46
|
|
|
{ |
|
47
|
|
|
return 'digital-ocean'; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function parseToken($body) |
|
54
|
|
|
{ |
|
55
|
|
|
if (empty($body)) { |
|
56
|
|
|
throw new InvalidAccessToken('Provider response with empty body'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$result = json_decode($body); |
|
60
|
|
|
if ($result) { |
|
61
|
|
|
if (isset($result->access_token)) { |
|
62
|
|
|
return new AccessToken($result->access_token); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
throw new InvalidAccessToken('Provider API returned without access_token field inside JSON'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
throw new InvalidAccessToken('Provider response with not valid JSON'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getIdentity(AccessToken $accessToken) |
|
75
|
|
|
{ |
|
76
|
|
|
$response = $this->service->getHttpClient()->request( |
|
77
|
|
|
$this->getBaseUri() . 'account', |
|
78
|
|
|
[], |
|
79
|
|
|
Client::GET, |
|
80
|
|
|
[ |
|
81
|
|
|
'Authorization' => 'Bearer ' . $accessToken->getToken() |
|
82
|
|
|
] |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
if (!$response->isSuccess()) { |
|
86
|
|
|
throw new InvalidResponse( |
|
87
|
|
|
'API response with error code', |
|
88
|
|
|
$response |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$result = $response->json(); |
|
93
|
|
|
if (!$result) { |
|
94
|
|
|
throw new InvalidResponse( |
|
95
|
|
|
'API response is not a valid JSON object', |
|
96
|
|
|
$response->getBody() |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$hydrator = new ObjectMap(array( |
|
101
|
|
|
'uuid' => 'id', |
|
102
|
|
|
)); |
|
103
|
|
|
|
|
104
|
|
|
return $hydrator->hydrate(new User(), $result->account); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|