1 28 package net.sf.jguard.ext.authentication.manager; 29 30 import java.io.IOException ; 31 import java.io.OutputStream ; 32 import java.util.Locale ; 33 34 import javax.security.auth.Subject ; 35 import javax.security.auth.callback.CallbackHandler ; 36 import javax.security.auth.login.Configuration ; 37 import javax.security.auth.login.LoginContext ; 38 import javax.security.auth.login.LoginException ; 39 40 import net.sf.jguard.core.authentication.configuration.LocalLoginContext; 41 import net.sf.jguard.core.principals.UserPrincipal; 42 import net.sf.jguard.ext.util.ThrowableUtils; 43 44 import org.apache.commons.logging.Log; 45 import org.apache.commons.logging.LogFactory; 46 47 48 52 public class AuthenticationUtils { 53 54 private LoginContext loginContext = null; 55 private LocalLoginContext localLoginContext = null; 56 private Subject subject = null; 57 private boolean loggedOut = false; 58 private boolean local = false; 59 private static final Log logger = LogFactory.getLog(AuthenticationUtils.class); 60 private Configuration configuration = null; 61 62 public AuthenticationUtils(){ 63 super(); 64 } 65 66 67 public AuthenticationUtils(Configuration config){ 68 super(); 69 configuration = config; 70 local= true; 71 } 72 73 79 public void login( String applicationName, CallbackHandler cbh,Locale locale) throws LoginException { 80 81 try{ 82 if(local){ 83 localLoginContext = new LocalLoginContext(applicationName,cbh,configuration); 84 }else{ 85 loginContext = new LoginContext (applicationName,cbh); 86 } 87 } catch(LoginException se) { 88 logger.error("LoginContext cannot be created. "+ se.getMessage(),se); 89 throw se; 90 } 91 92 try{ 93 if(local){ 94 localLoginContext.login(); 95 }else{ 96 loginContext.login(); 97 } 98 99 }catch(LoginException le){ 100 Throwable localizedThrowable = ThrowableUtils.localizeThrowable(le,locale); 101 throw (LoginException )localizedThrowable; 102 } 103 104 if(local){ 105 subject = localLoginContext.getSubject(); 106 }else{ 107 subject = loginContext.getSubject(); 108 } 109 if(subject != null){ 110 UserPrincipal userPrincipal = new UserPrincipal(subject); 112 subject.getPrincipals().add(userPrincipal); 113 } 114 } 115 116 117 118 119 123 public Subject getSubject(){ 124 return subject; 125 } 126 127 130 public void logout() { 131 if(loggedOut==false){ 132 try { 133 if(local){ 134 if(localLoginContext!=null){ 135 localLoginContext.logout(); 136 loggedOut = true; 137 }else{ 138 logger.debug(" user is not logged, so we don't logout him "); 139 } 140 }else{ 141 if(loginContext!=null){ 142 loginContext.logout(); 143 loggedOut = true; 144 }else{ 145 logger.debug(" user is not logged, so we don't logout him "); 146 } 147 } 148 } catch (LoginException e) { 149 logger.error(" error raised when the user logout ", e); 150 } 151 } 152 } 153 154 public boolean isLocal() { 155 return local; 156 } 157 158 public static String exportAsXMLString(AuthenticationManager authenticationManager){ 159 XmlAuthenticationManager xmlAuthenticationManager = exportAsXmlAuthenticationManager(authenticationManager); 160 return xmlAuthenticationManager.exportAsXMLString(); 161 } 162 163 168 public static XmlAuthenticationManager exportAsXmlAuthenticationManager(AuthenticationManager authenticationManager) { 169 XmlAuthenticationManager xmlAuthenticationManager = null; 170 xmlAuthenticationManager = new XmlAuthenticationManager(); 171 xmlAuthenticationManager.importAuthenticationManager(authenticationManager); 172 173 return xmlAuthenticationManager; 174 } 175 176 public static void writeAsHTML(AuthenticationManager authenticationManager,OutputStream outputStream) throws IOException { 177 XmlAuthenticationManager xmlAuthenticationManager = exportAsXmlAuthenticationManager(authenticationManager); 178 xmlAuthenticationManager.writeAsHTML(outputStream); 179 } 180 181 public static void writeAsXML(AuthenticationManager authenticationManager,OutputStream outputStream,String encodingScheme) throws IOException { 182 XmlAuthenticationManager xmlAuthenticationManager = exportAsXmlAuthenticationManager(authenticationManager); 183 xmlAuthenticationManager.writeAsXML(outputStream, encodingScheme); 184 } 185 186 public static void exportAsXMLFile(AuthenticationManager authenticationManager,String fileName) throws IOException { 187 XmlAuthenticationManager xmlAuthenticationManager = exportAsXmlAuthenticationManager(authenticationManager); 188 xmlAuthenticationManager.exportAsXMLFile(fileName); 189 } 190 } 191 | Popular Tags |