1 18 package sync4j.framework.security; 19 20 import java.util.logging.Logger ; 21 import java.util.logging.Level ; 22 23 import java.security.Principal ; 24 import javax.security.auth.*; 25 import javax.security.auth.login.*; 26 import javax.security.*; 27 28 import sync4j.framework.security.Officer; 29 import sync4j.framework.security.jaas.CredentialHandler; 30 import sync4j.framework.logging.Sync4jLogger; 31 import sync4j.framework.core.Cred; 32 33 60 public class JAASOfficer implements Officer, java.io.Serializable { 61 62 64 protected transient Logger log = Sync4jLogger.getLogger(); 65 66 68 71 private boolean loginFailed = false; 72 73 public boolean isLoginFailed() { 74 return loginFailed; 75 } 76 77 80 private boolean loginExpired = false; 81 82 public boolean isAccountExpired() { 83 return loginExpired; 84 } 85 86 89 private String clientAuth = Cred.AUTH_TYPE_BASIC; 90 91 public String getClientAuth() { 92 return clientAuth; 93 } 94 public void setClientAuth(String clientAuth) { 95 this.clientAuth = clientAuth; 96 } 97 98 101 private String serverAuth = Cred.AUTH_NONE; 102 public String getServerAuth() { 103 return this.serverAuth; 104 } 105 106 public void setServerAuth(String serverAuth) { 107 this.serverAuth = serverAuth; 108 } 109 110 112 119 public boolean authenticate(Cred credential) { 120 CredentialHandler handler = null; 121 122 if (log.isLoggable(Level.INFO)) { 123 log.info("Authenticating credential: " + credential); 124 } 125 126 try { 127 handler = new CredentialHandler(credential); 128 LoginContext lc = new LoginContext("sync4j", handler); 129 lc.login(); 130 131 if (log.isLoggable(Level.INFO)) { 132 log.info(lc.getSubject() + " authenticated!"); 133 } 134 } catch (AccountExpiredException e) { 135 log.throwing(getClass().getName(), "authenticate", e); 136 if (log.isLoggable(Level.INFO)) { 137 log.info( "Login failed for " 138 + handler.getLogin() 139 ); 140 } 141 142 loginFailed = false; 143 loginExpired = true; 144 145 return false; 146 } catch (FailedLoginException e) { 147 log.throwing(getClass().getName(), "authenticate", e); 148 if (log.isLoggable(Level.INFO)) { 149 log.info( "Login failed for " 150 + handler.getLogin() 151 ); 152 } 153 154 loginFailed = true; 155 loginExpired = false; 156 157 return false; 158 } catch (LoginException e) { 159 log.throwing(getClass().getName(), "authenticate", e); 160 if (log.isLoggable(Level.INFO)) { 161 log.info( "Login failed for " 162 + handler.getLogin() 163 ); 164 } 165 166 loginFailed = false; 167 loginExpired = false; 168 169 return false; 170 } catch (IllegalArgumentException e) { 171 log.throwing(getClass().getName(), "authenticate", e); 172 return false; 173 } catch (Throwable t) { 174 t.printStackTrace(); 178 return false; 179 } 180 181 log.info( handler.getLogin() + " logged in"); 182 return true; 183 } 184 185 194 public boolean authorize(Principal principal, String resource) { 195 return true; 196 } 197 198 202 public void unAuthenticate(Cred credential) { 203 } 208 209 private void readObject(java.io.ObjectInputStream in) 210 throws java.io.IOException , ClassNotFoundException { 211 in.defaultReadObject(); 212 log = Sync4jLogger.getLogger(); 213 } 214 } | Popular Tags |