package org.alfresco.repo.webdav.auth; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.alfresco.model.ContentModel; import org.alfresco.repo.SessionUser; import org.alfresco.repo.security.authentication.AuthenticationException; import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.Authorization; import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.security.AuthenticationService; import org.alfresco.service.cmr.security.PersonService; import org.alfresco.service.transaction.TransactionService; import org.apache.commons.codec.binary.Base64; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** *
* Basic HTTP web authentication implementation. Main purpose to use as fallback authentication with SSO filters. *
* * @author pavel.yurkevich */ public class SSOFallbackBasicAuthenticationDriver implements AuthenticationDriver { private Log logger = LogFactory.getLog(SSOFallbackBasicAuthenticationDriver.class); private AuthenticationService authenticationService; private PersonService personService; private NodeService nodeService; private TransactionService transactionService; private String userAttributeName = AuthenticationDriver.AUTHENTICATION_USER; public void setAuthenticationService(AuthenticationService authenticationService) { this.authenticationService = authenticationService; } public void setPersonService(PersonService personService) { this.personService = personService; } public void setNodeService(NodeService nodeService) { this.nodeService = nodeService; } public void setTransactionService(TransactionService transactionService) { this.transactionService = transactionService; } public void setUserAttributeName(String userAttributeName) { this.userAttributeName = userAttributeName; } @Override public boolean authenticateRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String authHdr = request.getHeader("Authorization"); HttpSession session = request.getSession(false); SessionUser user = session == null ? null : (SessionUser) session.getAttribute(userAttributeName); if (user == null) { if (authHdr != null && authHdr.length() > 5 && authHdr.substring(0, 5).equalsIgnoreCase("Basic")) { String basicAuth = new String(Base64.decodeBase64(authHdr.substring(5).getBytes())); String username = null; String password = null; int pos = basicAuth.indexOf(":"); if (pos != -1) { username = basicAuth.substring(0, pos); password = basicAuth.substring(pos + 1); } else { username = basicAuth; password = ""; } try { if (logger.isDebugEnabled()) logger.debug("Authenticating user '" + username + "'"); Authorization auth = new Authorization(username, password); if (auth.isTicket()) { authenticationService.validate(auth.getTicket()); } else { authenticationService.authenticate(username, password.toCharArray()); } final RetryingTransactionCallback