diff --git a/README.md b/README.md index e376b14..7eb3900 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ $Jira = \Badoo\Jira\REST\Client::instance(); $Jira ->setJiraUrl('https://jira.example.com/') ->setAuth('user', 'token/password'); + // or via bearer token + ->setBearerToken('token'); ``` ## Create new issue diff --git a/src/REST/Client.php b/src/REST/Client.php index ea6d947..55dad5a 100644 --- a/src/REST/Client.php +++ b/src/REST/Client.php @@ -69,6 +69,16 @@ public function setAuth(string $login, string $secret) : Client return $this; } + /** + * @param string $token + * @return static + */ + public function setBearerToken(string $token) : Client + { + $this->getRawClient()->setBearerToken($token); + return $this; + } + /** * @return \Badoo\Jira\REST\Section\Jql */ diff --git a/src/REST/ClientRaw.php b/src/REST/ClientRaw.php index 5fbb1c7..2a44059 100644 --- a/src/REST/ClientRaw.php +++ b/src/REST/ClientRaw.php @@ -35,6 +35,8 @@ class ClientRaw private $login = ''; /** @var string - login's authentication secret. It can be API token (good) or bare user password (deprecated) */ private $secret = ''; + /** @var string a bearer token. It can be Personal Access Token or OAuth Access Token.*/ + private $bearerToken = ''; protected $request_timeout = 60; @@ -74,6 +76,19 @@ public function setAuth(string $login, string $secret) : ClientRaw return $this; } + /** + * Set a bearer token to use in the Authorization header of a REST API call. + * + * @param string $token bearer token + * @return ClientRaw + */ + public function setBearerToken(string $token) : ClientRaw + { + $this->bearerToken = $token; + + return $this; + } + public function getLogin() : string { return $this->login; @@ -226,7 +241,6 @@ private function request(string $http_method, string $api_method, $arguments = [ } $curl_options = [ - CURLOPT_USERPWD => $this->login . ':' . $this->secret, CURLOPT_URL => $url, CURLOPT_TIMEOUT => $this->request_timeout, CURLOPT_RETURNTRANSFER => true, @@ -239,6 +253,12 @@ private function request(string $http_method, string $api_method, $arguments = [ 'Content-Type' => 'application/json', ]; + if (strlen($this->bearerToken) > 0) { + $header_options['Authorization'] = 'Bearer ' . $this->bearerToken; + } else { + $curl_options[CURLOPT_USERPWD] = $this->login . ':' . $this->secret; + } + switch ($http_method) { case self::REQ_POST: $arguments = json_encode($arguments);