Commit 79c5af28 authored by David Maus's avatar David Maus
Browse files

Authenticate via LOAN3 web interface

* src/HAB/Pica/Auth/LOAN3WebAuthentication.php: New
  class. Authenticate via LOAN3 web interface.
parent 51fac9ee
<?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 a LOAN3 web interface.
*
* @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 LOAN3WebAuthentication implements AuthenticationInterface
{
/**
* Service URL.
*
* @var string
*/
private $serviceUrl;
/**
* HTTP client.
*
* @var Client
*/
private $client;
/**
* Constructor.
*
* @param string $serviceUrl
* @return void
*/
public function __construct ($serviceUrl)
{
$this->serviceUrl = $serviceUrl;
}
/**
* {@inheritDoc}
*/
public function authenticate ($username, $password)
{
$query = array(
'BOR_U' => $username,
'BOR_PW' => $password,
'ACT' => 'UI_DATA',
);
try {
$response = $this->getClient()->post($this->serviceUrl, array('body' => $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)
{
$attributes = array();
preg_match_all('@<input([^>]+)>@i', $body, $matches);
if (!empty($matches)) {
foreach ($matches[1] as $match) {
if ($this->getAttributeValue('type', $match) === 'hidden') {
$name = $this->getAttributeValue('name', $match);
$value = $this->getAttributeValue('value', $match);
if ($name) {
$attributes[$name] = $value;
}
}
}
}
if (array_key_exists('STATUS', $attributes) && $attributes['STATUS'] === 'HML_OK') {
return $attributes;
}
return false;
}
/**
* Return value of attribute in attribute list.
*
* @param string $name
* @param string $attrlist
* @return string|null
*/
private function getAttributeValue ($name, $attrlist)
{
$regex = sprintf('@\b%s=["\'](?<value>[^"\']*)@i', preg_quote($name));
if (preg_match($regex, $attrlist, $match)) {
return $match['value'];
}
return null;
}
}
\ No newline at end of file
HTTP/1.1 200 OK
Date: Wed, 06 May 2015 12:12:11 GMT
Server: opc4/2.8.4.0-vzg8-dbs1
Content-Length: 18777
X-PSI-Class: loan
X-PSI-Context: USERINFO_LOGIN
Content-Type: text/html; charset=UTF-8
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
X-Pad: avoid browser bug
<HTML>
<HEAD>
<TITLE>OPC4 - borrower/identification</TITLE>
<BASE HREF="https://opac.lbs-braunschweig.gbv.de:443/DB=2/SET=12/TTL=1/">
<link rel="stylesheet" media="print" href="https://opac.lbs-braunschweig.gbv.de:443/FORCETT=HTML/STYLESHEET_P">
<link rel="stylesheet" href="https://opac.lbs-braunschweig.gbv.de:443/FORCETT=HTML/STYLESHEET">
<link rel="shortcut icon" type="image/vnd.microsoft.icon" href="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/favicons//default.ico">
<link rel="icon" type="image/vnd.microsoft.icon" href="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/favicons//default.ico">
<link rel="search" type="application/opensearchdescription+xml" title="OPC4" href="https://opac.lbs-braunschweig.gbv.de:443/DB=2/SET=12/TTL=1/XML=1.0/SEARCHPLUGIN?NAME=OPC4&FAVICON=https%3A%2F%2Fopac.lbs-braunschweig.gbv.de%3A443%2Fimg_psi%2F2.0%2Ffavicons%2F%2Fdefault.ico">
<META http-equiv="Refresh"
content="600;
URL=TIMEOUT?CWIN=Y&REFERER=&REFRTIME=-1&REFRURL=EXIT%3FREFRTIME%3D-1%26DEST%3D%252F">
</HEAD>
<BODY marginwidth="0" marginheight="0" background="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/bg.gif">
<table summary="Main layout" width="100%"
cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="lng" height="20" width="120"><center><table summary="Language switch"
cellpadding="0" cellspacing="0" border="0">
<tr><td width="4"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="4"
height="1"
border="0"></td>
<td><img
alt="Deutsch"
title="Deutsch"
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/flags/du-.gif"
width="16" height="10" border="0"></td>
<td width="4"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="4"
height="1"
border="0"></td>
<td><img
alt="Englisch"
title="Englisch"
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/flags/en.gif"
width="16" height="10" border="0"></td>
<td width="4"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="4"
height="1"
border="0"></td>
</tr>
</table></center></td>
<td bgcolor="#cccccc" width="1"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/white.gif"
width="1"
height="20"
border="0"></td>
<td class="nav" width="100%"><table summary="Navigation bar" width="100%"
border="0" cellpadding="0" cellspacing="0">
<tr><td width="35"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="35"
height="20"
border="0"></td>
<td class="copy" align="right" width="100%" nowrap><a
target="_blank"
href="http://oclcpica.org/?id=2&ln=de"
title="&copy;&nbsp;1998-2015&nbsp;OCLC&nbsp;PICA opc4 v2.8.4.0-vzg8-dbs1"
class="copy">&copy;</a></td>
<td width="10"
bgcolor="#cccccc"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="10"
height="1"
border="0"></td>
</tr>
</table></td>
</tr>
<tr class="logo"><td height="1"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/white.gif"
width="120"
height="1"
border="0"></td></tr>
<tr class="logo">
<td height="90" align="center" bgcolor="#eeeeee"><img
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/logos/lbs-hab.gif"
alt=""
height="90" width="120" border="0"></td>
<td bgcolor="#eeeeee"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/white.gif"
width="1"
height="90"
border="0"></td>
<td class="cmd"><table summary="safety warning"
cellpadding="0" cellspacing="0" border="0" width="100%">
<tr><td><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="35"
height="90"
border="0"></td>
<td width="100%" align="left"><strong class="alert">Zum Schutz Ihrer Daten sollten Sie dieses Fenster nach Benutzung schlie&szlig;en!</strong></td>
<td class="h2" align="right"><a href="Javascript:self.print();">Seite&nbsp;drucken</a>&nbsp;&nbsp;&nbsp;</td>
</tr>
</table></td>
<!-- /td moved one line up [MM] -->
</tr>
<tr class="logo"><td height="1"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/white.gif"
width="120"
height="1"
border="0"></td></tr>
<tr class="logo">
<td height="20" bgcolor="#577FBB"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td>
<td bgcolor="#577FBB"><img
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/white.gif" alt="" width="1" height="20"></td>
<td colspan="1"
bgcolor="#577FBB" align="center" nowrap><center><table summary="tab bar - loan server - default tab bar"
cellpadding="0" cellspacing="0" border="0">
<tr><td bgcolor="#577FBB"
height="1" colspan="5"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td><td bgcolor="#577FBB"
height="1" colspan="5"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td><td bgcolor="#577FBB"
height="1" colspan="5"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td><td bgcolor="#577FBB"
height="1" colspan="5"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td><td bgcolor="#577FBB"
height="1" colspan="5"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td></tr>
<tr> <td width="1"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td>
<td class="tabbar" nowrap><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/tableft.gif"
width="11" height="18"></td>
<td class="tab0" height="18"><a class="tab0" href="/loan/DB=2/SET=12/TTL=1/USERINFO?ACT=UI_DATA&BOR_U=wrong&BOR_PW_ENC=XXX">Benutzerdaten</a></td>
<td class="tabbar" nowrap><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/tabright.gif"
width="11" height="18"></td>
<td width="1"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td> <td width="1"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td>
<td class="tabbar" nowrap><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/tableft.gif"
width="11" height="18"></td>
<td class="tab0" height="18"><a class="tab0" href="/loan/DB=2/SET=12/TTL=1/USERINFO?ACT=UI_LOL&BOR_U=wrong&BOR_PW_ENC=XXX">Entleihungen</a></td>
<td class="tabbar" nowrap><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/tabright.gif"
width="11" height="18"></td>
<td width="1"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td> <td width="1"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td>
<td class="tabbar" nowrap><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/tableft.gif"
width="11" height="18"></td>
<td class="tab0" height="18"><a class="tab0" href="/loan/DB=2/SET=12/TTL=1/USERINFO?ACT=UI_LOR&BOR_U=wrong&BOR_PW_ENC=XXX">Vormerkungen</a></td>
<td class="tabbar" nowrap><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/tabright.gif"
width="11" height="18"></td>
<td width="1"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td> <td width="1"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td>
<td class="tabbar" nowrap><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/tableft.gif"
width="11" height="18"></td>
<td class="tab0" height="18"><a class="tab0" href="/loan/DB=2/SET=12/TTL=1/USERINFO?ACT=UI_LOC&BOR_U=wrong&BOR_PW_ENC=XXX">Kosten</a></td>
<td class="tabbar" nowrap><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/tabright.gif"
width="11" height="18"></td>
<td width="1"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td> <td width="1"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td>
<td class="tabbar" nowrap><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/tableft.gif"
width="11" height="18"></td>
<td class="tab0" height="18"><a class="tab0" href="/loan/DB=2/SET=12/TTL=1/USERINFO?ACT=UI_CHUPW&BOR_U=wrong&BOR_PW_ENC=XXX">Passwort</a></td>
<td class="tabbar" nowrap><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/tabright.gif"
width="11" height="18"></td>
<td width="1"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td>
</tr>
<tr>
<td bgcolor="#577FBB"
height="1" colspan="5"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td>
<td bgcolor="#577FBB"
height="1" colspan="5"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td>
<td bgcolor="#577FBB"
height="1" colspan="5"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td>
<td bgcolor="#577FBB"
height="1" colspan="5"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td>
<td bgcolor="#577FBB"
height="1" colspan="5"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td>
<td><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td>
</tr>
</table></center></td>
</tr>
<tr class="logo"><td height="1"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/white.gif"
width="120"
height="1"
border="0"></td></tr>
<tr>
<td class="mnu" valign="top"><table summary="Menu layout" width="100%"
border="0" cellpadding="0" cellspacing="0">
<SCRIPT type="text/javascript" language="javascript">
document.write('<TR><TD class="mnu"><p class="mnu"><a class="mnu" href="javascript:window.close()">Schlie&szlig;en</A></TD></TR>');
</SCRIPT>
<NOSCRIPT>
</NOSCRIPT>
<tr><td><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="300"
border="0"></td></tr>
</table></td>
<td><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></td>
<td colspan="1"
class="cnt" valign="top"><form method="post" action="https://opac.lbs-braunschweig.gbv.de:443/loan/DB=2/SET=12/TTL=1/USERINFO">
<input type="hidden" name="ACT" value="UI_DATA">
<input type="hidden" name="HOST_NAME" value="">
<input type="hidden" name="HOST_PORT" value="">
<input type="hidden" name="HOST_SCRIPT" value="">
<input type="hidden" name="LOGIN" value="KNOWNUSER">
<input type="hidden" name="STATUS" value="HML_REJECT">
<table summary="Identification header" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="h2" valign="top"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="16"
border="0"></td>
</tr>
<tr>
<td valign="top"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="12"
height="1"
border="0"><img alt="*" src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/h2.gif" width="19" height="12" border="0"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="4"
height="1"
border="0"></td>
<td valign="top" width="100%"><strong class="h2">Benutzer</strong><span class="subtitle">&nbsp;&nbsp;Anmeldung</span></td>
<td><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="10"
height="1"
border="0"></td>
</tr>
<tr>
<td class="h2" valign="top"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="7"
border="0"></td>
</tr>
</table>
<table summary="body separator" width="100%" cellpadding="0" cellspacing="0" border="0">
<TR><TD><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="10"
border="0"></TD></TR>
<TR><TD class="bodysep" colspan="2"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="1"
border="0"></TD></TR>
<TR><TD><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="10"
border="0"></TD></TR>
</table>
<table summary="Message block header" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td valign="top" width="35" nowrap><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="12"
height="1"
border="0"><img
alt="error" src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/error.gif" width="12" height="15" border="0"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="11"
height="1"
border="0"></td>
<td valign="top" width="100%" nowrap><strong class="alert">Nummer und/oder Passwort sind nicht korrekt.</strong></td>
<td><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="10"
height="1"
border="0"></td>
</tr>
<tr>
<td class="h2" valign="top"><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"
width="1"
height="7"
border="0"></td>
</tr>
</table>
<table summary="body separator" width="100%" cellpadding="0" cellspacing="0" border="0">
<TR><TD><img
alt=""
src="https://opac.lbs-braunschweig.gbv.de:443/img_psi/2.0/gui/empty.gif"