001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.auth.login; 020 021import org.apache.log4j.Logger; 022import org.apache.wiki.auth.WikiPrincipal; 023import org.apache.wiki.util.HttpUtil; 024import org.apache.wiki.util.TextUtil; 025 026import javax.security.auth.callback.Callback; 027import javax.security.auth.callback.UnsupportedCallbackException; 028import javax.security.auth.login.FailedLoginException; 029import javax.security.auth.login.LoginException; 030import javax.servlet.http.Cookie; 031import javax.servlet.http.HttpServletRequest; 032import javax.servlet.http.HttpServletResponse; 033import javax.servlet.http.HttpSession; 034import java.io.IOException; 035 036/** 037 * <p> 038 * Logs in a user based on assertion of a name supplied in a cookie. If the 039 * cookie is not found, authentication fails. 040 * </p> 041 * This module must be used with a CallbackHandler (such as 042 * {@link WebContainerCallbackHandler}) that supports the following Callback 043 * types: 044 * </p> 045 * <ol> 046 * <li>{@link HttpRequestCallback}- supplies the cookie, which should contain 047 * a user name.</li> 048 * </ol> 049 * <p> 050 * After authentication, a generic WikiPrincipal based on the username will be 051 * created and associated with the Subject. 052 * </p> 053 * @see javax.security.auth.spi.LoginModule#commit() 054 * @see CookieAuthenticationLoginModule 055 * @since 2.3 056 */ 057public class CookieAssertionLoginModule extends AbstractLoginModule { 058 059 /** The name of the cookie that gets stored to the user browser. */ 060 public static final String PREFS_COOKIE_NAME = "JSPWikiAssertedName"; 061 062 protected static final Logger log = Logger.getLogger( CookieAssertionLoginModule.class ); 063 064 /** 065 * Logs in the user by calling back to the registered CallbackHandler with 066 * an HttpRequestCallback. The CallbackHandler must supply the current 067 * servlet HTTP request as its response. 068 * @return the result of the login; if a cookie is 069 * found, this method returns <code>true</code>. If not found, this 070 * method throws a <code>FailedLoginException</code>. 071 * @see javax.security.auth.spi.LoginModule#login() 072 * @throws {@inheritDoc} 073 */ 074 public boolean login() throws LoginException 075 { 076 // Otherwise, let's go and look for the cookie! 077 HttpRequestCallback hcb = new HttpRequestCallback(); 078 Callback[] callbacks = new Callback[] 079 { hcb }; 080 try 081 { 082 m_handler.handle( callbacks ); 083 HttpServletRequest request = hcb.getRequest(); 084 HttpSession session = ( request == null ) ? null : request.getSession( false ); 085 String sid = ( session == null ) ? NULL : session.getId(); 086 String name = (request != null) ? getUserCookie( request ) : null; 087 if ( name == null ) 088 { 089 if ( log.isDebugEnabled() ) 090 { 091 log.debug( "No cookie " + PREFS_COOKIE_NAME + " present in session ID=: " + sid ); 092 } 093 throw new FailedLoginException( "The user cookie was not found." ); 094 } 095 096 if ( log.isDebugEnabled() ) 097 { 098 log.debug( "Logged in session ID=" + sid + "; asserted=" + name ); 099 } 100 // If login succeeds, commit these principals/roles 101 m_principals.add( new WikiPrincipal( name, WikiPrincipal.FULL_NAME ) ); 102 return true; 103 } 104 catch( IOException e ) 105 { 106 log.error( "IOException: " + e.getMessage() ); 107 return false; 108 } 109 catch( UnsupportedCallbackException e ) 110 { 111 String message = "Unable to handle callback, disallowing login."; 112 log.error( message, e ); 113 throw new LoginException( message ); 114 } 115 116 } 117 118 /** 119 * Returns the username cookie value. 120 * 121 * @param request The Servlet request, as usual. 122 * @return the username, as retrieved from the cookie 123 */ 124 public static String getUserCookie( HttpServletRequest request ) 125 { 126 String cookie = HttpUtil.retrieveCookieValue( request, PREFS_COOKIE_NAME ); 127 128 return TextUtil.urlDecodeUTF8(cookie); 129 } 130 131 /** 132 * Sets the username cookie. The cookie value is URLEncoded in UTF-8. 133 * 134 * @param response The Servlet response 135 * @param name The name to write into the cookie. 136 */ 137 public static void setUserCookie( HttpServletResponse response, String name ) 138 { 139 name = TextUtil.urlEncodeUTF8(name); 140 Cookie userId = new Cookie( PREFS_COOKIE_NAME, name ); 141 userId.setMaxAge( 1001 * 24 * 60 * 60 ); // 1001 days is default. 142 response.addCookie( userId ); 143 } 144 145 /** 146 * Removes the user cookie from the response. This makes the user appear 147 * again as an anonymous coward. 148 * 149 * @param response The servlet response. 150 */ 151 public static void clearUserCookie( HttpServletResponse response ) 152 { 153 Cookie userId = new Cookie( PREFS_COOKIE_NAME, "" ); 154 userId.setMaxAge( 0 ); 155 response.addCookie( userId ); 156 } 157}