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 */
019 package org.apache.wiki.auth.login;
020
021 import java.io.IOException;
022
023 import javax.security.auth.callback.*;
024 import javax.servlet.http.HttpServletRequest;
025
026 import org.apache.wiki.WikiEngine;
027
028 /**
029 * Handles logins made from inside the wiki application, rather than via the web
030 * container. This handler is instantiated in
031 * {@link org.apache.wiki.auth.AuthenticationManager#login(org.apache.wiki.WikiSession,HttpServletRequest, String, String)}.
032 * If container-managed authentication is used, the
033 * {@link WebContainerCallbackHandler}is used instead. This callback handler is
034 * designed to be used with {@link UserDatabaseLoginModule}.
035 * @since 2.3
036 */
037 public class WikiCallbackHandler implements CallbackHandler
038 {
039 private final HttpServletRequest m_request;
040
041 private final WikiEngine m_engine;
042
043 private final String m_password;
044
045 private final String m_username;
046
047 /**
048 * Create a new callback handler.
049 * @param engine the WikiEngine
050 * @param request the user's HTTP request. If passed as <code>null</code>,
051 * later requests for {@link HttpRequestCallback} will return an UnsupportedCallbackException
052 * @param username the username
053 * @param password the password
054 */
055 public WikiCallbackHandler( WikiEngine engine, HttpServletRequest request, String username, String password )
056 {
057 m_request = request;
058 m_engine = engine;
059 m_username = username;
060 m_password = password;
061 }
062
063 /**
064 * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
065 *
066 * {@inheritDoc}
067 */
068 public void handle( Callback[] callbacks ) throws IOException, UnsupportedCallbackException
069 {
070 for( int i = 0; i < callbacks.length; i++ )
071 {
072 Callback callback = callbacks[i];
073 if ( callback instanceof HttpRequestCallback )
074 {
075 ( (HttpRequestCallback) callback ).setRequest( m_request );
076 }
077 else if( callback instanceof WikiEngineCallback )
078 {
079 ( (WikiEngineCallback) callback ).setEngine( m_engine );
080 }
081 else if ( callback instanceof UserDatabaseCallback )
082 {
083 ( (UserDatabaseCallback) callback ).setUserDatabase( m_engine.getUserManager().getUserDatabase() );
084 }
085 else if ( callback instanceof NameCallback )
086 {
087 ( (NameCallback) callback ).setName( m_username );
088 }
089 else if ( callback instanceof PasswordCallback )
090 {
091 ( (PasswordCallback) callback ).setPassword( m_password.toCharArray() );
092 }
093 else
094 {
095 throw new UnsupportedCallbackException( callback );
096 }
097 }
098 }
099 }