1 17 package org.alfresco.web.app.servlet; 18 19 import java.io.IOException ; 20 21 import javax.portlet.PortletSession; 22 import javax.servlet.ServletContext ; 23 import javax.servlet.http.Cookie ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.http.HttpSession ; 27 import javax.transaction.UserTransaction ; 28 29 import org.alfresco.error.AlfrescoRuntimeException; 30 import org.alfresco.i18n.I18NUtil; 31 import org.alfresco.model.ContentModel; 32 import org.alfresco.repo.security.authentication.AuthenticationException; 33 import org.alfresco.repo.security.permissions.AccessDeniedException; 34 import org.alfresco.service.ServiceRegistry; 35 import org.alfresco.service.cmr.repository.InvalidNodeRefException; 36 import org.alfresco.service.cmr.repository.NodeRef; 37 import org.alfresco.service.cmr.repository.NodeService; 38 import org.alfresco.service.cmr.security.AuthenticationService; 39 import org.alfresco.service.cmr.security.PermissionService; 40 import org.alfresco.service.cmr.security.PersonService; 41 import org.alfresco.web.app.Application; 42 import org.alfresco.web.app.portlet.AlfrescoFacesPortlet; 43 import org.alfresco.web.bean.LoginBean; 44 import org.alfresco.web.bean.repository.User; 45 import org.apache.commons.logging.Log; 46 import org.apache.commons.logging.LogFactory; 47 import org.springframework.web.context.WebApplicationContext; 48 import org.springframework.web.context.support.WebApplicationContextUtils; 49 50 63 public final class AuthenticationHelper 64 { 65 66 public static final String AUTHENTICATION_USER = "_alfAuthTicket"; 67 public static final String SESSION_USERNAME = "_alfLastUser"; 68 public static final String SESSION_INVALIDATED = "_alfSessionInvalid"; 69 70 71 public static final String LOGIN_BEAN = "LoginBean"; 72 73 74 private static final String AUTHENTICATION_SERVICE = "AuthenticationService"; 75 private static final String UNPROTECTED_AUTH_SERVICE = "authenticationServiceImpl"; 76 private static final String PERSON_SERVICE = "personService"; 77 78 79 private static final String COOKIE_ALFUSER = "alfUser"; 80 81 private static Log logger = LogFactory.getLog(AuthenticationHelper.class); 82 83 84 94 public static AuthenticationStatus authenticate( 95 ServletContext context, HttpServletRequest httpRequest, HttpServletResponse httpResponse, boolean guest) 96 throws IOException 97 { 98 HttpSession session = httpRequest.getSession(); 99 100 User user; 102 LoginBean loginBean = null; 103 if (Application.inPortalServer() == false) 104 { 105 user = (User)session.getAttribute(AUTHENTICATION_USER); 106 loginBean = (LoginBean)session.getAttribute(LOGIN_BEAN); 107 } 108 else 109 { 110 user = (User)session.getAttribute(AlfrescoFacesPortlet.MANAGED_BEAN_PREFIX + AUTHENTICATION_USER); 111 } 112 113 WebApplicationContext wc = WebApplicationContextUtils.getRequiredWebApplicationContext(context); 115 AuthenticationService auth = (AuthenticationService)wc.getBean(AUTHENTICATION_SERVICE); 116 117 if (user == null || guest) 118 { 119 if (session.getAttribute(AuthenticationHelper.SESSION_INVALIDATED) == null) 124 { 125 Cookie authCookie = getAuthCookie(httpRequest); 126 if (authCookie == null || guest) 127 { 128 UserTransaction tx = null; 130 try 131 { 132 auth.authenticateAsGuest(); 133 134 ServiceRegistry services = BaseServlet.getServiceRegistry(context); 136 tx = services.getTransactionService().getUserTransaction(); 137 tx.begin(); 138 139 NodeService nodeService = services.getNodeService(); 140 PersonService personService = (PersonService)wc.getBean(PERSON_SERVICE); 141 NodeRef guestRef = personService.getPerson(PermissionService.GUEST_AUTHORITY); 142 user = new User(PermissionService.GUEST_AUTHORITY, auth.getCurrentTicket(), guestRef); 143 NodeRef guestHomeRef = (NodeRef)nodeService.getProperty(guestRef, ContentModel.PROP_HOMEFOLDER); 144 145 if (nodeService.exists(guestHomeRef) == false) 147 { 148 throw new InvalidNodeRefException(guestHomeRef); 149 } 150 user.setHomeSpaceId(guestHomeRef.getId()); 151 152 tx.commit(); 153 tx = null; 155 session.setAttribute(AuthenticationHelper.AUTHENTICATION_USER, user); 157 158 I18NUtil.setLocale(Application.getLanguage(httpRequest.getSession())); 160 161 session.removeAttribute(AuthenticationHelper.SESSION_INVALIDATED); 163 164 return AuthenticationStatus.Guest; 166 } 167 catch (AuthenticationException guestError) 168 { 169 } 171 catch (AccessDeniedException accessError) 172 { 173 AuthenticationService unprotAuthService = (AuthenticationService)wc.getBean(UNPROTECTED_AUTH_SERVICE); 175 unprotAuthService.invalidateTicket(unprotAuthService.getCurrentTicket()); 176 unprotAuthService.clearCurrentSecurityContext(); 177 logger.warn("Unable to login as Guest: " + accessError.getMessage()); 178 } 179 catch (Throwable e) 180 { 181 AuthenticationService unprotAuthService = (AuthenticationService)wc.getBean(UNPROTECTED_AUTH_SERVICE); 183 unprotAuthService.invalidateTicket(unprotAuthService.getCurrentTicket()); 184 unprotAuthService.clearCurrentSecurityContext(); 185 throw new AlfrescoRuntimeException("Failed to authenticate as Guest user.", e); 186 } 187 finally 188 { 189 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 190 } 191 } 192 } 193 194 return AuthenticationStatus.Failure; 196 } 197 else 198 { 199 try 200 { 201 auth.validate(user.getTicket()); 202 } 203 catch (AuthenticationException authErr) 204 { 205 return AuthenticationStatus.Failure; 207 } 208 209 if (loginBean != null) 211 { 212 setUsernameCookie(httpRequest, httpResponse, loginBean.getUsernameInternal()); 213 } 214 215 I18NUtil.setLocale(Application.getLanguage(httpRequest.getSession())); 217 218 return AuthenticationStatus.Success; 219 } 220 } 221 222 227 public static AuthenticationStatus authenticate( 228 ServletContext context, HttpServletRequest httpRequest, HttpServletResponse httpResponse, String ticket) 229 throws IOException 230 { 231 WebApplicationContext wc = WebApplicationContextUtils.getRequiredWebApplicationContext(context); 233 AuthenticationService auth = (AuthenticationService)wc.getBean(AUTHENTICATION_SERVICE); 234 try 235 { 236 auth.validate(ticket); 237 } 238 catch (AuthenticationException authErr) 239 { 240 return AuthenticationStatus.Failure; 241 } 242 243 I18NUtil.setLocale(Application.getLanguage(httpRequest.getSession())); 245 246 return AuthenticationStatus.Success; 247 } 248 249 255 public static AuthenticationStatus portalGuestAuthenticate(WebApplicationContext ctx, PortletSession session, AuthenticationService auth) 256 { 257 UserTransaction tx = null; 258 try 259 { 260 auth.authenticateAsGuest(); 261 262 ServiceRegistry services = (ServiceRegistry)ctx.getBean(ServiceRegistry.SERVICE_REGISTRY); 264 tx = services.getTransactionService().getUserTransaction(); 265 tx.begin(); 266 267 NodeService nodeService = services.getNodeService(); 268 PersonService personService = (PersonService)ctx.getBean(PERSON_SERVICE); 269 NodeRef guestRef = personService.getPerson(PermissionService.GUEST_AUTHORITY); 270 User user = new User(PermissionService.GUEST_AUTHORITY, auth.getCurrentTicket(), guestRef); 271 NodeRef guestHomeRef = (NodeRef)nodeService.getProperty(guestRef, ContentModel.PROP_HOMEFOLDER); 272 273 if (nodeService.exists(guestHomeRef) == false) 275 { 276 throw new InvalidNodeRefException(guestHomeRef); 277 } 278 user.setHomeSpaceId(guestHomeRef.getId()); 279 280 tx.commit(); 281 tx = null; 283 session.setAttribute(AuthenticationHelper.AUTHENTICATION_USER, user); 285 286 I18NUtil.setLocale(Application.getLanguage(session)); 288 289 session.removeAttribute(AuthenticationHelper.SESSION_INVALIDATED); 291 292 return AuthenticationStatus.Guest; 294 } 295 catch (AuthenticationException guestError) 296 { 297 } 299 catch (AccessDeniedException accessError) 300 { 301 AuthenticationService unprotAuthService = (AuthenticationService)ctx.getBean(UNPROTECTED_AUTH_SERVICE); 303 unprotAuthService.invalidateTicket(unprotAuthService.getCurrentTicket()); 304 unprotAuthService.clearCurrentSecurityContext(); 305 logger.warn("Unable to login as Guest: " + accessError.getMessage()); 306 } 307 catch (Throwable e) 308 { 309 AuthenticationService unprotAuthService = (AuthenticationService)ctx.getBean(UNPROTECTED_AUTH_SERVICE); 311 unprotAuthService.invalidateTicket(unprotAuthService.getCurrentTicket()); 312 unprotAuthService.clearCurrentSecurityContext(); 313 throw new AlfrescoRuntimeException("Failed to authenticate as Guest user.", e); 314 } 315 finally 316 { 317 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 318 } 319 320 return AuthenticationStatus.Failure; 321 } 322 323 330 public static void setUsernameCookie(HttpServletRequest httpRequest, HttpServletResponse httpResponse, String username) 331 { 332 Cookie authCookie = getAuthCookie(httpRequest); 333 if (authCookie == null) 334 { 335 authCookie = new Cookie (COOKIE_ALFUSER, username); 336 } 337 else 338 { 339 authCookie.setValue(username); 340 } 341 authCookie.setPath(httpRequest.getContextPath()); 342 authCookie.setMaxAge(60*60*24*7); 344 httpResponse.addCookie(authCookie); 345 } 346 347 354 public static Cookie getAuthCookie(HttpServletRequest httpRequest) 355 { 356 Cookie authCookie = null; 357 Cookie [] cookies = httpRequest.getCookies(); 358 if (cookies != null) 359 { 360 for (int i=0; i<cookies.length; i++) 361 { 362 if (COOKIE_ALFUSER.equals(cookies[i].getName())) 363 { 364 authCookie = cookies[i]; 366 break; 367 } 368 } 369 } 370 return authCookie; 371 } 372 } 373 | Popular Tags |