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
|
|
|
use Respect\Validation\Validator as v; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Mastodon Authorization Code grant |
13
|
|
|
* |
14
|
|
|
* @author USAMI Kenta <[email protected]> |
15
|
|
|
* @copyright 2017 Baguette HQ |
16
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0 |
17
|
|
|
* @see https://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-4.1 |
18
|
|
|
*/ |
19
|
|
|
class CodeGrant extends Grant |
20
|
|
|
{ |
21
|
|
|
/** @var string */ |
22
|
|
|
private $code; |
23
|
|
|
/** @var string */ |
24
|
|
|
private $redirect_uri; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $code |
28
|
|
|
* @param string $redirect_uri |
29
|
|
|
*/ |
30
|
|
|
public function __construct($code, $redirect_uri) |
31
|
|
|
{ |
32
|
|
|
$this->code = $code; |
33
|
|
|
$this->redirect_uri = $redirect_uri; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Mastodon\Client $client |
38
|
|
|
* @param AuthFactory $auth |
39
|
|
|
* @param Scope $scope |
40
|
|
|
* @param string $callback_uri |
41
|
|
|
* @param string $state |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public static function getRedirectUrl(Mastodon\Client $client, Mastodon\Service\AuthFactory $auth, Scope $scope, $callback_uri, $state = null) |
45
|
|
|
{ |
46
|
|
|
$query = [ |
47
|
|
|
'client_id' => $auth->client_id, |
48
|
|
|
'response_type' => 'code', |
49
|
|
|
'redirect_uri' => $callback_uri, |
50
|
|
|
'scopes' => (string)$scope, |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
if ($state !== null) { |
54
|
|
|
v::stringType()->length(1, null)->assert($state); |
55
|
|
|
$query['state'] = $state; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return sprintf('%s://%s/oauth/authorize?%s', $client->getScheme(), $client->getHostname(), http_build_query($query)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Client $http |
63
|
|
|
* @param AuthFactory $factory |
64
|
|
|
* @param Scope $scope |
65
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function auth(Client $http, AuthFactory $factory, Scope $scope = null) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
return $http->request('POST', static::getPathToOAuthToken($factory->client), [ |
70
|
|
|
'form_params' => [ |
71
|
|
|
'grant_type' => 'authorization_code', |
72
|
|
|
'code' => $this->code, |
73
|
|
|
'redirect_uri' => $this->redirect_uri, |
74
|
|
|
] + static::getFormParamsWithSecret($factory), |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.