001    /*
002      Copyright (C) 2001-2002 Laurent Martelli <laurent@aopsys.com>
003      
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.aspects.authentication;
019    
020    import org.objectweb.jac.util.WrappedThrowableException;
021    import java.io.File;
022    import java.io.FileReader;
023    import java.io.StreamTokenizer;
024    import java.util.HashMap;
025    
026    /**
027     * This Authenticator ask for a username and password and compares
028     * them to declared ones stored in a file. The number of allowed
029     * attempts to enter a valid (username,password) is configurable. It
030     * needs a DisplayContext attribute in order to be able to interact
031     * with the user.  */
032    
033    public class FilePasswordAuthenticator extends PasswordAuthenticator {   
034    
035        File passwordFile;
036    
037        /**
038         * The constructor.
039         *
040         * @param retries the number of time the authenticator will ask the
041         * user to retype wrong information
042         * @param passwordFilename the filename where the users are stored
043         */
044        public FilePasswordAuthenticator(String retries, String passwordFilename) {
045            super(Integer.parseInt(retries));
046            passwordFile = new File(passwordFilename);
047        }
048    
049        HashMap passwords;
050    
051        boolean checkPassword(String username, String password) {
052            if (passwords==null) {
053                // read the password file
054                passwords = new HashMap();
055                try {
056                    StreamTokenizer tokens = new StreamTokenizer(new FileReader(passwordFile));
057                    tokens.resetSyntax();
058                    tokens.wordChars('\000','\377');
059                    tokens.whitespaceChars('\n','\n');
060                    tokens.whitespaceChars('\r','\r');
061                    while (tokens.nextToken() != StreamTokenizer.TT_EOF) {
062                        String line = tokens.sval.trim();
063                        int index = line.indexOf(':');
064                        if (index!=-1) {
065                            passwords.put(line.substring(0,index),
066                                          line.substring(index+1));
067                        }
068                    }
069                } catch (Exception e) {
070                    throw new WrappedThrowableException(e);
071                }
072            }
073            Object pass = passwords.get(username);
074            return (pass!=null && pass.equals(password));
075        }
076    }