From fba4f21339b6efa395922c33c9da7504d489427f Mon Sep 17 00:00:00 2001 From: Ere Maijala Date: Thu, 27 Jun 2024 09:00:22 +0300 Subject: [PATCH] [FINNA-2393] Add proper authentication support. --- conf/datasources.ini.sample | 8 ++++++++ src/RecordManager/Base/Harvest/AbstractBase.php | 12 ++++++++++++ src/RecordManager/Base/Harvest/HTTPFiles.php | 2 +- src/RecordManager/Base/Harvest/OaiPmh.php | 2 +- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/conf/datasources.ini.sample b/conf/datasources.ini.sample index 182a5b8e..03ed30a1 100644 --- a/conf/datasources.ini.sample +++ b/conf/datasources.ini.sample @@ -41,6 +41,12 @@ ; ; RecordManager settings: ; type Harvester type (HTTPFiles, OAI-PMH (default), SFX, SierraApi) +; url Harvesting URL +; username Username for HTTP authentication (OAI-PMH, HTTPFiled) +; password Password for HTTP authentication (OAI-PMH, HTTPFiled) +; authType Authentication type for HTTP authentication (OAI-PMH, HTTPFiled). +; Allowed options are "basic" (default), "digest" and "ntlm". +; Digest and NTLM authentication require the Curl adapter to be used (see HTTP section in recordmanager.ini). ; institution The institution code mapped to the source (required) ; recordXPath xpath expression used when loading records from a file to identify a single record (optional, e.g. //record) ; format Record format in RecordManager @@ -165,6 +171,8 @@ ;[arc] ;institution = MyInst ;url = http://oai-pmh.ead-server/request +;username = "user" +;password = "secret" ;metadataPrefix = ead ;recordSplitterClass = "\RecordManager\Base\Splitter\Ead" ;format = ead diff --git a/src/RecordManager/Base/Harvest/AbstractBase.php b/src/RecordManager/Base/Harvest/AbstractBase.php index 914fcf4a..db1639dd 100644 --- a/src/RecordManager/Base/Harvest/AbstractBase.php +++ b/src/RecordManager/Base/Harvest/AbstractBase.php @@ -98,6 +98,13 @@ abstract class AbstractBase */ protected $baseURL = null; + /** + * HTTP authentication settings + * + * @var ?array + */ + protected $httpAuth = null; + /** * Source ID * @@ -255,6 +262,11 @@ public function init(string $source, bool $verbose, bool $reharvest): void } $this->baseURL = $settings['url']; + if (($username = $settings['username']) && ($password = $settings['password'])) { + $type = $settings['authType'] ?? 'basic'; + $this->httpAuth = [$username, $password, $type]; + } + if (!empty($settings['preTransformation'])) { foreach ((array)$settings['preTransformation'] as $transformation) { $style = new \DOMDocument(); diff --git a/src/RecordManager/Base/Harvest/HTTPFiles.php b/src/RecordManager/Base/Harvest/HTTPFiles.php index 866516eb..54dc746b 100644 --- a/src/RecordManager/Base/Harvest/HTTPFiles.php +++ b/src/RecordManager/Base/Harvest/HTTPFiles.php @@ -257,7 +257,7 @@ protected function retrieveFileList() protected function retrieveFile($filename) { $url = $this->baseURL . $filename; - $request = $this->httpService->createClient($url); + $request = $this->httpService->createClient($url, ['auth' => $this->httpAuth]); $this->infoMsg("Sending request: $url"); // Perform request and throw an exception on error: diff --git a/src/RecordManager/Base/Harvest/OaiPmh.php b/src/RecordManager/Base/Harvest/OaiPmh.php index be0136ad..0ed2b96a 100644 --- a/src/RecordManager/Base/Harvest/OaiPmh.php +++ b/src/RecordManager/Base/Harvest/OaiPmh.php @@ -395,7 +395,7 @@ protected function normalizeDate($date) protected function sendRequest($verb, $params = []) { // Set up the request: - $client = $this->httpService->createClient($this->baseURL); + $client = $this->httpService->createClient($this->baseURL, ['auth' => $this->httpAuth]); $params['verb'] = $verb; $url = $this->httpService->appendQueryParams($this->baseURL, $params);