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
|
2 |
|
public function getBaseAccessTokenUrl(array $params) |
23
|
|
|
{ |
24
|
|
|
// Untappd requires inclusion of additional params for verification |
25
|
|
|
return 'https://untappd.com/oauth/authorize?' . |
26
|
2 |
|
http_build_query(array_replace($params, [ |
27
|
2 |
|
'client_id' => $this->clientId, |
28
|
2 |
|
'client_secret' => $this->clientSecret, |
29
|
2 |
|
'redirect_url' => $this->redirectUri, |
30
|
2 |
|
])); |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
34
|
|
|
{ |
35
|
|
|
return 'https://api.untappd.com/v4/user/info?' . |
36
|
2 |
|
http_build_query([ |
37
|
2 |
|
'access_token' => (string) $token, |
38
|
2 |
|
]); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
protected function getDefaultScopes() |
42
|
|
|
{ |
43
|
1 |
|
return []; |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
protected function getAuthorizationParameters(array $options) |
47
|
|
|
{ |
48
|
1 |
|
$params = parent::getAuthorizationParameters($options); |
49
|
|
|
|
50
|
|
|
// Untappd uses a non-standard redirect name |
51
|
1 |
|
$params['redirect_url'] = $params['redirect_uri']; |
52
|
1 |
|
unset($params['redirect_uri']); |
53
|
|
|
|
54
|
|
|
// Untappd does not support state passing |
55
|
1 |
|
$this->state = ''; |
56
|
1 |
|
unset($params['state']); |
57
|
|
|
|
58
|
1 |
|
return $params; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
protected function getAccessTokenMethod() |
62
|
|
|
{ |
63
|
1 |
|
return self::METHOD_GET; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
protected function prepareAccessTokenResponse(array $result) |
67
|
|
|
{ |
68
|
|
|
// Untappd wraps the response to include metadata |
69
|
1 |
|
return parent::prepareAccessTokenResponse($result['response']); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
protected function checkResponse(ResponseInterface $response, $data) |
73
|
|
|
{ |
74
|
2 |
|
if (!empty($data['meta']['error_type'])) { |
75
|
1 |
|
$code = 0; |
76
|
1 |
|
if (!empty($data['meta']['http_code'])) { |
77
|
1 |
|
$code = $data['meta']['http_code']; |
78
|
1 |
|
} |
79
|
1 |
|
$error = $data['meta']['error_detail']; |
80
|
|
|
|
81
|
1 |
|
throw new IdentityProviderException($error, $code, $data); |
82
|
|
|
} |
83
|
1 |
|
} |
84
|
|
|
|
85
|
1 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
86
|
|
|
{ |
87
|
1 |
|
return new UntappdUser($response['response']['user']); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|