|
13 | 13 | */ |
14 | 14 | class JiraClient |
15 | 15 | { |
16 | | - /** |
17 | | - * @var static |
18 | | - */ |
19 | | - private static $_instance; |
20 | | - |
21 | 16 | /** |
22 | 17 | * Json Mapper. |
23 | 18 | * |
@@ -61,23 +56,11 @@ class JiraClient |
61 | 56 | protected $configuration; |
62 | 57 |
|
63 | 58 | /** |
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 |
70 | 60 | * |
71 | | - * @return static Service instance |
| 61 | + * @var string |
72 | 62 | */ |
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'; |
81 | 64 |
|
82 | 65 | /** |
83 | 66 | * Constructor. |
@@ -223,7 +206,7 @@ public function exec($context, $post_data = null, $custom_request = null) |
223 | 206 | } |
224 | 207 |
|
225 | 208 | curl_setopt($ch, CURLOPT_HTTPHEADER, |
226 | | - ['Accept: */*', 'Content-Type: application/json']); |
| 209 | + ['Accept: */*', 'Content-Type: application/json', 'X-Atlassian-Token: no-check']); |
227 | 210 |
|
228 | 211 | curl_setopt($ch, CURLOPT_VERBOSE, $this->getConfiguration()->isCurlOptVerbose()); |
229 | 212 |
|
@@ -449,54 +432,19 @@ protected function createUrlByContext($context) |
449 | 432 | */ |
450 | 433 | protected function authorization($ch) |
451 | 434 | { |
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); |
455 | 439 |
|
456 | | - return; |
| 440 | + $this->log->addDebug("Using cookie.."); |
457 | 441 | } |
458 | 442 |
|
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"); |
500 | 448 | } |
501 | 449 | } |
502 | 450 |
|
|
0 commit comments