Skip to content
This repository has been archived by the owner on Jul 21, 2022. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/issue252' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
xmorave2 committed Jun 22, 2021
2 parents 28d9e23 + bc8d7c5 commit 9b0e5ae
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 22 deletions.
1 change: 1 addition & 0 deletions languages/cs.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,7 @@ institution_api_token_endpoint = Token endpoint pro OAuth2
institution_api_client_id = Client ID pro OAuth2
institution_api_client_secret = Client secret pro OAuth2
institution_api_grant_type = Grant type pro OAuth2
institution_token_basic_auth = Použít basic authentication pro získání Oauth2 tokenu
Not published = Nezveřejněno
Page title = Název stránky
Last modified = Naposled upraveno
Expand Down
1 change: 1 addition & 0 deletions languages/en.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,7 @@ institution_api_token_endpoint = Token endpoint for OAuth2 authentication
institution_api_client_id = Client ID for OAuth2 authentication
institution_api_client_secret = Client secret for OAuth2 authentication
institution_api_grant_type = Grant type for OAuth2 authentication
institution_token_basic_auth = Použít basic authentication pro získání Oauth2 tokenu
Not published = Not published
Page title = Page title
Last modified = Last modified
Expand Down
14 changes: 14 additions & 0 deletions local/knihovny/config/vufind/xcncip2_template.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ hideHoldLinks = off
ils_type = ils_system_name
pick_up_location = off

tokenEndpoint = institution_api_token_endpoint
clientId = institution_api_client_id
clientSecret = institution_api_client_secret
tokenBasicAuth = institution_token_basic_auth

[IdResolver]
prefix = source

Expand Down Expand Up @@ -48,6 +53,11 @@ Catalog[hideHoldLinks] = checkbox
Catalog[pick_up_location] = checkbox
Catalog[ils_type] = select

Catalog[tokenEndpoint] = text
Catalog[clientId] = text
Catalog[clientSecret] = text
Catalog[tokenBasicAuth] = checkbox

; Define which settings are about to be shown as advanced
; These will be shown on an extra click
advanced[] = Catalog:timeout
Expand All @@ -69,6 +79,10 @@ optional[] = Catalog:cannotUseLUIS
optional[] = Catalog:hideHoldLinks
optional[] = Catalog:pick_up_location
optional[] = Catalog:ils_type
optional[] = Catalog:tokenEndpoint
optional[] = Catalog:clientId
optional[] = Catalog:clientSecret
optional[] = Catalog:tokenBasicAuth

; Define hidden configurations which cannot be modified by library admin
hidden[] = IdResolver
Expand Down
4 changes: 2 additions & 2 deletions module/CPK/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@
'invokables' => [
'dummy' => 'CPK\ILS\Driver\Dummy',
'xcncip2' => 'CPK\ILS\Driver\XCNCIP2',
'xcncip2v2' => 'CPK\ILS\Driver\XCNCIP2V2'
],
'factories' => array(
'multibackend' => 'CPK\ILS\Driver\Factory::getMultiBackend',
'aleph' => 'CPK\ILS\Driver\Factory::getAleph',
'koharest' => 'CPK\ILS\Driver\Factory::getKohaRest',
'xcncip2v2' => 'CPK\ILS\Driver\Factory::getXcncip2V2',
), /* factories */
], /* ils_driver */
'autocomplete' => [
Expand Down Expand Up @@ -244,7 +244,7 @@
'CPK\Mailer' => 'CPK\Mailer\Factory::createService',
'VuFind\ILSHoldLogic' => 'CPK\ILS\Logic\Factory::getFlatHolds',
'Mzk\ZiskejApi\Api' => 'CPK\ZiskejApiFactory',
'CPK\KohaOAUTH2Service' => 'CPK\Auth\Factory::getKohaOAUTH2Service',
'CPK\Auth\Oauth2Service' => 'CPK\Auth\Factory::getOauth2Service',
\CPK\Ziskej\ZiskejEdd::class => \CPK\Ziskej\ZiskejEddFactory::class,
\CPK\Ziskej\ZiskejMvs::class => \CPK\Ziskej\ZiskejMvsFactory::class,
), // Exceptions throwing system
Expand Down
6 changes: 3 additions & 3 deletions module/CPK/src/CPK/Auth/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ public static function getILSAuthenticator(ServiceManager $sm)
*
* @param ServiceManager $sm
*
* @return KohaRestService
* @return Oauth2Service
* @throws \Exception
*/
public static function getKohaOAUTH2Service(ServiceManager $sm)
public static function getOauth2Service(ServiceManager $sm)
{
return new KohaRestService(
return new Oauth2Service(
$sm->get('VuFind\CacheManager'),
$sm->get('VuFind\Config')->get('config')
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

use VuFind\Exception\ILS as ILSException;

class KohaRestService implements \VuFindHttp\HttpServiceAwareInterface,
class Oauth2Service implements \VuFindHttp\HttpServiceAwareInterface,
\Zend\Log\LoggerAwareInterface
{
use \VuFindHttp\HttpServiceAwareTrait;
Expand Down Expand Up @@ -60,6 +60,7 @@ class KohaRestService implements \VuFindHttp\HttpServiceAwareInterface,
*/
protected $token;


/**
* Constructor
*
Expand Down Expand Up @@ -104,19 +105,28 @@ protected function requestNewOAUTH2Token()
{
$tokenEndpoint = $this->config['Catalog']['tokenEndpoint'];
$client = $this->createHttpClient($tokenEndpoint);
$postFields = [
'grant_type' => $this->config['Catalog']['grantType']
?? 'client_credentials',
];
if (isset($this->config['Catalog']['tokenBasicAuth'])
&& $this->config['Catalog']['tokenBasicAuth']) {
$client->setAuth(
$this->config['Catalog']['clientId'],
$this->config['Catalog']['clientSecret']
);
} else {
$postFields['client_id'] = $this->config['Catalog']['clientId'];
$postFields['client_secret'] = $this->config['Catalog']['clientSecret'];
}

$adapter = new \Zend\Http\Client\Adapter\Curl();
$client->setAdapter($adapter);
$adapter->setOptions([
'curloptions' => [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => [
'client_id' => $this->config['Catalog']['clientId'],
'client_secret' => $this->config['Catalog']['clientSecret'],
'grant_type' => $this->config['Catalog']['grantType']
?? 'client_credentials'
]
CURLOPT_POSTFIELDS => $postFields,
]
]);

Expand Down Expand Up @@ -185,7 +195,7 @@ public function invalidateToken()
}

protected function getCacheKey() {
return "KohaREST_token_" . $this->source;
return "Oauth2_token_" . $this->source;
}

protected function setToCache($value) {
Expand Down
14 changes: 13 additions & 1 deletion module/CPK/src/CPK/ILS/Driver/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,20 @@ public static function getKohaRest(ServiceManager $sm)

return new KohaRest(
$sl->get('VuFind\DateConverter'),
$sl->get('CPK\KohaOAUTH2Service')
$sl->get('CPK\Auth\Oauth2Service')
);
}

/**
* Factory for XCNCIP2V2 driver.
*
* @param ServiceManager $sm Service manager.
*
* @return XCNCIP2V2
*/
public static function getXcncip2V2(ServiceManager $sm)
{
$sl = $sm->getServiceLocator();
return new XCNCIP2V2($sl->get('CPK\Auth\Oauth2Service'));
}
}
8 changes: 4 additions & 4 deletions module/CPK/src/CPK/ILS/Driver/KohaRest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
namespace CPK\ILS\Driver;

use CPK\Auth\KohaRestService;
use CPK\Auth\Oauth2Service;
use VuFind\ILS\Driver\AbstractBase;
use CPK\ILS\Logic\KohaRestNormalizer;
use VuFind\Exception\Date as DateException;
Expand Down Expand Up @@ -125,10 +125,10 @@ class KohaRest extends AbstractBase implements \Zend\Log\LoggerAwareInterface,
/**
* Constructor
*
* @param DateConverter $dateConverter Date converter
* @param KohaRestService $kohaRestService Koha API authentication service
* @param DateConverter $dateConverter Date converter
* @param Oauth2Service $kohaRestService Koha API authentication service
*/
public function __construct(DateConverter $dateConverter, KohaRestService $kohaRestService)
public function __construct(DateConverter $dateConverter, Oauth2Service $kohaRestService)
{
$this->dateConverter = $dateConverter;
$this->kohaRestService = $kohaRestService;
Expand Down
35 changes: 31 additions & 4 deletions module/CPK/src/CPK/ILS/Driver/XCNCIP2V2.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@

namespace CPK\ILS\Driver;

use CPK\Auth\Oauth2Service;
use CPK\ILS\Logic\XmlTransformation\JsonXML;
use CPK\ILS\Logic\XmlTransformation\JsonXMLException;
use CPK\ILS\Logic\XmlTransformation\NCIPDenormalizerRouter;
use CPK\ILS\Logic\XmlTransformation\NCIPNormalizer;
use CPK\ILS\Logic\XmlTransformation\NCIPDenormalizer;
use CPK\ILS\Logic\XmlTransformation\NCIPNormalizerRouter;
use VuFind\Date\Converter as DateConverter;
use VuFind\Exception\ILS as ILSException;
use VuFind\I18n\Translator\TranslatorAwareInterface;
use VuFind\ILS\Driver\AbstractBase;
Expand Down Expand Up @@ -124,6 +126,11 @@ class XCNCIP2V2 extends AbstractBase implements HttpServiceAwareInterface, Trans
*/
protected $translator = false;

/**
* @var \CPK\Auth\Oauth2Service
*/
protected $oauth2Service;

/**
* @var LoggerInterface
*/
Expand All @@ -134,6 +141,16 @@ class XCNCIP2V2 extends AbstractBase implements HttpServiceAwareInterface, Trans
'ns1' => 'http://www.niso.org/2008/ncip',
);

/**
* Constructor
*
* @param Oauth2Service $oauth2Service Oauth2 authentication service
*/
public function __construct(Oauth2Service $oauth2Service)
{
$this->oauth2Service = $oauth2Service;
}

/**
* Set the HTTP service to be used for HTTP requests.
*
Expand Down Expand Up @@ -196,6 +213,12 @@ public function init()
if (isset($this->config['Availability']['source']))
$this->source = $this->config['Availability']['source'];

$clientId = $this->config['Catalog']['clientId'] ?? '';
if (!empty($clientId)) {
$this->oauth2Service->setConfig($this->config);
$this->oauth2Service->setSource($this->source);
}

$this->requests = new NCIPRequests($this->config);
}

Expand Down Expand Up @@ -254,12 +277,17 @@ protected function sendRequest($xml)
{
$this->denormalizeRequest($xml);

$apiUrl = $this->config['Catalog']['url'];
$clientId = $this->config['Catalog']['clientId'] ?? '';
if (!empty($clientId)) {
$client = $this->oauth2Service->createClient($apiUrl);
} else {
$client = $this->httpService->createClient($apiUrl);
}
// $client->setAdapter(new \Zend\Http\Client\Adapter\Socket());

// Make the NCIP request:
try {
$this->httpService->setDefaultAdapter(new \Zend\Http\Client\Adapter\Socket());
$client = $this->httpService->createClient($this->config['Catalog']['url']);
//$client->setRawBody($jsonXML->toXmlString());
$client->setRawBody($xml);
$client->setEncType('application/xml; "charset=utf-8"');
$client->setMethod('POST');
Expand All @@ -273,7 +301,6 @@ protected function sendRequest($xml)
));

if ((!empty($this->config['Catalog']['username'])) && (!empty($this->config['Catalog']['password']))) {

$user = $this->config['Catalog']['username'];
$password = $this->config['Catalog']['password'];
$client->setAuth($user, $password);
Expand Down

0 comments on commit 9b0e5ae

Please sign in to comment.