KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > authentication > FilePasswordAuthenticator


1 /*
2   Copyright (C) 2001-2002 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.authentication;
19
20 import org.objectweb.jac.util.WrappedThrowableException;
21 import java.io.File JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.StreamTokenizer JavaDoc;
24 import java.util.HashMap JavaDoc;
25
26 /**
27  * This Authenticator ask for a username and password and compares
28  * them to declared ones stored in a file. The number of allowed
29  * attempts to enter a valid (username,password) is configurable. It
30  * needs a DisplayContext attribute in order to be able to interact
31  * with the user. */

32
33 public class FilePasswordAuthenticator extends PasswordAuthenticator {
34
35     File JavaDoc passwordFile;
36
37     /**
38      * The constructor.
39      *
40      * @param retries the number of time the authenticator will ask the
41      * user to retype wrong information
42      * @param passwordFilename the filename where the users are stored
43      */

44     public FilePasswordAuthenticator(String JavaDoc retries, String JavaDoc passwordFilename) {
45         super(Integer.parseInt(retries));
46         passwordFile = new File JavaDoc(passwordFilename);
47     }
48
49     HashMap JavaDoc passwords;
50
51     boolean checkPassword(String JavaDoc username, String JavaDoc password) {
52         if (passwords==null) {
53             // read the password file
54
passwords = new HashMap JavaDoc();
55             try {
56                 StreamTokenizer JavaDoc tokens = new StreamTokenizer JavaDoc(new FileReader JavaDoc(passwordFile));
57                 tokens.resetSyntax();
58                 tokens.wordChars('\000','\377');
59                 tokens.whitespaceChars('\n','\n');
60                 tokens.whitespaceChars('\r','\r');
61                 while (tokens.nextToken() != StreamTokenizer.TT_EOF) {
62                     String JavaDoc line = tokens.sval.trim();
63                     int index = line.indexOf(':');
64                     if (index!=-1) {
65                         passwords.put(line.substring(0,index),
66                                       line.substring(index+1));
67                     }
68                 }
69             } catch (Exception JavaDoc e) {
70                 throw new WrappedThrowableException(e);
71             }
72         }
73         Object JavaDoc pass = passwords.get(username);
74         return (pass!=null && pass.equals(password));
75     }
76 }
77
Popular Tags