1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SocialConnect project |
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace SocialConnect\OAuth2\Provider; |
8
|
|
|
|
9
|
|
|
use SocialConnect\Provider\AccessTokenInterface; |
10
|
|
|
use SocialConnect\Provider\Exception\InvalidResponse; |
11
|
|
|
use SocialConnect\Common\Entity\User; |
12
|
|
|
use SocialConnect\Common\Hydrator\ObjectMap; |
13
|
|
|
|
14
|
|
|
class GitHub extends \SocialConnect\OAuth2\AbstractProvider |
15
|
|
|
{ |
16
|
3 |
|
public function getBaseUri() |
17
|
|
|
{ |
18
|
3 |
|
return 'https://api.github.com/'; |
19
|
|
|
} |
20
|
|
|
|
21
|
2 |
|
public function getAuthorizeUri() |
22
|
|
|
{ |
23
|
2 |
|
return 'https://github.com/login/oauth/authorize'; |
24
|
|
|
} |
25
|
|
|
|
26
|
2 |
|
public function getRequestTokenUri() |
27
|
|
|
{ |
28
|
2 |
|
return 'https://github.com/login/oauth/access_token'; |
29
|
|
|
} |
30
|
|
|
|
31
|
3 |
|
public function getName() |
32
|
|
|
{ |
33
|
3 |
|
return 'github'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
2 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
40
|
|
|
{ |
41
|
2 |
|
$response = $this->httpClient->request( |
42
|
2 |
|
$this->getBaseUri() . 'user', |
43
|
|
|
[ |
44
|
2 |
|
'access_token' => $accessToken->getToken() |
45
|
2 |
|
] |
46
|
2 |
|
); |
47
|
|
|
|
48
|
2 |
|
if (!$response->isSuccess()) { |
49
|
1 |
|
throw new InvalidResponse( |
50
|
1 |
|
'API response with error code', |
51
|
|
|
$response |
52
|
1 |
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
$result = $response->json(); |
56
|
1 |
|
if (!$result) { |
57
|
1 |
|
throw new InvalidResponse( |
58
|
1 |
|
'API response is not a valid JSON object', |
59
|
|
|
$response |
60
|
1 |
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$hydrator = new ObjectMap( |
64
|
|
|
[ |
65
|
|
|
'id' => 'id', |
66
|
|
|
'login' => 'username', |
67
|
|
|
'email' => 'email', |
68
|
|
|
] |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
return $hydrator->hydrate(new User(), $result); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|