Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
12.50% |
5 / 40 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
SIP2 | |
12.50% |
5 / 40 |
|
33.33% |
1 / 3 |
92.06 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
authenticate | |
12.12% |
4 / 33 |
|
0.00% |
0 / 1 |
63.97 | |||
processSIP2User | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * SIP2 authentication module. |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Authentication |
25 | * @author Franck Borel <franck.borel@gbv.de> |
26 | * @author Demian Katz <demian.katz@villanova.edu> |
27 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
28 | * @link https://vufind.org/wiki/development:plugins:authentication_handlers Wiki |
29 | */ |
30 | |
31 | namespace VuFind\Auth; |
32 | |
33 | use VuFind\Db\Entity\UserEntityInterface; |
34 | use VuFind\Exception\Auth as AuthException; |
35 | |
36 | /** |
37 | * SIP2 authentication module. |
38 | * |
39 | * @category VuFind |
40 | * @package Authentication |
41 | * @author Franck Borel <franck.borel@gbv.de> |
42 | * @author Demian Katz <demian.katz@villanova.edu> |
43 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
44 | * @link https://vufind.org/wiki/development:plugins:authentication_handlers Wiki |
45 | */ |
46 | class SIP2 extends AbstractBase |
47 | { |
48 | /** |
49 | * Constructor |
50 | * |
51 | * @param ILSAuthenticator $ilsAuthenticator ILS authenticator |
52 | */ |
53 | public function __construct(protected ILSAuthenticator $ilsAuthenticator) |
54 | { |
55 | } |
56 | |
57 | /** |
58 | * Attempt to authenticate the current user. Throws exception if login fails. |
59 | * |
60 | * @param \Laminas\Http\PhpEnvironment\Request $request Request object containing |
61 | * account credentials. |
62 | * |
63 | * @throws AuthException |
64 | * @return UserEntityInterface Object representing logged-in user. |
65 | */ |
66 | public function authenticate($request) |
67 | { |
68 | $username = trim($request->getPost()->get('username', '')); |
69 | $password = trim($request->getPost()->get('password', '')); |
70 | if ($username == '' || $password == '') { |
71 | throw new AuthException('authentication_error_blank'); |
72 | } |
73 | |
74 | // Attempt SIP2 Authentication |
75 | $mysip = new \sip2(); |
76 | $config = $this->getConfig(); |
77 | if (isset($config->SIP2)) { |
78 | $mysip->hostname = $config->SIP2->host; |
79 | $mysip->port = $config->SIP2->port; |
80 | } |
81 | |
82 | if (!$mysip->connect()) { |
83 | throw new AuthException('authentication_error_technical'); |
84 | } |
85 | |
86 | //send selfcheck status message |
87 | $in = $mysip->msgSCStatus(); |
88 | $msg_result = $mysip->get_message($in); |
89 | |
90 | // Make sure the response is 98 as expected |
91 | if (!preg_match('/^98/', $msg_result)) { |
92 | $mysip->disconnect(); |
93 | throw new AuthException('authentication_error_technical'); |
94 | } |
95 | $result = $mysip->parseACSStatusResponse($msg_result); |
96 | |
97 | // Use result to populate SIP2 settings |
98 | $mysip->AO = $result['variable']['AO'][0]; |
99 | $mysip->AN = $result['variable']['AN'][0]; |
100 | |
101 | $mysip->patron = $username; |
102 | $mysip->patronpwd = $password; |
103 | |
104 | $in = $mysip->msgPatronStatusRequest(); |
105 | $msg_result = $mysip->get_message($in); |
106 | |
107 | // Make sure the response is 24 as expected |
108 | if (!preg_match('/^24/', $msg_result)) { |
109 | $mysip->disconnect(); |
110 | throw new AuthException('authentication_error_technical'); |
111 | } |
112 | |
113 | $result = $mysip->parsePatronStatusResponse($msg_result); |
114 | $mysip->disconnect(); |
115 | if ( |
116 | ($result['variable']['BL'][0] == 'Y') |
117 | and ($result['variable']['CQ'][0] == 'Y') |
118 | ) { |
119 | // Success!!! |
120 | $user = $this->processSIP2User($result, $username, $password); |
121 | } else { |
122 | throw new AuthException('authentication_error_invalid'); |
123 | } |
124 | |
125 | return $user; |
126 | } |
127 | |
128 | /** |
129 | * Process SIP2 User Account |
130 | * |
131 | * Based on code by Bob Wicksall <bwicksall@pls-net.org>. |
132 | * |
133 | * @param array $info An array of user information |
134 | * @param string $username The user's ILS username |
135 | * @param string $password The user's ILS password |
136 | * |
137 | * @throws AuthException |
138 | * @return UserEntityInterface Processed User object. |
139 | */ |
140 | protected function processSIP2User($info, $username, $password) |
141 | { |
142 | $user = $this->getOrCreateUserByUsername($info['variable']['AA'][0]); |
143 | |
144 | // This could potentially be different depending on the ILS. Name could be |
145 | // Bob Wicksall or Wicksall, Bob. This is currently assuming Wicksall, Bob |
146 | $ae = $info['variable']['AE'][0]; |
147 | $user->setFirstname(trim(substr($ae, 1 + strripos($ae, ',')))); |
148 | $user->setLastname(trim(substr($ae, 0, strripos($ae, ',')))); |
149 | // I'm inserting the sip username and password since the ILS is the source. |
150 | // Should revisit this. |
151 | $this->ilsAuthenticator->saveUserCatalogCredentials($user, $username, $password); |
152 | return $user; |
153 | } |
154 | } |