Skip to content

Commit e0f3b35

Browse files
committed
fixed #173 - add new env variable COOKIE_FILE and array config variable cookieFile.
1 parent 01bab4b commit e0f3b35

File tree

6 files changed

+53
-15
lines changed

6 files changed

+53
-15
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ JIRA_USER="jira-username"
6565
JIRA_PASS="jira-password-OR-api-token"
6666
# to enable session cookie authorization
6767
# COOKIE_AUTH_ENABLED=true
68+
# COOKIE_FILE=storage/jira-cookie.txt
6869
```
6970

7071
**Important Note:**
@@ -90,6 +91,7 @@ $iss = new IssueService(new ArrayConfiguration(
9091
'jiraPassword' => 'jira-password-OR-api-token',
9192
// to enable session cookie authorization (with basic authorization only)
9293
'cookieAuthEnabled' => true,
94+
'cookieFile' => storage_path('jira-cookie.txt'),
9395
)
9496
));
9597
```

src/Configuration/AbstractConfiguration.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ abstract class AbstractConfiguration implements ConfigurationInterface
8484
*/
8585
protected $cookieAuthEnabled;
8686

87+
/**
88+
* HTTP cookie file name.
89+
*
90+
* @var string
91+
*/
92+
protected $cookieFile;
93+
8794
/**
8895
* @return string
8996
*/
@@ -185,4 +192,12 @@ public function getDefaultUserAgentString()
185192

186193
return sprintf('curl/%s (%s)', $curlVersion['version'], $curlVersion['host']);
187194
}
195+
196+
/**
197+
* @return string
198+
*/
199+
public function getCookieFile()
200+
{
201+
return $this->cookieFile;
202+
}
188203
}

src/Configuration/ArrayConfiguration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function __construct(array $configuration)
2424
$this->curlOptSslVerifyPeer = false;
2525
$this->curlOptVerbose = false;
2626
$this->cookieAuthEnabled = false;
27+
$this->cookieFile = 'jira-cookie.txt';
2728
$this->curlOptUserAgent = $this->getDefaultUserAgentString();
2829

2930
foreach ($configuration as $key => $value) {

src/Configuration/ConfigurationInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,11 @@ public function getOAuthAccessToken();
8989
* @return bool
9090
*/
9191
public function isCookieAuthorizationEnabled();
92+
93+
/**
94+
* get HTTP cookie file name.
95+
*
96+
* @return mixed
97+
*/
98+
public function getCookieFile();
9299
}

src/Configuration/DotEnvConfiguration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function __construct($path = '.')
3434
$this->jiraPassword = $this->env('JIRA_PASS');
3535
$this->oauthAccessToken = $this->env('OAUTH_ACCESS_TOKEN');
3636
$this->cookieAuthEnabled = $this->env('COOKIE_AUTH_ENABLED', false);
37+
$this->cookieFile = $this->env('COOKIE_FILE', 'jira-cookie.txt');
3738
$this->jiraLogFile = $this->env('JIRA_LOG_FILE', 'jira-rest-client.log');
3839
$this->jiraLogLevel = $this->env('JIRA_LOG_LEVEL', 'WARNING');
3940
$this->curlOptSslVerifyHost = $this->env('CURLOPT_SSL_VERIFYHOST', false);

src/JiraClient.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ class JiraClient
5454
*/
5555
protected $configuration;
5656

57-
/**
58-
* cookie file name.
59-
*
60-
* @var string
61-
*/
62-
protected $cookie = 'jira-cookies.txt';
63-
6457
/**
6558
* Constructor.
6659
*
@@ -164,12 +157,13 @@ protected function filterNullVariable($haystack)
164157
* @param string $context Rest API context (ex.:issue, search, etc..)
165158
* @param string $post_data
166159
* @param string $custom_request [PUT|DELETE]
160+
* @param string $cookieFile cookie file
167161
*
168162
* @throws JiraException
169163
*
170164
* @return string
171165
*/
172-
public function exec($context, $post_data = null, $custom_request = null)
166+
public function exec($context, $post_data = null, $custom_request = null, $cookieFile = null)
173167
{
174168
$url = $this->createUrlByContext($context);
175169

@@ -198,7 +192,7 @@ public function exec($context, $post_data = null, $custom_request = null)
198192
}
199193
}
200194

201-
$this->authorization($ch);
195+
$this->authorization($ch, $cookieFile);
202196

203197
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->getConfiguration()->isCurlOptSslVerifyHost());
204198
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->getConfiguration()->isCurlOptSslVerifyPeer());
@@ -434,18 +428,22 @@ protected function createUrlByContext($context)
434428
*
435429
* @param resource $ch
436430
*/
437-
protected function authorization($ch)
431+
protected function authorization($ch, $cookieFile = null)
438432
{
439433
// use cookie
440434
if ($this->getConfiguration()->isCookieAuthorizationEnabled()) {
441-
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
442-
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
435+
if ($cookieFile === null){
436+
$cookieFile = $this->getConfiguration()->getCookieFile();
437+
}
438+
439+
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
440+
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
443441

444442
$this->log->addDebug('Using cookie..');
445443
}
446444

447445
// if cookie file not exist, using id/pwd login
448-
if (!file_exists($this->cookie)) {
446+
if (!file_exists($cookieFile)) {
449447
$username = $this->getConfiguration()->getJiraUser();
450448
$password = $this->getConfiguration()->getJiraPassword();
451449
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
@@ -505,12 +503,13 @@ public function toHttpQueryParameter($paramArray)
505503
* @param $url full url
506504
* @param $outDir save dir
507505
* @param $file save filename
506+
* @param $cookieFile cookie filename
508507
*
509508
* @throws JiraException
510509
*
511510
* @return bool|mixed
512511
*/
513-
public function download($url, $outDir, $file)
512+
public function download($url, $outDir, $file, $cookieFile = null)
514513
{
515514
$file = fopen($outDir.DIRECTORY_SEPARATOR.$file, 'w');
516515

@@ -521,7 +520,7 @@ public function download($url, $outDir, $file)
521520
// output to file handle
522521
curl_setopt($ch, CURLOPT_FILE, $file);
523522

524-
$this->authorization($ch);
523+
$this->authorization($ch, $cookieFile);
525524

526525
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->getConfiguration()->isCurlOptSslVerifyHost());
527526
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->getConfiguration()->isCurlOptSslVerifyPeer());
@@ -577,4 +576,17 @@ public function download($url, $outDir, $file)
577576

578577
return $response;
579578
}
579+
580+
/**
581+
* setting cookie file path.
582+
*
583+
* @param $cookieFile
584+
* @return $this
585+
*/
586+
public function setCookieFile($cookieFile)
587+
{
588+
$this->cookieFile = $cookieFile;
589+
590+
return $this;
591+
}
580592
}

0 commit comments

Comments
 (0)