Commit 51fac9ee authored by David Maus's avatar David Maus
Browse files

Authenticate via LBS4 Authentication webservice

* src/HAB/Pica/Auth/LBSAuthentication.php: New class. Authenticate via
  LBS4 Authentication webservice.
parent 116b2096
<?php
/**
* This file is part of PicaAuth.
*
* PicaAuth is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PicaAuth 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PicaAuth. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2015 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Pica\Auth;
use RuntimeException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
/**
* Authenticate user against the LBS4 Authentication webservice.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2015 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
class LBSAuthentication implements AuthenticationInterface
{
/**
* Service URL.
*
* @var string
*/
private $serviceUrl;
/**
* Catalog number.
*
* @var integer
*/
private $catalogNumber;
/**
* User number for connecting to LBS4.
*
* @var integer
*/
private $lbsUserNumber;
/**
* HTTP client.
*
* @var Client
*/
private $client;
/**
* Constructor.
*
* @param string $serviceUrl
* @param integer $catalogNumber
* @param integer $lbsUserNumber
* @return void
*/
public function __construct ($serviceUrl, $catalogNumber, $lbsUserNumber)
{
$this->serviceUrl = $serviceUrl;
$this->catalogNumber = $catalogNumber;
$this->lbsUserNumber = $lbsUserNumber;
}
/**
* {@inheritDoc}
*/
public function authenticate ($username, $password)
{
$query = array(
'UK' => $username,
'PW' => $password,
'UN' => $this->lbsUserNumber,
'FNO' => $this->catalogNumber,
'LNG' => 'EN'
);
try {
$response = $this->getClient()->get($this->serviceUrl, array('query' => $query));
} catch (RequestException $e) {
throw new RuntimeException(null, -1, $e);
}
$attributes = $this->parseResponseBody($response->getBody());
return $attributes;
}
/**
* Return HTTP client.
*
* @return Client
*/
public function getClient ()
{
if ($this->client === null) {
$this->client = new Client();
}
return $this->client;
}
/**
* Parse response body and user return attributes.
*
* Returns false on authentication failure.
*
* @param string $body
* @return array|false
*/
private function parseResponseBody ($body)
{
$response = @simplexml_load_string($body);
if ($response === false || $response->getName() !== 'AuthenticationResponse' || (string)$response->status !== 'OK') {
return false;
}
$attributes = array();
foreach ($response as $attribute) {
$attributes[$attribute->getName()] = (string)$attribute;
}
return $attributes;
}
}
\ No newline at end of file
HTTP/1.1 200 OK
Date: Wed, 06 May 2015 09:43:16 GMT
Server: Apache/2.2.13 (Unix) mod_jk/1.2.28 mod_ssl/2.2.13 OpenSSL/0.9.8k PHP/5.2.11
Content-Length: 168
Content-Type: application/xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><AuthenticationResponse><status>NOK</status><error>Wrong password and/or number.</error></AuthenticationResponse>
\ No newline at end of file
HTTP/1.1 200 OK
Date: Wed, 06 May 2015 09:42:33 GMT
Server: Apache/2.2.13 (Unix) mod_jk/1.2.28 mod_ssl/2.2.13 OpenSSL/0.9.8k PHP/5.2.11
Content-Length: 232
Content-Type: application/xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><AuthenticationResponse><status>OK</status><sessionID>c3ac4981-eb02-46f5-b092-d7261d08386f</sessionID><userKey>username</userKey><userGroup>20</userGroup></AuthenticationResponse>
\ No newline at end of file
<?php
/**
* This file is part of PicaAuth.
*
* PicaAuth is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PicaAuth 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PicaAuth. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2015 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Pica\Auth;
use GuzzleHttp\Subscriber\Mock;
use PHPUnit_Framework_TestCase as TestCase;
/**
* Unit tests for the LBSAuthenitcation service module.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2015 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
class LBSAuthenticationTest extends TestCase
{
/**
* @expectedException RuntimeException
*/
public function testRuntimeExceptionOnRemoteError ()
{
$service = new LBSAuthentication('invalid://example.org/', 0, 0);
$response = new Mock(array('HTTP/1.1 500 Internal Server Error'));
$service->getClient()->getEmitter()->attach($response);
$service->authenticate('username', 'password');
}
public function testAuthenticationFailure ()
{
$service = new LBSAuthentication('invalid://example.org/', 0, 0);
$response = new Mock(array(file_get_contents(APP_TESTDIR . '/unit-tests/data/lbs4authentication.error.response')));
$service->getClient()->getEmitter()->attach($response);
$attributes = $service->authenticate('username', 'password');
$this->assertFalse($attributes);
}
public function testAuthenticationSuccess ()
{
$service = new LBSAuthentication('invalid://example.org/', 0, 0);
$response = new Mock(array(file_get_contents(APP_TESTDIR . '/unit-tests/data/lbs4authentication.success.response')));
$service->getClient()->getEmitter()->attach($response);
$attributes = $service->authenticate('username', 'password');
$this->assertInternalType('array', $attributes);
$this->assertNotEmpty($attributes);
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment