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.logging.log4j.LogManager;
022import org.apache.logging.log4j.Logger;
023import org.apache.wiki.auth.NoSuchPrincipalException;
024import org.apache.wiki.auth.WikiPrincipal;
025import org.apache.wiki.auth.user.UserDatabase;
026import org.apache.wiki.auth.user.UserProfile;
027
028import javax.security.auth.callback.Callback;
029import javax.security.auth.callback.NameCallback;
030import javax.security.auth.callback.PasswordCallback;
031import javax.security.auth.callback.UnsupportedCallbackException;
032import javax.security.auth.login.AccountLockedException;
033import javax.security.auth.login.FailedLoginException;
034import javax.security.auth.login.LoginException;
035import java.io.IOException;
036
037/**
038 * <p>
039 * Logs in a user based on a username, password, and static password file
040 * location. This module must be used with a CallbackHandler (such as
041 * {@link WikiCallbackHandler}) that supports the following Callback types:
042 * </p>
043 * <ol>
044 * <li>{@link javax.security.auth.callback.NameCallback}- supplies the
045 * username</li>
046 * <li>{@link javax.security.auth.callback.PasswordCallback}- supplies the
047 * password</li>
048 * <li>{@link org.apache.wiki.auth.login.UserDatabaseCallback}- supplies the
049 * {@link org.apache.wiki.auth.user.UserDatabase}</li>
050 * </ol>
051 * <p>
052 * After authentication, a Principals based on the login name will be created
053 * and associated with the Subject.
054 * </p>
055 * @since 2.3
056 */
057public class UserDatabaseLoginModule extends AbstractLoginModule {
058
059    private static final Logger LOG = LogManager.getLogger( UserDatabaseLoginModule.class );
060
061    /**
062     * @see javax.security.auth.spi.LoginModule#login()
063     * 
064     * {@inheritDoc}
065     */
066    @Override
067    public boolean login() throws LoginException
068    {
069        final UserDatabaseCallback ucb = new UserDatabaseCallback();
070        final NameCallback ncb = new NameCallback( "User name" );
071        final PasswordCallback pcb = new PasswordCallback( "Password", false );
072        final Callback[] callbacks = new Callback[]
073        { ucb, ncb, pcb };
074        try
075        {
076            m_handler.handle( callbacks );
077            final UserDatabase db = ucb.getUserDatabase();
078            final String username = ncb.getName();
079            final String password = new String( pcb.getPassword() );
080
081            // Look up the user and compare the password hash
082            if ( db == null ) {
083                throw new FailedLoginException( "No user database: check the callback handler code!" );
084            }
085            final UserProfile profile = db.findByLoginName( username );
086            final String storedPassword = profile.getPassword();
087            if ( storedPassword != null && db.validatePassword( username, password ) ) {
088                // Only someone who knows the password is told that the profile is locked
089                if( profile.isLocked() ) {
090                    LOG.info( "Login denied for locked user database user {}", username );
091                    throw new AccountLockedException( "The profile is locked." );
092                }
093                LOG.debug( "Logged in user database user {}", username );
094
095                // If login succeeds, commit these principals/roles
096                m_principals.add( new WikiPrincipal( username,  WikiPrincipal.LOGIN_NAME ) );
097
098                return true;
099            }
100            throw new FailedLoginException( "The username or password is incorrect." );
101        } catch( final IOException e ) {
102            final String message = "IO exception; disallowing login.";
103            LOG.error( message, e );
104            throw new LoginException( message );
105        } catch( final UnsupportedCallbackException e ) {
106            final String message = "Unable to handle callback; disallowing login.";
107            LOG.error( message, e );
108            throw new LoginException( message );
109        } catch( final NoSuchPrincipalException e ) {
110            throw new FailedLoginException( "The username or password is incorrect." );
111        }
112    }
113
114}