diff --git a/FEATURES/216638 b/FEATURES/216638 new file mode 100644 index 0000000000000000000000000000000000000000..74d81b3c9a5d3004684bd54e6f4fd32e121f7ca3 --- /dev/null +++ b/FEATURES/216638 @@ -0,0 +1,10 @@ + '216638' => + ['Label' => $this->_('SIGB PMB : prise en charge de de l'authentification HTTP basic'), + 'Desc' => '', + 'Image' => '', + 'Video' => '', + 'Category' => '', + 'Right' => function($feature_description, $user) {return true;}, + 'Wiki' => '', + 'Test' => '', + 'Date' => '2025-03-07'], \ No newline at end of file diff --git a/VERSIONS_WIP/216638 b/VERSIONS_WIP/216638 new file mode 100644 index 0000000000000000000000000000000000000000..5b3e665df69ff918708318989eb92e38deca0e41 --- /dev/null +++ b/VERSIONS_WIP/216638 @@ -0,0 +1 @@ + - fonctionnalité #216638 : SIGB PMB : prise en charge de de l'authentification HTTP basic \ No newline at end of file diff --git a/cosmogramme/php/fonctions/objets_saisie.php b/cosmogramme/php/fonctions/objets_saisie.php index ad76dcdcb866fc448a2e00bd54f8326d159c537e..96eb180874c2e94ada9688db2bd2eead53076765 100644 --- a/cosmogramme/php/fonctions/objets_saisie.php +++ b/cosmogramme/php/fonctions/objets_saisie.php @@ -105,8 +105,12 @@ function getBlocsParams($id_bib, $type, $valeurs) { if (in_array($clef, [Class_IntBib::COM_PMB, Class_IntBib::COM_VSMART, Class_IntBib::COM_MICROBIB, Class_IntBib::COM_BIBLIXNET, Class_IntBib::COM_WATERBEAR])) $champs_params[0] = ['url_serveur']; - if (Class_IntBib::COM_PMB == $clef) + if (Class_IntBib::COM_PMB == $clef) { + $champs_params[0][] = ['api_user' => fn($id, $valeur) => getChamp($id, $valeur, 30)]; + $champs_params[0][] = ['api_pass' => fn($id, $valeur) => getChamp($id, $valeur, 30)]; $champs_params[0][] = ['exp_bulletin_is_exp_notice' => fn($id, $valeur) => getOuiNon($id, $valeur)]; + } + if ($clef == Class_IntBib::COM_CARTHAME) $champs_params[0] = ['url_serveur', 'key','sigb_field_name']; diff --git a/library/Class/WebService/SIGB/PMB.php b/library/Class/WebService/SIGB/PMB.php index ab60a070a267ec14f09b4dee4fa17b4c0e0e13de..bfe09eb006e503736c1d18f4ffcebf72138a977f 100644 --- a/library/Class/WebService/SIGB/PMB.php +++ b/library/Class/WebService/SIGB/PMB.php @@ -28,6 +28,9 @@ class Class_WebService_SIGB_PMB extends Class_WebService_SIGB_Abstract { && '1' == $params['exp_bulletin_is_exp_notice']) $service->bePeriodicIdWithoutBull(); + if ($params['api_user'] ?? null && $params['api_pass'] ?? null) + $service->setCredentials($params['api_user'], $params['api_pass']); + return $service; } } diff --git a/library/Class/WebService/SIGB/PMB/Service.php b/library/Class/WebService/SIGB/PMB/Service.php index fb6919cc255731948381fe914109fdf187e6bf5b..e9f6998ab9cb1af47916aa1a382785b582892714 100644 --- a/library/Class/WebService/SIGB/PMB/Service.php +++ b/library/Class/WebService/SIGB/PMB/Service.php @@ -31,7 +31,9 @@ class Class_Webservice_SIGB_PMB_Service extends Class_WebService_SIGB_AbstractSe $_json_web_client, $_pmb_url, $_periodic_id_without_bull = false, - $_user; + $_user, + $_api_user, + $_api_pass; public static function getService($pmb_url) { @@ -52,7 +54,7 @@ class Class_Webservice_SIGB_PMB_Service extends Class_WebService_SIGB_AbstractSe public function getJsonWebClient() { if(!$this->_json_web_client) { - $this->_json_web_client = new JsonWebClient($this->_pmb_url); + $this->_json_web_client = new JsonWebClient($this); } return $this->_json_web_client; } @@ -70,6 +72,26 @@ class Class_Webservice_SIGB_PMB_Service extends Class_WebService_SIGB_AbstractSe } + public function setCredentials(string $user, string $password): self + { + $this->_api_user = $user; + $this->_api_pass = $password; + return $this; + } + + + public function getApiUser(): string + { + return $this->_api_user ?? ''; + } + + + public function getApiPass(): string + { + return $this->_api_pass ?? ''; + } + + public function providesAuthentication() :bool { return true; } @@ -332,12 +354,13 @@ class Class_Webservice_SIGB_PMB_Service extends Class_WebService_SIGB_AbstractSe class JsonWebClient { - protected $_http_client, - $_url; + protected + $_service, + $_http_client; - public function __construct($url) { - $this->_url = $url; + public function __construct($service) { + $this->_service = $service; } @@ -358,10 +381,14 @@ class JsonWebClient { public function httpPost($method, $params) { $http_client = $this->getHttpClient(); $http_client->resetParameters(); - $http_client->setUri($this->_url); + $http_client->setUri($this->_service->getServerRoot()); $http_client->setMethod(Zend_Http_Client::POST); $http_client->setEnctype('application/json'); + if (($user = $this->_service->getApiUser()) + && ($pass = $this->_service->getApiPass())) + $http_client->setAuth($user, $pass); + $data = json_encode(['method' => $method, 'params' => $params, 'id' => 1]); diff --git a/tests/library/Class/HttpClientForTest.php b/tests/library/Class/HttpClientForTest.php index 100788cefdf2dd97cb369b0dc96b959aa3605d53..3fe3445f4ee08a1a2598b9b6d7a95b0983dafd9c 100644 --- a/tests/library/Class/HttpClientForTest.php +++ b/tests/library/Class/HttpClientForTest.php @@ -202,4 +202,16 @@ class HttpClientForTest extends Zend_Http_Client { return $this->config; } + + + public function getAuthUser(): string + { + return $this->auth['user'] ?? ''; + } + + + public function getAuthPassword(): string + { + return $this->auth['password'] ?? ''; + } } diff --git a/tests/library/Class/WebService/SIGB/PMBAuthenticationTest.php b/tests/library/Class/WebService/SIGB/PMBAuthenticationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..fe392d39bae54a1ec9b1bf452305ed206f7f3711 --- /dev/null +++ b/tests/library/Class/WebService/SIGB/PMBAuthenticationTest.php @@ -0,0 +1,80 @@ +<?php +/** + * Copyright (c) 2012-2024, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with BOKEH; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +class PMBAuthenticationTest extends AbstractControllerTestCase +{ + public function setUp(): void + { + parent::setUp(); + + $this->_buildTemplateProfil(['id' => 8]); + + $this->fixture(Class_IntBib::class, + ['id' => 31, + 'libelle' => 'Le Mas-d\'Azil', + 'comm_sigb' => Class_IntBib::COM_PMB, + 'comm_params' => serialize(['url_serveur' => 'https://mypmb.com/ws', + 'api_user' => 'admin', + 'api_pass' => 'PMB455'])]); + + $this->fixture(Class_Notice::class, + ['id' => 1011, + 'titre_principal' => 'Le photographe', + 'auteur_principal' => 'Guibert']); + + $this->fixture(Class_Exemplaire::class, + ['id' => 2011, + 'id_notice' => 1011, + 'id_origine' => 42, + 'id_bib' => 31, + 'id_int_bib' => 31]); + + + Class_HttpClientFactory::forTest(); + + Class_HttpClientFactory::getInstance() + ->getLastHttpClient() + ->addPostRequestWithResponse('https://mypmb.com:443/ws', + '{"method":"pmbesItems_fetch_notice_items","params":[42],"id":1}', + ''); + + $this->dispatch('/opac/noticeajax/resources/id/1011'); + } + + + /** @test */ + public function httpClientShouldHaveUserAdminAuthParams() + { + $this->assertEquals('admin', Class_HttpClientFactory::getInstance() + ->getLastHttpClient() + ->getAuthUser()); + } + + + /** @test */ + public function httpClientShouldHavePassPMB455AuthParams() + { + $this->assertEquals('PMB455', Class_HttpClientFactory::getInstance() + ->getLastHttpClient() + ->getAuthPassword()); + } +}