1
|
|
|
<?php namespace Bogstag\OAuth2\Client\Provider; |
2
|
|
|
|
3
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
4
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
5
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
6
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Trakt |
11
|
|
|
* @package Bogstag\OAuth2\Client\Provider |
12
|
|
|
*/ |
13
|
|
|
class Trakt extends AbstractProvider |
14
|
|
|
{ |
15
|
|
|
use BearerAuthorizationTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $baseUrlApi = 'https://api.trakt.tv'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $baseUrl = 'https://trakt.tv'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $traktApiVersion = 2; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get authorization url to begin OAuth flow |
34
|
|
|
* As noted in api docs you should use the normal url not the api url. |
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
6 |
|
public function getBaseAuthorizationUrl() |
39
|
|
|
{ |
40
|
6 |
|
return $this->baseUrl.'/oauth/authorize'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritDoc |
45
|
|
|
*/ |
46
|
12 |
|
public function getBaseAccessTokenUrl(array $params) |
47
|
|
|
{ |
48
|
12 |
|
return $this->baseUrlApi.'/oauth/token'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritDoc |
53
|
|
|
*/ |
54
|
3 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
55
|
|
|
{ |
56
|
3 |
|
return $this->baseUrlApi.'/users/settings'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritDoc |
61
|
|
|
*/ |
62
|
9 |
|
public function getHeaders($token = null) |
63
|
|
|
{ |
64
|
9 |
|
$headers = []; |
65
|
9 |
|
if ($token) { |
66
|
|
|
$headers = [ |
67
|
3 |
|
'Content-Type' => 'application/json', |
68
|
3 |
|
'trakt-api-version' => $this->traktApiVersion, |
69
|
3 |
|
'trakt-api-key' => $this->clientId |
70
|
2 |
|
]; |
71
|
2 |
|
} |
72
|
|
|
|
73
|
9 |
|
return array_merge(parent::getHeaders($token), $headers); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
*/ |
79
|
6 |
|
protected function getDefaultScopes() |
80
|
|
|
{ |
81
|
6 |
|
return []; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritDoc |
86
|
|
|
*/ |
87
|
9 |
|
protected function checkResponse(ResponseInterface $response, $data) |
88
|
|
|
{ |
89
|
9 |
|
if (isset($data['error'])) { |
90
|
3 |
|
throw new IdentityProviderException( |
91
|
3 |
|
(isset($data['error']['message']) ? $data['error']['message'] : $response->getReasonPhrase()), |
92
|
3 |
|
$response->getStatusCode(), |
93
|
|
|
$data |
94
|
2 |
|
); |
95
|
|
|
} |
96
|
6 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritDoc |
100
|
|
|
*/ |
101
|
3 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
102
|
|
|
{ |
103
|
3 |
|
return new TraktResourceOwner($response); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|