Changeset View
Changeset View
Standalone View
Standalone View
src/applications/auth/adapter/PhutilJIRAAuthAdapter.php
| <?php | <?php | ||||
| /** | /** | ||||
| * Authentication adapter for JIRA OAuth1. | * Authentication adapter for JIRA OAuth1. | ||||
| */ | */ | ||||
| final class PhutilJIRAAuthAdapter extends PhutilOAuth1AuthAdapter { | final class PhutilJIRAAuthAdapter extends PhutilOAuth1AuthAdapter { | ||||
| // TODO: JIRA tokens expire (after 5 years) and we could surface and store | // TODO: JIRA tokens expire (after 5 years) and we could surface and store | ||||
| // that. | // that. | ||||
| private $jiraBaseURI; | private $jiraBaseURI; | ||||
| private $adapterDomain; | private $adapterDomain; | ||||
| private $currentSession; | |||||
| private $userInfo; | private $userInfo; | ||||
| public function setJIRABaseURI($jira_base_uri) { | public function setJIRABaseURI($jira_base_uri) { | ||||
| $this->jiraBaseURI = $jira_base_uri; | $this->jiraBaseURI = $jira_base_uri; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getJIRABaseURI() { | public function getJIRABaseURI() { | ||||
| ▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | final class PhutilJIRAAuthAdapter extends PhutilOAuth1AuthAdapter { | ||||
| } | } | ||||
| private function getJIRAURI($path) { | private function getJIRAURI($path) { | ||||
| return rtrim($this->jiraBaseURI, '/').'/'.ltrim($path, '/'); | return rtrim($this->jiraBaseURI, '/').'/'.ltrim($path, '/'); | ||||
| } | } | ||||
| private function getUserInfo() { | private function getUserInfo() { | ||||
| if ($this->userInfo === null) { | if ($this->userInfo === null) { | ||||
| $this->currentSession = $this->newJIRAFuture('rest/auth/1/session', 'GET') | $this->userInfo = $this->newUserInfo(); | ||||
| } | |||||
| return $this->userInfo; | |||||
| } | |||||
| private function newUserInfo() { | |||||
| // See T13493. Try a relatively modern (circa early 2020) API call first. | |||||
| try { | |||||
| return $this->newJIRAFuture('rest/api/3/myself', 'GET') | |||||
| ->resolveJSON(); | |||||
| } catch (Exception $ex) { | |||||
| // If we failed the v3 call, assume the server version is too old | |||||
| // to support this API and fall back to trying the older method. | |||||
| } | |||||
| $session = $this->newJIRAFuture('rest/auth/1/session', 'GET') | |||||
| ->resolveJSON(); | ->resolveJSON(); | ||||
| // The session call gives us the username, but not the user key or other | // The session call gives us the username, but not the user key or other | ||||
| // information. Make a second call to get additional information. | // information. Make a second call to get additional information. | ||||
| $params = array( | $params = array( | ||||
| 'username' => $this->currentSession['name'], | 'username' => $session['name'], | ||||
| ); | ); | ||||
| $this->userInfo = $this->newJIRAFuture('rest/api/2/user', 'GET', $params) | return $this->newJIRAFuture('rest/api/2/user', 'GET', $params) | ||||
| ->resolveJSON(); | ->resolveJSON(); | ||||
| } | } | ||||
| return $this->userInfo; | |||||
| } | |||||
| public static function newJIRAKeypair() { | public static function newJIRAKeypair() { | ||||
| $config = array( | $config = array( | ||||
| 'digest_alg' => 'sha512', | 'digest_alg' => 'sha512', | ||||
| 'private_key_bits' => 4096, | 'private_key_bits' => 4096, | ||||
| 'private_key_type' => OPENSSL_KEYTYPE_RSA, | 'private_key_type' => OPENSSL_KEYTYPE_RSA, | ||||
| ); | ); | ||||
| $res = openssl_pkey_new($config); | $res = openssl_pkey_new($config); | ||||
| ▲ Show 20 Lines • Show All 52 Lines • Show Last 20 Lines | |||||