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

http_authentication with server using rets-version 1.8 (401 Unauthorized) #161

Open
wants to merge 4 commits 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
21 changes: 20 additions & 1 deletion src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Configuration
protected $http_authentication = 'digest';
/** @var \PHRETS\Strategies\Strategy */
protected $strategy;
protected $sessionTempDir = null;
protected $options = [];

public function __construct()
Expand Down Expand Up @@ -135,6 +136,24 @@ public function setUsername($username)
return $this;
}

/**
* @param string $username
* @return $this
*/
public function setSessionTempDir($sessionTempDir)
{
$this->sessionTempdDir = $sessionTempDir;
return $this;
}

/**
* @return mixed
*/
public function getSessionTempDir()
{
return $this->sessionTempDir;
}

/**
* @param $name
* @param $value
Expand Down Expand Up @@ -240,7 +259,7 @@ public function userAgentDigestHash(Session $session)
*/
public function setHttpAuthenticationMethod($auth_method)
{
if (!in_array($auth_method, [self::AUTH_BASIC, self::AUTH_DIGEST])) {
if ($auth_method && !in_array($auth_method, [self::AUTH_BASIC, self::AUTH_DIGEST])) {
throw new \InvalidArgumentException("Given authentication method is invalid. Must be 'basic' or 'digest'");
}
$this->http_authentication = $auth_method;
Expand Down
21 changes: 19 additions & 2 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ protected function request($capability, $options = [])

$response = new \PHRETS\Http\Response($response);

$this->removeSessionCookieFile($options);

$this->last_response = $response;

if ($response->getHeader('Set-Cookie')) {
Expand All @@ -349,7 +351,7 @@ protected function request($capability, $options = [])
}
}
}

if (preg_match('/text\/xml/', $response->getHeader('Content-Type')) and $capability != 'GetObject') {
$parser = $this->grab(Strategy::PARSER_XML);
$xml = $parser->parse($response);
Expand Down Expand Up @@ -481,7 +483,7 @@ public function getDefaultOptions()
'Accept-Encoding' => 'gzip',
'Accept' => '*/*',
],
'curl' => [ CURLOPT_COOKIEFILE => tempnam('/tmp', 'phrets') ]
'curl' => [ CURLOPT_COOKIEFILE => tempnam($this->configuration->getSessionTempDir(), 'phrets') ]
];

// disable following 'Location' header (redirects) automatically
Expand All @@ -492,6 +494,21 @@ public function getDefaultOptions()
return $defaults;
}

/**
* If the session cookie file have beend created, we will remove it.
*
* @param array $options Assoc array that came from the method getDefaultOptions.
*
* @return void
*/
private function removeSessionCookieFile(array $options)
{
$tmpFile = $options['curl'][CURLOPT_COOKIEFILE];
if (file_exists($tmpFile)) {
unlink($tmpFile);
}
}

public function setParser($parser_name, $parser_object)
{
/** @var Container $container */
Expand Down