1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shadowhand\OAuth2\Client\Provider; |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
6
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
7
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
8
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
class Untappd extends AbstractProvider |
12
|
|
|
{ |
13
|
|
|
use BearerAuthorizationTrait; |
14
|
|
|
|
15
|
|
|
const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'uid'; |
16
|
|
|
|
17
|
1 |
|
public function getBaseAuthorizationUrl() |
18
|
|
|
{ |
19
|
1 |
|
return 'https://untappd.com/oauth/authenticate'; |
20
|
|
|
} |
21
|
|
|
|
22
|
1 |
|
public function getBaseAccessTokenUrl(array $params) |
23
|
|
|
{ |
24
|
1 |
|
return 'https://untappd.com/oauth/authorize'; |
25
|
|
|
} |
26
|
|
|
|
27
|
2 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
28
|
|
|
{ |
29
|
2 |
|
return $this->appendQuery( |
30
|
2 |
|
'https://api.untappd.com/v4/user/info', |
31
|
2 |
|
http_build_query([ |
32
|
2 |
|
'access_token' => (string) $token, |
33
|
2 |
|
]) |
34
|
2 |
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
protected function getDefaultScopes() |
38
|
|
|
{ |
39
|
1 |
|
return []; |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
protected function getAuthorizationParameters(array $options) |
43
|
|
|
{ |
44
|
1 |
|
$params = parent::getAuthorizationParameters($options); |
45
|
|
|
|
46
|
|
|
// Untappd uses a non-standard redirect name |
47
|
1 |
|
$params['redirect_url'] = $params['redirect_uri']; |
48
|
1 |
|
unset($params['redirect_uri']); |
49
|
|
|
|
50
|
|
|
// Untappd does not support state passing |
51
|
1 |
|
$this->state = ''; |
52
|
1 |
|
unset($params['state']); |
53
|
|
|
|
54
|
1 |
|
return $params; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function getAccessTokenMethod() |
58
|
|
|
{ |
59
|
|
|
return self::METHOD_GET; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function getAccessTokenUrl(array $params) |
63
|
|
|
{ |
64
|
|
|
// Untappd requires inclusion of additional params for verification |
65
|
|
|
$params = array_replace($params, [ |
66
|
|
|
'client_id' => $this->clientId, |
67
|
|
|
'client_secret' => $this->clientSecret, |
68
|
|
|
'redirect_url' => $this->redirectUri, |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
return parent::getAccessTokenUrl($params); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function prepareAccessTokenResponse(array $result) |
75
|
|
|
{ |
76
|
|
|
// Untappd wraps the response to include metadata |
77
|
|
|
return parent::prepareAccessTokenResponse($result['response']); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
protected function checkResponse(ResponseInterface $response, $data) |
81
|
|
|
{ |
82
|
1 |
|
if (!empty($data['meta']['error_type'])) { |
83
|
1 |
|
$code = 0; |
84
|
1 |
|
if (!empty($data['meta']['http_code'])) { |
85
|
|
|
$code = $data['meta']['http_code']; |
86
|
|
|
} |
87
|
1 |
|
$error = $data['meta']['error_detail']; |
88
|
|
|
|
89
|
1 |
|
throw new IdentityProviderException($error, $code, $data); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
94
|
|
|
{ |
95
|
1 |
|
return new UntappdUser($response['response']['user']); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|