1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ChrisHemmings\OAuth2\Client\Provider; |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
6
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
7
|
|
|
use League\OAuth2\Client\Provider\ResourceOwnerInterface; |
8
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
9
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
class Deezer extends AbstractProvider |
13
|
|
|
{ |
14
|
|
|
use BearerAuthorizationTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Get authorization url to begin OAuth flow |
18
|
|
|
* |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
6 |
|
public function getBaseAuthorizationUrl() |
22
|
|
|
{ |
23
|
6 |
|
return 'https://connect.deezer.com/oauth/auth.php'; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get access token url to retrieve token |
28
|
|
|
* |
29
|
|
|
* @param array $params |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
12 |
|
public function getBaseAccessTokenUrl(array $params) |
34
|
|
|
{ |
35
|
12 |
|
return 'https://connect.deezer.com/oauth/access_token.php'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Parses the response according to its content-type header. |
40
|
|
|
* |
41
|
|
|
* @throws UnexpectedValueException |
42
|
|
|
* @param ResponseInterface $response |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
9 |
|
protected function parseResponse(ResponseInterface $response) |
46
|
|
|
{ |
47
|
9 |
|
$content = (string) $response->getBody(); |
48
|
9 |
|
$type = $this->getContentType($response); |
49
|
|
|
|
50
|
9 |
|
if (strpos($type, 'urlencoded') !== false || strpos($type, 'text/html') !== false) { |
51
|
3 |
|
parse_str($content, $parsed); |
52
|
3 |
|
return $parsed; |
53
|
|
|
} |
54
|
|
|
// Attempt to parse the string as JSON regardless of content type, |
55
|
|
|
// since some providers use non-standard content types. Only throw an |
56
|
|
|
// exception if the JSON could not be parsed when it was expected to. |
57
|
|
|
try { |
58
|
9 |
|
return $this->parseJson($content); |
59
|
|
|
} catch (\UnexpectedValueException $e) { |
60
|
|
|
if (strpos($type, 'json') !== false) { |
61
|
|
|
throw $e; |
62
|
|
|
} |
63
|
|
|
return $content; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get provider url to fetch user details |
69
|
|
|
* |
70
|
|
|
* @param AccessToken $token |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
3 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
75
|
|
|
{ |
76
|
3 |
|
return 'https://api.deezer.com/user/me?' . http_build_query(['access_token' => $token->getToken()]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the default scopes used by this provider. |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
6 |
|
protected function getDefaultScopes() |
85
|
|
|
{ |
86
|
6 |
|
return []; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Check a provider response for errors. |
91
|
|
|
* |
92
|
|
|
* @param ResponseInterface $response |
93
|
|
|
* @param array|string $data |
94
|
|
|
* |
95
|
|
|
* @throws IdentityProviderException |
96
|
|
|
*/ |
97
|
9 |
|
protected function checkResponse(ResponseInterface $response, $data) |
98
|
|
|
{ |
99
|
9 |
|
if ($response->getStatusCode() >= 400) { |
100
|
3 |
|
throw new IdentityProviderException( |
101
|
3 |
|
$data['error'] ?: $response->getReasonPhrase(), |
102
|
3 |
|
$response->getStatusCode(), |
103
|
|
|
$response |
|
|
|
|
104
|
2 |
|
); |
105
|
|
|
} |
106
|
6 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Generate a user object from a successful user details request. |
110
|
|
|
* |
111
|
|
|
* @param array $response |
112
|
|
|
* @param AccessToken $token |
113
|
|
|
* |
114
|
|
|
* @return League\OAuth2\Client\Provider\ResourceOwnerInterface |
115
|
|
|
*/ |
116
|
3 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
117
|
|
|
{ |
118
|
3 |
|
return new DeezerResourceOwner($response); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: