1 13 package info.magnolia.jaas.sp.jcr; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.core.Content; 17 import info.magnolia.cms.core.HierarchyManager; 18 import info.magnolia.cms.security.auth.Entity; 19 import info.magnolia.jaas.principal.EntityImpl; 20 import info.magnolia.jaas.sp.AbstractLoginModule; 21 22 import java.io.IOException ; 23 24 import javax.jcr.PathNotFoundException; 25 import javax.jcr.RepositoryException; 26 import javax.security.auth.callback.Callback ; 27 import javax.security.auth.callback.NameCallback ; 28 import javax.security.auth.callback.PasswordCallback ; 29 import javax.security.auth.callback.UnsupportedCallbackException ; 30 import javax.security.auth.login.LoginException ; 31 import javax.security.auth.login.FailedLoginException ; 32 33 import org.apache.commons.codec.binary.Base64; 34 import org.apache.commons.lang.StringUtils; 35 import org.slf4j.Logger; 36 import org.slf4j.LoggerFactory; 37 38 39 42 public class JCRAuthenticationModule extends AbstractLoginModule { 43 44 47 private static Logger log = LoggerFactory.getLogger(JCRAuthenticationModule.class); 48 49 protected String name; 50 51 protected char[] pswd; 52 53 protected boolean success; 54 55 protected Content user; 56 57 60 public boolean login() throws LoginException { 61 if (this.callbackHandler == null) { 62 throw new LoginException ("Error: no CallbackHandler available for JCRModule"); 63 } 64 65 Callback [] callbacks = new Callback [2]; 66 callbacks[0] = new NameCallback ("name"); 67 callbacks[1] = new PasswordCallback ("pswd", false); 68 69 this.success = false; 70 try { 71 this.callbackHandler.handle(callbacks); 72 this.name = ((NameCallback ) callbacks[0]).getName(); 73 this.pswd = ((PasswordCallback ) callbacks[1]).getPassword(); 74 this.success = this.isValidUser(); 75 } 76 catch (IOException ioe) { 77 if (log.isDebugEnabled()) { 78 log.debug("Exception caught", ioe); 79 } 80 throw new LoginException (ioe.toString()); 81 } 82 catch (UnsupportedCallbackException ce) { 83 if (log.isDebugEnabled()) { 84 log.debug(ce.getMessage(), ce); 85 } 86 throw new LoginException (ce.getCallback().toString() + " not available"); 87 } 88 if (!this.success) { 89 throw new FailedLoginException ("failed to authenticate " + this.name); 90 } 91 92 return this.success; 93 } 94 95 98 public boolean commit() throws LoginException { 99 if (!this.success) { 100 throw new LoginException ("failed to authenticate " + this.name); 101 } 102 this.setEntity(); 103 return true; 104 } 105 106 109 public boolean release() { 110 return true; 111 } 112 113 117 public boolean isValidUser() { 118 HierarchyManager hm = ContentRepository.getHierarchyManager(ContentRepository.USERS); 119 try { 120 this.user = hm.getContent(this.name); 121 String serverPassword = this.user.getNodeData("pswd").getString().trim(); 122 if (StringUtils.isEmpty(serverPassword)) return false; 124 serverPassword = new String (Base64.decodeBase64(serverPassword.getBytes())); 126 return StringUtils.equals(serverPassword, new String (this.pswd)); 127 } 128 catch (PathNotFoundException pe) { 129 log.info("Unable to locate user [{}], authentication failed", this.name); 130 } 131 catch (RepositoryException re) { 132 log.error("Unable to locate user [" 133 + this.name 134 + "], authentication failed due to a " 135 + re.getClass().getName(), re); 136 } 137 return false; 138 } 139 140 143 public void setEntity() { 144 EntityImpl user = new EntityImpl(); 145 String language = this.user.getNodeData("language").getString(); 146 user.addProperty(Entity.LANGUAGE, language); 147 user.addProperty(Entity.NAME, this.user.getName()); 148 user.addProperty(Entity.FULL_NAME, this.user.getTitle()); 149 user.addProperty(Entity.PASSWORD, new String (this.pswd)); 150 this.subject.getPrincipals().add(user); 151 } 152 153 156 public void setACL() { 157 } 158 159 } 160 | Popular Tags |