1 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 33 34 public abstract class PasswordAuthenticator implements Authenticator { 35 36 int retries = 3; 37 38 40 41 public PasswordAuthenticator() {} 42 43 48 49 public PasswordAuthenticator(int retries) { 50 this.retries = retries; 51 } 52 53 62 63 public String 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 username = null; 72 for (int i=retries; i>0 && username==null; i--) { 73 Object [] parameters = new Object [method.getParameterTypes().length]; 74 if (!display.showInput(this,method,parameters)) { 75 Log.trace("authentication","Authentication cancelled"); 77 return null; 78 } 79 if (checkPassword((String )parameters[0],(String )parameters[1])) { 80 username = (String )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 103 104 abstract boolean checkPassword(String username, String password); 105 106 113 114 public void askUsernameAndPassword(String username, String password) {} 115 116 } 117 | Popular Tags |