Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auth): add support Bearer authentication #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/REST/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
22 changes: 21 additions & 1 deletion src/REST/ClientRaw.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down