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
|
|
|
* @param array $options |
47
|
|
|
* @param array $collaborators |
48
|
|
|
* |
49
|
|
|
* @throws \InvalidArgumentException |
50
|
|
|
*/ |
51
|
33 |
|
public function __construct($options = [], array $collaborators = []) |
52
|
|
|
{ |
53
|
33 |
|
parent::__construct($options, $collaborators); |
54
|
33 |
|
if (empty($options['domain'])) { |
55
|
3 |
|
$message = 'The "domain" option not set. Please set a domain.'; |
56
|
3 |
|
throw new \InvalidArgumentException($message); |
57
|
|
|
} |
58
|
33 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get domain. |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
3 |
|
public function getDomain() |
66
|
|
|
{ |
67
|
3 |
|
return $this->domain; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get authorization url to begin OAuth flow |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
12 |
|
public function getBaseAuthorizationUrl() |
76
|
|
|
{ |
77
|
12 |
|
$url = $this->domain.self::PATH_AUTHORIZE; |
78
|
12 |
|
return $url; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get access token url to retrieve token |
83
|
|
|
* |
84
|
|
|
* @param array $params |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
18 |
|
public function getBaseAccessTokenUrl(array $params) |
89
|
|
|
{ |
90
|
18 |
|
$url = $this->domain.self::PATH_TOKEN; |
91
|
18 |
|
return $url; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get provider url to fetch user details |
96
|
|
|
* |
97
|
|
|
* @param AccessToken $token |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
6 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
102
|
|
|
{ |
103
|
6 |
|
$url = $this->domain.self::PATH_API_USER.'?'.$this->buildQueryString(array( |
104
|
6 |
|
'access_token' => $token->getToken(), |
105
|
4 |
|
)); |
106
|
6 |
|
return $url; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the default scopes used by this provider. |
111
|
|
|
* |
112
|
|
|
* This should not be a complete list of all scopes, but the minimum |
113
|
|
|
* required for the provider user interface! |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
6 |
|
protected function getDefaultScopes() |
118
|
2 |
|
{ |
119
|
6 |
|
return []; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Check a provider response for errors. |
124
|
|
|
* |
125
|
|
|
* @throws IdentityProviderException |
126
|
|
|
* @param ResponseInterface $response |
127
|
|
|
* @param string $data Parsed response data |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
15 |
|
protected function checkResponse(ResponseInterface $response, $data) |
131
|
|
|
{ |
132
|
15 |
|
if ($response->getStatusCode() >= 400) { |
133
|
3 |
|
throw PhabricatorIdentityProviderException::clientException($response, $data); |
134
|
12 |
|
} elseif (isset($data['error']) === true) { |
135
|
3 |
|
throw PhabricatorIdentityProviderException::oauthException($response, $data); |
136
|
|
|
} |
137
|
11 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Generate a user object from a successful user details request. |
141
|
|
|
* |
142
|
|
|
* @param array $response |
143
|
|
|
* @param AccessToken $token |
144
|
|
|
* @return Ofbeaton\OAuth2\Client\Provider\ResourceOwnerInterface |
145
|
|
|
*/ |
146
|
3 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
147
|
|
|
{ |
148
|
3 |
|
$user = new PhabricatorResourceOwner($response); |
149
|
3 |
|
$user->setDomain($this->domain); |
150
|
3 |
|
return $user; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|