Skip to content

Commit f374ef8

Browse files
committed
Merge branch 'feature/cookie_login' into develop
2 parents 88b8495 + 6ba454f commit f374ef8

File tree

4 files changed

+23
-80
lines changed

4 files changed

+23
-80
lines changed

README.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,16 @@
2424

2525
2. Next, run the Composer command to install the latest version of php jira rest client.
2626
``` sh
27-
php composer.phar require lesstif/php-jira-rest-client "^1.7.0"
27+
php composer.phar require lesstif/php-jira-rest-client
2828
```
2929
or add the following to your composer.json file.
3030
```json
3131
{
3232
"require": {
33-
"lesstif/php-jira-rest-client": "^1.7.0"
33+
"lesstif/php-jira-rest-client": "^1.19"
3434
}
3535
}
3636
```
37-
**Note:**
38-
If you are using **laravel 5.0 or 5.1**(this version dependent on phpdotenv 1.x), then use **"1.5.\*"** version instead.
3937

4038
3. Then run Composer's install or update commands to complete installation.
4139

@@ -61,16 +59,13 @@ copy .env.example file to .env on your project root.
6159
```sh
6260
JIRA_HOST="https://your-jira.host.com"
6361
JIRA_USER="jira-username"
64-
JIRA_PASS="jira-password"
62+
JIRA_PASS="jira-password-OR-api-token"
6563
# to enable session cookie authorization
66-
# COOKIE_AUTH_ENABLED=1
67-
```
68-
69-
Or for OAuth authorization:
70-
71-
```sh
72-
JIRA_ACCESS_TOKEN="access-token"
64+
# COOKIE_AUTH_ENABLED=true
7365
```
66+
**Note:**
67+
As of March 15, 2018, in accordance to the [Atlassian REST API Policy](https://developer.atlassian.com/platform/marketplace/atlassian-rest-api-policy/), Basic auth with password to be deprecated.
68+
Instead of password, you should using [API token](https://confluence.atlassian.com/cloud/api-tokens-938839638.html).
7469

7570
**important-note:** If you are using previous versions(a prior v1.2), you should move config.jira.json to .env and will edit it.
7671

src/Configuration/AbstractConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,6 @@ public function getOAuthAccessToken()
154154
*/
155155
public function isCookieAuthorizationEnabled()
156156
{
157-
return $this->oauthAccessToken;
157+
return $this->cookieAuthEnabled;
158158
}
159159
}

src/Configuration/DotEnvConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct($path = '.')
3333
$this->jiraUser = $this->env('JIRA_USER');
3434
$this->jiraPassword = $this->env('JIRA_PASS');
3535
$this->oauthAccessToken = $this->env('OAUTH_ACCESS_TOKEN');
36-
$this->cookieAuthEnabled = $this->env('COOKIE_AUTH_ENABLED');
36+
$this->cookieAuthEnabled = $this->env('COOKIE_AUTH_ENABLED', false);
3737
$this->jiraLogFile = $this->env('JIRA_LOG_FILE', 'jira-rest-client.log');
3838
$this->jiraLogLevel = $this->env('JIRA_LOG_LEVEL', 'WARNING');
3939
$this->curlOptSslVerifyHost = $this->env('CURLOPT_SSL_VERIFYHOST', false);

src/JiraClient.php

Lines changed: 14 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
*/
1414
class JiraClient
1515
{
16-
/**
17-
* @var static
18-
*/
19-
private static $_instance;
20-
2116
/**
2217
* Json Mapper.
2318
*
@@ -61,23 +56,11 @@ class JiraClient
6156
protected $configuration;
6257

6358
/**
64-
* @param ConfigurationInterface|null $configuration
65-
* @param Logger|null $logger
66-
* @param string $path
67-
*
68-
* @throws JiraException
69-
* @throws \Exception
59+
* cookie file name
7060
*
71-
* @return static Service instance
61+
* @var string
7262
*/
73-
public static function getInstance(ConfigurationInterface $configuration = null, Logger $logger = null, $path = './')
74-
{
75-
if (empty(static::$_instance)) {
76-
static::$_instance = new static($configuration, $logger, $path);
77-
}
78-
79-
return static::$_instance;
80-
}
63+
protected $cookie = 'jira-cookies.txt';
8164

8265
/**
8366
* Constructor.
@@ -223,7 +206,7 @@ public function exec($context, $post_data = null, $custom_request = null)
223206
}
224207

225208
curl_setopt($ch, CURLOPT_HTTPHEADER,
226-
['Accept: */*', 'Content-Type: application/json']);
209+
['Accept: */*', 'Content-Type: application/json', 'X-Atlassian-Token: no-check']);
227210

228211
curl_setopt($ch, CURLOPT_VERBOSE, $this->getConfiguration()->isCurlOptVerbose());
229212

@@ -449,54 +432,19 @@ protected function createUrlByContext($context)
449432
*/
450433
protected function authorization($ch)
451434
{
452-
$token = $this->getConfiguration()->getOAuthAccessToken();
453-
if ($token) {
454-
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer $token"]);
435+
// use cookie
436+
if ($this->getConfiguration()->isCookieAuthorizationEnabled()) {
437+
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
438+
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
455439

456-
return;
440+
$this->log->addDebug("Using cookie..");
457441
}
458442

459-
if ($this->cookieSessionAuthorization($ch)) {
460-
return;
461-
}
462-
463-
$username = $this->getConfiguration()->getJiraUser();
464-
$password = $this->getConfiguration()->getJiraPassword();
465-
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
466-
}
467-
468-
/**
469-
* @param resource $ch
470-
*
471-
* @return bool
472-
*/
473-
protected function cookieSessionAuthorization($ch)
474-
{
475-
$cookieAuthOn = $this->getConfiguration()->isCookieAuthorizationEnabled();
476-
if (!$cookieAuthOn) {
477-
return false;
478-
}
479-
480-
try {
481-
$authService = AuthService::getInstance($this->getConfiguration(), $this->log);
482-
if ($authService->isAuthInProgress()) {
483-
return false;
484-
}
485-
486-
if (!$authService->isAuthorized()) {
487-
$authService->authorizeWithCookie();
488-
}
489-
490-
$name = $authService->getSessionCookieName();
491-
$value = $authService->getSessionCookieValue();
492-
493-
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Cookie: $name=$value"]);
494-
495-
return true;
496-
} catch (\Exception $e) {
497-
$this->log->addError("Cookie authorization error: {$e->getMessage()}");
498-
499-
return false;
443+
// if cookie file not exist, using id/pwd login
444+
if (! file_exists($this->cookie)) {
445+
$username = $this->getConfiguration()->getJiraUser();
446+
$password = $this->getConfiguration()->getJiraPassword();
447+
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
500448
}
501449
}
502450

0 commit comments

Comments
 (0)