package org.bodington.server.realm;


import java.util.Properties;
import java.util.logging.*;

import org.bodington.database.PrimaryKey;
import org.bodington.server.BuildingServerException;


public class PassPhraseAuthenticator implements Authenticator
{
 
    Properties credentials;
    boolean valid = false;
    boolean authenticated;
    PassPhrase pass_phrase;
    PrimaryKey user_id=null;
    String error=null;
    
    private void authenticate() throws AuthenticationException
    {
	if ( valid ) return;

	authenticated = false;
	error=null;
	user_id=null;
	
	if ( credentials == null )
	    throw new AuthenticationException( "Error, attempt to authenticate without credentials." );
	
	try
	{
	    String u = credentials.getProperty( "user_name" );
	    String p = credentials.getProperty( "pass_phrase" );
	    pass_phrase = PassPhrase.findPassPhraseByUserName( u );
	    if ( pass_phrase != null && pass_phrase.isPassPhrase( p ) )
	    {
		user_id = pass_phrase.getUserId();
		authenticated = true;
	    }
	}
	catch ( BuildingServerException bs )
	{
	    Logger.getLogger( "org.bodington" ).logp(
		Level.SEVERE,
		"PassPhraseAuthenticator",
		"authenticate",
		bs.getMessage(),
		bs );
	    throw new AuthenticationException( "Technical problem authenticating user." );
	}
	
    valid = true;
    }
    
    
    public PrimaryKey getAuthenticatedUserId() throws AuthenticationException
    {
	authenticate();
	return user_id;
    }
    
    public String getAuthenticationError() throws AuthenticationException
    {
	authenticate();
	return error;
    }
    
    public void setAuthenticationCredentials(Properties credentials) throws AuthenticationException
    {
	this.credentials = credentials;
	valid = false;
    }
    
    public void changeCredentials( Properties credentials ) throws AuthenticationException
    {
	try
	{
	    if ( !isAuthenticated() )
		throw new AuthenticationException( "Technical problem changing password - user isn't authenticated." );
	    
	    String p = credentials.getProperty( "pass_phrase" );
	    if ( p == null || p.length() == 0 )
		throw new AuthenticationException( "Technical problem changing password - no password was supplied." );

	    if ( p.length() < 6 )
		throw new AuthenticationException( "The password was not long enough - please supply at least 6 characters." );
		
	    if ( pass_phrase == null )
		throw new AuthenticationException( "Technical problem changing password - can't find database record." );
	    
	    pass_phrase.setUnencryptedPassPhrase( p );
	    pass_phrase.save();
	}
	catch ( BuildingServerException bs )
	{
	    Logger.getLogger( "org.bodington" ).logp(
		Level.SEVERE,
		"PassPhraseAuthenticator",
		"authenticate",
		bs.getMessage(),
		bs );
	    throw new AuthenticationException( "Technical problem saving password." );
	}
    }
        public boolean isAuthenticated() throws AuthenticationException
    {
	authenticate();
	return authenticated;
    }

    public boolean isAnonymous() throws AuthenticationException
    {
	return false;
    }
    
}
