1 package org.apache.archiva.redback.integration.util;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import javax.annotation.Resource;
23 import javax.servlet.http.Cookie;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
27 import org.apache.archiva.redback.keys.AuthenticationKey;
28 import org.apache.archiva.redback.keys.KeyManager;
29 import org.apache.archiva.redback.keys.KeyManagerException;
30 import org.apache.archiva.redback.keys.KeyNotFoundException;
31 import org.apache.archiva.redback.policy.CookieSettings;
32 import org.apache.archiva.redback.system.SecuritySystem;
33 import org.codehaus.plexus.util.StringUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.stereotype.Service;
41 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
44 @Service("autoLoginCookies")
45 public class AutoLoginCookies
48 private Logger log = LoggerFactory.getLogger( getClass() );
51 private SecuritySystem securitySystem;
54 * Cookie key for the Remember Me functionality.
56 private static final String REMEMBER_ME_KEY = "rbkRememberMe";
59 * Cookie key for the signon cookie.
61 private static final String SIGNON_KEY = "rbkSignon";
63 public AuthenticationKey getRememberMeKey(HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest )
65 if ( !isRememberMeEnabled() )
70 Cookie rememberMeCookie = getCookie( httpServletRequest, REMEMBER_ME_KEY );
72 if ( rememberMeCookie == null )
74 log.debug( "Remember Me Cookie Not Found: {}", REMEMBER_ME_KEY );
78 // Found user with a remember me key.
79 String providedKey = rememberMeCookie.getValue();
81 log.debug( "Found remember me cookie : {}", providedKey );
83 CookieSettings settings = securitySystem.getPolicy().getRememberMeCookieSettings();
84 return findAuthKey( REMEMBER_ME_KEY, providedKey, settings.getDomain(), settings.getPath(), httpServletResponse, httpServletRequest );
87 public void setRememberMeCookie( String principal, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest )
89 if ( !isRememberMeEnabled() )
96 CookieSettings settings = securitySystem.getPolicy().getRememberMeCookieSettings();
97 int timeout = settings.getCookieTimeout();
98 KeyManager keyManager = securitySystem.getKeyManager();
99 AuthenticationKey authkey = keyManager.createKey( principal, "Remember Me Key", timeout );
101 Cookie cookie = createCookie( REMEMBER_ME_KEY, authkey.getKey(), settings.getDomain(), settings.getPath(), httpServletRequest );
104 cookie.setMaxAge( timeout );
106 httpServletResponse.addCookie( cookie );
109 catch ( KeyManagerException e )
111 log.warn( "Unable to set remember me cookie." );
115 public void removeRememberMeCookie( HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest )
117 CookieSettings settings = securitySystem.getPolicy().getRememberMeCookieSettings();
118 removeCookie( httpServletResponse, httpServletRequest, REMEMBER_ME_KEY, settings.getDomain(), settings.getPath() );
121 public AuthenticationKey getSignonKey( HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest )
123 Cookie ssoCookie = getCookie( httpServletRequest, SIGNON_KEY );
125 if ( ssoCookie == null )
127 log.debug( "Single Sign On Cookie Not Found: {}", SIGNON_KEY );
131 // Found user with a single sign on key.
133 String providedKey = ssoCookie.getValue();
135 log.debug( "Found sso cookie : {}", providedKey );
137 CookieSettings settings = securitySystem.getPolicy().getSignonCookieSettings();
138 return findAuthKey( SIGNON_KEY, providedKey, settings.getDomain(), settings.getPath(), httpServletResponse, httpServletRequest );
141 public void setSignonCookie( String principal, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest )
145 CookieSettings settings = securitySystem.getPolicy().getSignonCookieSettings();
146 int timeout = settings.getCookieTimeout();
147 KeyManager keyManager = securitySystem.getKeyManager();
148 AuthenticationKey authkey = keyManager.createKey( principal, "Signon Session Key", timeout );
150 /* The path must remain as "/" in order for SSO to work on installations where the only
151 * all of the servers are installed into the same web container but under different
154 Cookie cookie = createCookie( SIGNON_KEY, authkey.getKey(), settings.getDomain(), settings.getPath(), httpServletRequest );
157 cookie.setMaxAge( timeout );
159 httpServletResponse.addCookie( cookie );
162 catch ( KeyManagerException e )
164 log.warn( "Unable to set single sign on cookie." );
169 public void removeSignonCookie( HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest )
171 CookieSettings settings = securitySystem.getPolicy().getSignonCookieSettings();
172 removeCookie( httpServletResponse, httpServletRequest, SIGNON_KEY, settings.getDomain(), settings.getPath() );
175 private static String getWebappContext( HttpServletRequest httpRequest )
177 // Calculate the webapp context.
178 String webappContext = httpRequest.getContextPath();
180 if ( StringUtils.isEmpty( webappContext ) )
182 // Still empty? means you are a root context.
186 return webappContext;
189 public boolean isRememberMeEnabled()
191 return securitySystem.getPolicy().getRememberMeCookieSettings().isEnabled();
194 private AuthenticationKey findAuthKey( String cookieName, String providedKey, String domain, String path,
195 HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest )
199 AuthenticationKey authkey = securitySystem.getKeyManager().findKey( providedKey );
201 log.debug( "Found AuthKey: {}", authkey );
205 catch ( KeyNotFoundException e )
207 log.info( "Invalid AuthenticationKey {} submitted. Invalidating cookie.", providedKey );
209 // Invalid Cookie. Remove it.
210 removeCookie( httpServletResponse, httpServletRequest, cookieName, domain, path );
212 catch ( KeyManagerException e )
214 log.error( "KeyManagerException: " + e.getMessage(), e );
220 private static Cookie getCookie( HttpServletRequest request, String name )
222 Cookie[] cookies = request.getCookies();
224 Cookie cookie = null;
225 if ( cookies != null && !StringUtils.isEmpty( name ) )
227 for ( int i = 0; i < cookies.length && cookie == null; i++ )
229 if ( StringUtils.equals( name, cookies[i].getName() ) )
239 private static void removeCookie( HttpServletResponse response, HttpServletRequest httpRequest, String cookieName, String domain, String path )
241 Cookie cookie = createCookie( cookieName, "", domain, path, httpRequest );
242 cookie.setMaxAge( 0 );
243 response.addCookie( cookie );
246 private static Cookie createCookie( String cookieName, String value, String domain, String path, HttpServletRequest httpRequest )
248 Cookie cookie = new Cookie( cookieName, value );
249 if ( domain != null )
251 cookie.setDomain( domain );
255 cookie.setPath( path );
259 // default to the context path, otherwise you get /security and such in some places
260 cookie.setPath( getWebappContext( httpRequest ) );