KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 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.aspects.gui.DisplayContext;
21 import org.objectweb.jac.aspects.gui.GuiAC;
22 import org.objectweb.jac.core.Collaboration;
23 import org.objectweb.jac.core.Display;
24 import org.objectweb.jac.core.rtti.ClassRepository;
25 import org.objectweb.jac.core.rtti.MethodItem;
26 import org.objectweb.jac.util.Log;
27
28 /**
29  * This Authenticator ask for a username and password. The number of
30  * allowed attempts to enter a valid (username,password) is
31  * configurable. It needs a DisplayContext attribute in order to
32  * be able to interact with the user. */

33
34 public abstract class PasswordAuthenticator implements Authenticator {
35
36     int retries = 3;
37
38     /**
39      * The default constructor with 3 retries in case of failing. */

40
41     public PasswordAuthenticator() {}
42
43     /**
44      * This constructor can be used to parametrize the number of
45      * retries.
46      *
47      * @param retries the number of retries */

48
49     public PasswordAuthenticator(int retries) {
50         this.retries = retries;
51     }
52
53     /**
54      * This method perform the authentication by calling the
55      * askUserNameAndPassword and checkPassword methods.
56      *
57      * @return the username if the authentication succeded, null
58      * otherwise
59      *
60      * @see #askUsernameAndPassword(String,String)
61      * @see #checkPassword(String,String) */

62
63     public String JavaDoc authenticate() throws AuthenticationFailedException {
64         DisplayContext context =
65             (DisplayContext)Collaboration.get().getAttribute(GuiAC.DISPLAY_CONTEXT);
66         if (context!=null) {
67             Display display = context.getDisplay();
68             MethodItem method =
69                 ClassRepository.get().getClass(this).getMethod(
70                     "askUsernameAndPassword");
71             String JavaDoc username = null;
72             for (int i=retries; i>0 && username==null; i--) {
73                 Object JavaDoc[] parameters = new Object JavaDoc[method.getParameterTypes().length];
74                 if (!display.showInput(this,method,parameters)) {
75                     // cancel
76
Log.trace("authentication","Authentication cancelled");
77                     return null;
78                 }
79                 if (checkPassword((String JavaDoc)parameters[0],(String JavaDoc)parameters[1])) {
80                     username = (String JavaDoc)parameters[0];
81                 }
82             }
83             if (username==null) {
84                 throw new AuthenticationFailedException(
85                     "Wrong username and password");
86             } else {
87                 return username;
88             }
89         } else {
90             Log.warning("no display context available, cannot authenticate user");
91             return null;
92         }
93     }
94    
95     /**
96      * This method has to be implemented by subclasses to perform the
97      * actual password checking mechanism.
98      *
99      * @param username the username
100      * @param password the password
101      * @return true if the username exists and if the password is valid
102      * for this username */

103
104     abstract boolean checkPassword(String JavaDoc username, String JavaDoc password);
105
106     /**
107      * This empty method is used to popup a dialog on the current
108      * display so that the user can fill in the authentication
109      * information.
110      *
111      * @param username the username
112      * @param password the password */

113
114     public void askUsernameAndPassword(String JavaDoc username, String JavaDoc password) {}
115
116 }
117
Popular Tags