1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SocialConnect project |
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace SocialConnect\OAuth2\Provider; |
9
|
|
|
|
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
use SocialConnect\Common\ArrayHydrator; |
12
|
|
|
use SocialConnect\Provider\AccessTokenInterface; |
13
|
|
|
use SocialConnect\Common\Entity\User; |
14
|
|
|
|
15
|
|
|
class Reddit extends \SocialConnect\OAuth2\AbstractProvider |
16
|
|
|
{ |
17
|
|
|
const NAME = 'reddit'; |
18
|
|
|
|
19
|
4 |
|
public function getBaseUri() |
20
|
|
|
{ |
21
|
4 |
|
return 'https://oauth.reddit.com/api/v1/'; |
22
|
|
|
} |
23
|
|
|
|
24
|
2 |
|
public function getAuthorizeUri() |
25
|
|
|
{ |
26
|
2 |
|
return 'https://ssl.reddit.com/api/v1/authorize'; |
27
|
|
|
} |
28
|
|
|
|
29
|
2 |
|
public function getRequestTokenUri() |
30
|
|
|
{ |
31
|
2 |
|
return 'https://ssl.reddit.com/api/v1/access_token'; |
32
|
|
|
} |
33
|
|
|
|
34
|
3 |
|
public function getName() |
35
|
|
|
{ |
36
|
3 |
|
return self::NAME; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
*/ |
42
|
1 |
|
protected function makeAccessTokenRequest(string $code): RequestInterface |
43
|
|
|
{ |
44
|
|
|
$parameters = [ |
45
|
1 |
|
'code' => $code, |
46
|
1 |
|
'grant_type' => 'authorization_code', |
47
|
1 |
|
'redirect_uri' => $this->getRedirectUrl() |
48
|
|
|
]; |
49
|
|
|
|
50
|
1 |
|
return $this->httpStack->createRequest($this->requestHttpMethod, $this->getRequestTokenUri()) |
51
|
1 |
|
->withHeader('Content-Type', 'application/x-www-form-urlencoded') |
52
|
1 |
|
->withHeader('Authorization', 'Basic ' . base64_encode($this->consumer->getKey() . ':' . $this->consumer->getSecret())) |
53
|
1 |
|
->withBody($this->httpStack->createStream(http_build_query($parameters))) |
54
|
|
|
; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
3 |
|
public function prepareRequest(string $method, string $uri, array &$headers, array &$query, AccessTokenInterface $accessToken = null): void |
61
|
|
|
{ |
62
|
3 |
|
if ($accessToken) { |
63
|
3 |
|
$headers['Authorization'] = "Bearer {$accessToken->getToken()}"; |
64
|
|
|
} |
65
|
3 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
3 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
71
|
|
|
{ |
72
|
3 |
|
$response = $this->request('GET', 'me.json', [], $accessToken); |
73
|
|
|
|
74
|
1 |
|
$hydrator = new ArrayHydrator([]); |
75
|
|
|
|
76
|
1 |
|
return $hydrator->hydrate(new User(), $response); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|