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\OAuth2\Exception\InvalidState; |
13
|
|
|
use SocialConnect\OAuth2\Exception\Unauthorized; |
14
|
|
|
use SocialConnect\OAuth2\Exception\UnknownAuthorization; |
15
|
|
|
use SocialConnect\OAuth2\Exception\UnknownState; |
16
|
|
|
use SocialConnect\Provider\AccessTokenInterface; |
17
|
|
|
use SocialConnect\Provider\Exception\InvalidResponse; |
18
|
|
|
use SocialConnect\OAuth2\AccessToken; |
19
|
|
|
use SocialConnect\Common\Entity\User; |
20
|
|
|
|
21
|
|
|
class SmashCast extends \SocialConnect\OAuth2\AbstractProvider |
22
|
|
|
{ |
23
|
|
|
const NAME = 'smashcast'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
4 |
|
public function getBaseUri() |
29
|
|
|
{ |
30
|
4 |
|
return 'https://api.smashcast.tv/'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
1 |
|
public function getAuthorizeUri() |
37
|
|
|
{ |
38
|
1 |
|
return 'https://api.smashcast.tv/oauth/login'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
2 |
|
public function getRequestTokenUri() |
45
|
|
|
{ |
46
|
2 |
|
return 'https://api.smashcast.tv/oauth/exchange'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
1 |
|
public function getName() |
53
|
|
|
{ |
54
|
1 |
|
return self::NAME; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function getAuthUrlParameters(): array |
61
|
|
|
{ |
62
|
|
|
$parameters = $this->getArrayOption('auth.parameters', []); |
63
|
|
|
|
64
|
|
|
// Because SmashCast developers dont know about OAuth spec... |
65
|
|
|
$parameters['app_token'] = $this->consumer->getKey(); |
66
|
|
|
$parameters['redirect_uri'] = $this->getRedirectUrl(); |
67
|
|
|
$parameters['response_type'] = 'code'; |
68
|
|
|
|
69
|
|
|
return $parameters; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
1 |
|
protected function makeAccessTokenRequest(string $code): RequestInterface |
76
|
|
|
{ |
77
|
|
|
$parameters = [ |
78
|
|
|
'request_token' => $code, |
79
|
1 |
|
'app_token' => $this->consumer->getKey(), |
80
|
1 |
|
'hash' => base64_encode($this->consumer->getKey() . $this->consumer->getSecret()), |
81
|
|
|
]; |
82
|
|
|
|
83
|
1 |
|
return $this->httpStack->createRequest($this->requestHttpMethod, $this->getRequestTokenUri()) |
84
|
1 |
|
->withHeader('Content-Type', 'application/x-www-form-urlencoded') |
85
|
1 |
|
->withBody($this->httpStack->createStream(http_build_query($parameters))) |
86
|
|
|
; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
1 |
|
public function getAccessTokenByRequestParameters(array $parameters) |
93
|
|
|
{ |
94
|
1 |
|
if (isset($parameters['error']) && $parameters['error'] === 'access_denied') { |
95
|
1 |
|
throw new Unauthorized(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!$this->getBoolOption('stateless', false)) { |
99
|
|
|
$state = $this->session->get('oauth2_state'); |
100
|
|
|
if (!$state) { |
101
|
|
|
throw new UnknownAuthorization(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (!isset($parameters['state'])) { |
105
|
|
|
throw new UnknownState(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($state !== $parameters['state']) { |
109
|
|
|
throw new InvalidState(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (isset($parameters['authToken'])) { |
114
|
|
|
return new AccessToken(['access_token' => $parameters['authToken']]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->getAccessToken($parameters['request_token']); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* This method it needed, because I cannot fix auth/login with accessToken |
122
|
|
|
* BTW: Yes, I known that it's unneeded round trip to the server |
123
|
|
|
* |
124
|
|
|
* @param AccessTokenInterface $accessToken |
125
|
|
|
* @return string |
126
|
|
|
* @throws InvalidResponse |
127
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
128
|
|
|
*/ |
129
|
3 |
|
protected function getUserNameByToken(AccessTokenInterface $accessToken): string |
130
|
|
|
{ |
131
|
3 |
|
$response = $this->request('GET', 'userfromtoken/' . $accessToken->getToken(), [], $accessToken); |
132
|
|
|
|
133
|
1 |
|
return $response['user_name']; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritDoc} |
138
|
|
|
*/ |
139
|
3 |
|
public function prepareRequest(string $method, string $uri, array &$headers, array &$query, ?AccessTokenInterface $accessToken = null): void |
140
|
|
|
{ |
141
|
3 |
|
if ($accessToken) { |
142
|
3 |
|
$query['authToken'] = $accessToken->getToken(); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
3 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
150
|
|
|
{ |
151
|
|
|
// @todo Find a problem with this code |
152
|
|
|
/*$response = $this->httpClient->request( |
153
|
|
|
$this->getBaseUri() . 'auth/login', |
154
|
|
|
[ |
155
|
|
|
'app' => 'desktop', // @any app name, not working, I was using JSON and not working.. |
156
|
|
|
'authToken' => $accessToken->getToken() |
157
|
|
|
], |
158
|
|
|
'POST', |
159
|
|
|
[ |
160
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded' |
161
|
|
|
] |
162
|
|
|
);*/ |
163
|
|
|
|
164
|
3 |
|
$username = $this->getUserNameByToken($accessToken); |
165
|
1 |
|
$response = $this->request('GET', 'user/' . $username, [], $accessToken); |
166
|
|
|
|
167
|
1 |
|
$hydrator = new ArrayHydrator([ |
168
|
|
|
'user_id' => 'id', |
169
|
|
|
'user_name' => 'username', |
170
|
|
|
'user_email' => 'email', |
171
|
|
|
]); |
172
|
|
|
|
173
|
1 |
|
return $hydrator->hydrate(new User(), $response); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|