001    /**
002     * 
003     * $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
004     *
005     * This file is part of ** M y C o R e **
006     * Visit our homepage at http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, normally in the file license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     *
023     **/
024    
025    package org.mycore.user.authentication;
026    
027    import java.io.BufferedReader;
028    import java.io.InputStreamReader;
029    import java.io.PrintStream;
030    import java.net.InetAddress;
031    import java.net.Socket;
032    import java.net.UnknownHostException;
033    
034    import org.mycore.common.MCRConfiguration;
035    import org.mycore.common.MCRConfigurationException;
036    import org.mycore.common.MCRException;
037    
038    /**
039     * Authenticates a user by checking the password against a
040     * university library server supporting the Simple Library Network
041     * Protocol (SLNP), for example ALEPH and SISIS systems.
042     * Configuration properties are:
043     * 
044     * MCR.UserAuthenticator.[ID].Host
045     *   the host name of the SLNP server
046     * MCR.UserAuthenticator.[ID].Port
047     *   the SLNP port
048     */
049    public class MCRSLNPAuthenticator {
050        /** The address of the SLNP server */
051        private InetAddress addr;
052    
053        /** The port number of the SLNP port on that server */
054        private int port;
055    
056        /** This pattern identifies the password in the SLNP response */
057        private static String pinPattern = "601 OpacPin:";
058    
059        public void init(String ID) {
060            MCRConfiguration config = MCRConfiguration.instance();
061            String prefix = "MCR.UserAuthenticator." + ID;
062    
063            port = config.getInt(prefix + ".Port");
064            String host = config.getString(prefix + ".Host");
065            try {
066                addr = InetAddress.getByName(host);
067            } catch (UnknownHostException ex) {
068                String msg = "SLNP host unknown for " + prefix;
069                throw new MCRConfigurationException(msg, ex);
070            }
071        }
072    
073        public boolean authenticate(String username, String password) {
074            Socket socket = null;
075            boolean authenticated = false;
076    
077            try {
078                socket = new Socket(addr, port);
079    
080                PrintStream ps = new PrintStream(socket.getOutputStream());
081                ps.println("SLNPAlleBenutzerDaten");
082                ps.println("BenutzerNummer:" + username);
083                ps.println("SLNPEndCommand");
084                ps.flush();
085    
086                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
087    
088                for (int i = 0; i < 20; i++) {
089                    String line = br.readLine();
090                    if ((line == null) || line.startsWith("510") || line.startsWith("250")) {
091                        break;
092                    }
093                    if (line.startsWith(pinPattern)) {
094                        authenticated = line.equals(pinPattern + password);
095                        break;
096                    }
097                }
098            } catch (Exception ex) {
099                String msg = "Exception while communicating with SLNP Server";
100                throw new MCRException(msg, ex);
101            } finally {
102                if (socket != null)
103                    try {
104                        socket.close();
105                    } catch (Exception ignored) {
106                    }
107            }
108            return authenticated;
109        }
110    
111        /** A small test application, modify source code to test class */
112        public static void main(String[] args) throws Exception {
113            MCRSLNPAuthenticator auth = new MCRSLNPAuthenticator();
114            auth.addr = InetAddress.getByName("aleph420.bibl.uni-essen.de");
115            auth.port = 5441;
116            System.out.println("Authenticated: " + auth.authenticate("UEGSTest", "Wrong"));
117        }
118    }