Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / auth / adapter / PhutilJIRAAuthAdapter.php
bloba18b39690bd1691fcdf3061ece547eb633d2dac4
1 <?php
3 /**
4 * Authentication adapter for JIRA OAuth1.
5 */
6 final class PhutilJIRAAuthAdapter extends PhutilOAuth1AuthAdapter {
8 // TODO: JIRA tokens expire (after 5 years) and we could surface and store
9 // that.
11 private $jiraBaseURI;
12 private $adapterDomain;
13 private $userInfo;
15 public function setJIRABaseURI($jira_base_uri) {
16 $this->jiraBaseURI = $jira_base_uri;
17 return $this;
20 public function getJIRABaseURI() {
21 return $this->jiraBaseURI;
24 protected function newAccountIdentifiers() {
25 // Make sure the handshake is finished; this method is used for its
26 // side effect by Auth providers.
27 $this->getHandshakeData();
29 $info = $this->getUserInfo();
31 // See T13493. Older versions of JIRA provide a "key" with a username or
32 // email address. Newer versions of JIRA provide a GUID "accountId".
33 // Intermediate versions of JIRA provide both.
35 $identifiers = array();
37 $account_key = idx($info, 'key');
38 if ($account_key !== null) {
39 $identifiers[] = $this->newAccountIdentifier($account_key);
42 $account_id = idx($info, 'accountId');
43 if ($account_id !== null) {
44 $identifiers[] = $this->newAccountIdentifier(
45 sprintf(
46 'accountId(%s)',
47 $account_id));
50 return $identifiers;
53 public function getAccountName() {
54 return idx($this->getUserInfo(), 'name');
57 public function getAccountImageURI() {
58 $avatars = idx($this->getUserInfo(), 'avatarUrls');
59 if ($avatars) {
60 return idx($avatars, '48x48');
62 return null;
65 public function getAccountRealName() {
66 return idx($this->getUserInfo(), 'displayName');
69 public function getAccountEmail() {
70 return idx($this->getUserInfo(), 'emailAddress');
73 public function getAdapterType() {
74 return 'jira';
77 public function getAdapterDomain() {
78 return $this->adapterDomain;
81 public function setAdapterDomain($domain) {
82 $this->adapterDomain = $domain;
83 return $this;
86 protected function getSignatureMethod() {
87 return 'RSA-SHA1';
90 protected function getRequestTokenURI() {
91 return $this->getJIRAURI('plugins/servlet/oauth/request-token');
94 protected function getAuthorizeTokenURI() {
95 return $this->getJIRAURI('plugins/servlet/oauth/authorize');
98 protected function getValidateTokenURI() {
99 return $this->getJIRAURI('plugins/servlet/oauth/access-token');
102 private function getJIRAURI($path) {
103 return rtrim($this->jiraBaseURI, '/').'/'.ltrim($path, '/');
106 private function getUserInfo() {
107 if ($this->userInfo === null) {
108 $this->userInfo = $this->newUserInfo();
111 return $this->userInfo;
114 private function newUserInfo() {
115 // See T13493. Try a relatively modern (circa early 2020) API call first.
116 try {
117 return $this->newJIRAFuture('rest/api/3/myself', 'GET')
118 ->resolveJSON();
119 } catch (Exception $ex) {
120 // If we failed the v3 call, assume the server version is too old
121 // to support this API and fall back to trying the older method.
124 $session = $this->newJIRAFuture('rest/auth/1/session', 'GET')
125 ->resolveJSON();
127 // The session call gives us the username, but not the user key or other
128 // information. Make a second call to get additional information.
130 $params = array(
131 'username' => $session['name'],
134 return $this->newJIRAFuture('rest/api/2/user', 'GET', $params)
135 ->resolveJSON();
138 public static function newJIRAKeypair() {
139 $config = array(
140 'digest_alg' => 'sha512',
141 'private_key_bits' => 4096,
142 'private_key_type' => OPENSSL_KEYTYPE_RSA,
145 $res = openssl_pkey_new($config);
146 if (!$res) {
147 throw new Exception(pht('%s failed!', 'openssl_pkey_new()'));
150 $private_key = null;
151 $ok = openssl_pkey_export($res, $private_key);
152 if (!$ok) {
153 throw new Exception(pht('%s failed!', 'openssl_pkey_export()'));
156 $public_key = openssl_pkey_get_details($res);
157 if (!$ok || empty($public_key['key'])) {
158 throw new Exception(pht('%s failed!', 'openssl_pkey_get_details()'));
160 $public_key = $public_key['key'];
162 return array($public_key, $private_key);
167 * JIRA indicates that the user has clicked the "Deny" button by passing a
168 * well known `oauth_verifier` value ("denied"), which we check for here.
170 protected function willFinishOAuthHandshake() {
171 $jira_magic_word = 'denied';
172 if ($this->getVerifier() == $jira_magic_word) {
173 throw new PhutilAuthUserAbortedException();
177 public function newJIRAFuture($path, $method, $params = array()) {
178 if ($method == 'GET') {
179 $uri_params = $params;
180 $body_params = array();
181 } else {
182 // For other types of requests, JIRA expects the request body to be
183 // JSON encoded.
184 $uri_params = array();
185 $body_params = phutil_json_encode($params);
188 $uri = new PhutilURI($this->getJIRAURI($path), $uri_params);
190 // JIRA returns a 415 error if we don't provide a Content-Type header.
192 return $this->newOAuth1Future($uri, $body_params)
193 ->setMethod($method)
194 ->addHeader('Content-Type', 'application/json');