|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ofbeaton\OAuth2\Client\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Ofbeaton\OAuth2\Client\Provider\Exception\PhabricatorIdentityProviderException; |
|
6
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
|
7
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
|
8
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
11
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
12
|
|
|
|
|
13
|
|
|
class Phabricator extends AbstractProvider |
|
14
|
|
|
{ |
|
15
|
|
|
use BearerAuthorizationTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Phabricator API endpoint to retrieve logged in user information. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
const PATH_API_USER = '/api/user.whoami'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Phabricator OAuth server authorization endpoint. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
const PATH_AUTHORIZE = '/oauthserver/auth/'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* habricator OAuth server token request endpoint. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
const PATH_TOKEN = '/oauthserver/token/'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Domain |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $domain; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get domain. |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
3 |
|
public function getDomain() |
|
51
|
|
|
{ |
|
52
|
3 |
|
return $this->domain; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get authorization url to begin OAuth flow |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
12 |
|
public function getBaseAuthorizationUrl() |
|
61
|
|
|
{ |
|
62
|
12 |
|
$url = $this->domain.self::PATH_AUTHORIZE; |
|
63
|
12 |
|
return $url; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get access token url to retrieve token |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $params |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
18 |
|
public function getBaseAccessTokenUrl(array $params) |
|
74
|
|
|
{ |
|
75
|
18 |
|
$url = $this->domain.self::PATH_TOKEN; |
|
76
|
18 |
|
return $url; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get provider url to fetch user details |
|
81
|
|
|
* |
|
82
|
|
|
* @param AccessToken $token |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
6 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
|
87
|
|
|
{ |
|
88
|
6 |
|
$url = $this->domain.self::PATH_API_USER.'?'.$this->buildQueryString(array( |
|
89
|
6 |
|
'access_token' => $token->getToken(), |
|
90
|
4 |
|
)); |
|
91
|
6 |
|
return $url; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get the default scopes used by this provider. |
|
96
|
|
|
* |
|
97
|
|
|
* This should not be a complete list of all scopes, but the minimum |
|
98
|
|
|
* required for the provider user interface! |
|
99
|
|
|
* |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
6 |
|
protected function getDefaultScopes() |
|
103
|
|
|
{ |
|
104
|
6 |
|
return []; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Check a provider response for errors. |
|
109
|
|
|
* |
|
110
|
|
|
* @throws IdentityProviderException |
|
111
|
|
|
* @param ResponseInterface $response |
|
112
|
|
|
* @param string $data Parsed response data |
|
113
|
|
|
* @return void |
|
114
|
|
|
*/ |
|
115
|
15 |
|
protected function checkResponse(ResponseInterface $response, $data) |
|
116
|
|
|
{ |
|
117
|
15 |
|
if ($response->getStatusCode() >= 400) { |
|
118
|
3 |
|
throw PhabricatorIdentityProviderException::clientException($response, $data); |
|
119
|
12 |
|
} elseif (isset($data['error']) === true) { |
|
120
|
3 |
|
throw PhabricatorIdentityProviderException::oauthException($response, $data); |
|
121
|
|
|
} |
|
122
|
9 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Generate a user object from a successful user details request. |
|
126
|
|
|
* |
|
127
|
|
|
* @param array $response |
|
128
|
|
|
* @param AccessToken $token |
|
129
|
|
|
* @return Ofbeaton\OAuth2\Client\Provider\ResourceOwnerInterface |
|
130
|
|
|
*/ |
|
131
|
3 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
|
132
|
|
|
{ |
|
133
|
3 |
|
$user = new PhabricatorResourceOwner($response); |
|
134
|
3 |
|
$user->setDomain($this->domain); |
|
135
|
3 |
|
return $user; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|