1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Baguette\Mastodon\Grant; |
4
|
|
|
|
5
|
|
|
use Baguette\Mastodon; |
6
|
|
|
use Baguette\Mastodon\Service\AuthFactory; |
7
|
|
|
use Baguette\Mastodon\Service\Scope; |
8
|
|
|
use GuzzleHttp\ClientInterface as Client; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Mastodon Authorization Code grant |
12
|
|
|
* |
13
|
|
|
* @author USAMI Kenta <[email protected]> |
14
|
|
|
* @copyright 2017 Baguette HQ |
15
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0 |
16
|
|
|
* @see https://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-4.1 |
17
|
|
|
*/ |
18
|
|
|
class CodeGrant extends Grant |
19
|
|
|
{ |
20
|
|
|
/** @var string */ |
21
|
|
|
private $code; |
22
|
|
|
/** @var string */ |
23
|
|
|
private $redirect_uri; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $code |
27
|
|
|
* @param string $redirect_uri |
28
|
|
|
*/ |
29
|
|
|
public function __construct($code, $redirect_uri) |
30
|
|
|
{ |
31
|
|
|
$this->code = $code; |
32
|
|
|
$this->redirect_uri = $redirect_uri; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Mastodon\Client $client |
37
|
|
|
* @param AuthFactory $auth |
38
|
|
|
* @param Scope $scope |
39
|
|
|
* @param string $callback_uri |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public static function getRedirectUrl(Mastodon\Client $client, Mastodon\Service\AuthFactory $auth, Scope $scope, $callback_uri) |
43
|
|
|
{ |
44
|
|
|
return sprintf('%s://%s/oauth/authorize?%s', $client->getScheme(), $client->getHostname(), http_build_query([ |
45
|
|
|
'client_id' => $auth->client_id, |
46
|
|
|
'response_type' => 'code', |
47
|
|
|
'redirect_uri' => $callback_uri, |
48
|
|
|
'scopes' => (string)$scope, |
49
|
|
|
])); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Client $http |
54
|
|
|
* @param AuthFactory $factory |
55
|
|
|
* @param Scope $scope |
56
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
57
|
|
|
*/ |
58
|
|
|
public function auth(Client $http, AuthFactory $factory, Scope $scope) |
59
|
|
|
{ |
60
|
|
|
return $http->request('POST', static::getPathToOAuthToken($factory->client), [ |
61
|
|
|
'form_params' => [ |
62
|
|
|
'grant_type' => 'authorization_code', |
63
|
|
|
'code' => $this->code, |
64
|
|
|
'scope' => (string)$scope, |
65
|
|
|
] + static::getFormParams($factory), |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|