1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lrf141\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 Mastodon extends AbstractProvider |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
use BearerAuthorizationTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Mastodon Instance URL |
18
|
|
|
* ex) https://mstdn.jp |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $instance; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $scope; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Mastodon constructor. |
32
|
|
|
* @param array $options |
33
|
|
|
* @param array $collaborators |
34
|
|
|
*/ |
35
|
18 |
|
public function __construct(array $options = [], array $collaborators = []) |
36
|
|
|
{ |
37
|
18 |
|
parent::__construct($options, $collaborators); |
38
|
|
|
|
39
|
18 |
|
if (isset($options['instance'])) { |
40
|
18 |
|
$this->instance = $options['instance']; |
41
|
|
|
} |
42
|
|
|
|
43
|
18 |
|
if (isset($options['scope'])) { |
44
|
|
|
$this->scope = $options['scope']; |
45
|
|
|
} |
46
|
18 |
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get authorization url to begin OAuth flow |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
6 |
|
public function getBaseAuthorizationUrl() |
54
|
|
|
{ |
55
|
6 |
|
return $this->instance.'/oauth/authorize'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get access token url to retrieve token |
60
|
|
|
* @param array $params |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
8 |
|
public function getBaseAccessTokenUrl(array $params) |
64
|
|
|
{ |
65
|
8 |
|
return $this->instance.'/oauth/token'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get provider url to fetch user details |
71
|
|
|
* @param AccessToken $token |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
2 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
75
|
|
|
{ |
76
|
2 |
|
return $this->instance . '/api/v1/accounts/verify_credentials'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the default scopes used by this provider |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
2 |
|
protected function getDefaultScopes() |
84
|
|
|
{ |
85
|
2 |
|
return isset($this->scope) ? $this->scope : ['scope' => 'read']; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check a provider response for errors |
90
|
|
|
* @throws IdentityProviderException |
91
|
|
|
* @param ResponseInterface $response |
92
|
|
|
* @param array|string $data |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
6 |
|
protected function checkResponse(ResponseInterface $response, $data) |
96
|
|
|
{ |
97
|
6 |
|
if (isset($data['error'])) { |
98
|
2 |
|
throw new IdentityProviderException( |
99
|
2 |
|
$data['error'] ?: $response->getReasonPhrase(), |
100
|
2 |
|
$response->getStatusCode(), |
101
|
2 |
|
$response |
|
|
|
|
102
|
|
|
); |
103
|
|
|
} |
104
|
4 |
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Generate a user object from a successful user details request. |
109
|
|
|
* |
110
|
|
|
* @param array $response |
111
|
|
|
* @param AccessToken $token |
112
|
|
|
* @return \League\OAuth2\Client\Provider\ResourceOwnerInterface|MastodonResourceOwner |
113
|
|
|
*/ |
114
|
2 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
115
|
|
|
{ |
116
|
2 |
|
return new MastodonResourceOwner($response); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Requests resource owner details. |
122
|
|
|
* |
123
|
|
|
* @param AccessToken $token |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
2 |
|
protected function fetchResourceOwnerDetails(AccessToken $token) |
127
|
|
|
{ |
128
|
2 |
|
return parent::fetchResourceOwnerDetails($token); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Builds the authorization URL. |
134
|
|
|
* |
135
|
|
|
* @param array $options |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
6 |
|
public function getAuthorizationUrl(array $options = []) |
139
|
|
|
{ |
140
|
6 |
|
return parent::getAuthorizationUrl($options); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* get mastodon instance url |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
4 |
|
public function getInstanceUrl() : string |
149
|
|
|
{ |
150
|
4 |
|
return $this->instance; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|