john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

AppProperties AuthgatewayServiceDAO

// 2012-08-29 johnpfeiffer requires AuthgatewayService , FileSystem
// TODO: Builder Pattern

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Properties;

import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;

final class AuthgatewayServiceDAO
{
    protected static final String AUTHGATEWAYAPPPROPERTIES = "/var/lib/tomcat6/webapps/authgateway/WEB-INF/app.properties";
    protected static final String SHAREDSECRETIDENTIFIER = "sharedSecret";
    protected static final String LOGINEXPIRATIONINMINUTESIDENTIFIER = "loginExpirationInMinutes";
    protected static final String NETWORKIDAUTHENTICATIONURLIDENTIFIER = "networkIdAuthenticationURL";
    protected static final String NETWORKIDAUTHENTICATIONTIMEOUTINMSIDENTIFIER = "networkIdAuthenticationTimeoutInMs";

    protected static final String PREAUTHGATEWAYSERVICEIDENTIFIER = "preAuthGatewayService";
    protected static final String RSAACTIVEIDENTIFIER = "rsa1.active";
    protected static final String RSAAGENTPATHIDENTIFIER = "rsa1.rsaAgentPath";

    private Properties appProperties;

    private AuthgatewayService authgatewayService1;
    private AuthgatewayService authgatewayService2;
    private AuthgatewayService authgatewayService3;

    AuthgatewayServiceDAO() throws InstantiationException// Constructor is essentially "read from file"
    {
        FileSystem fs = new FileSystem();
        String fileLocation = null;
        try
        {
            fileLocation = fs.convertFileSystemPath( AUTHGATEWAYAPPPROPERTIES );
        }catch( IOException e )
        {
            throw new InstantiationException( "ERROR: problem converting " + AUTHGATEWAYAPPPROPERTIES + " to " + fileLocation + " " + e.getMessage() );
        }
        if( fileLocation == null || fileLocation.isEmpty() )
        {
            throw new InstantiationException( "ERROR: could not access the data source " + AUTHGATEWAYAPPPROPERTIES );
        }

        try
        {
            LoadPropertiesFromFile( fileLocation );
        }catch( FileNotFoundException e )
        {
            throw new InstantiationException( "ERROR: could not find the data source " + AUTHGATEWAYAPPPROPERTIES + " " + e.getMessage() );
        }catch( IOException e )
        {
            throw new InstantiationException( "ERROR: could not access the data source " + AUTHGATEWAYAPPPROPERTIES + " " + e.getMessage() );
        }
        if( this.appProperties == null )
        {
            throw new InstantiationException( "ERROR: reading properties from " + AUTHGATEWAYAPPPROPERTIES );
        }

        try
        {
            this.authgatewayService1 = populateAuthgatewayService( "service1" );
        }catch( IllegalArgumentException e )
        {
            throw new InstantiationException( "ERROR: error populating authentication service 1: " + e.getMessage() );
        }
        try
        {
            this.authgatewayService2 = populateAuthgatewayService( "service2" );
        }catch( IllegalArgumentException e )
        {
            throw new InstantiationException( "ERROR: error populating authentication service 2: " + e.getMessage() );
        }
        try
        {
            this.authgatewayService3 = populateAuthgatewayService( "service3" );
        }catch( IllegalArgumentException e )
        {
            throw new InstantiationException( "ERROR: error populating authentication service 3: " + e.getMessage() );
        }
    }

    private AuthgatewayService populateAuthgatewayService( String serviceNumber ) throws IllegalArgumentException
    {
        String header = "authgateway." + serviceNumber + ".";
        String activeString = appProperties.getProperty( header + AuthgatewayService.PARAMACTIVE );
        if( activeString == null )
        {
            throw new IllegalArgumentException( "ERROR: unable to read property: " + header + AuthgatewayService.PARAMACTIVE );
        }
        boolean active = false;
        if( activeString.equals( "true" ) )
        {
            active = true;
        }
        String host = appProperties.getProperty( header + AuthgatewayService.PARAMHOST );
        if( host == null )
        {
            throw new IllegalArgumentException( "ERROR: unable to read property: " + header + AuthgatewayService.PARAMHOST );
        }
        String portString = appProperties.getProperty( header + AuthgatewayService.PARAMPORT );
        if( portString == null )
        {
            throw new IllegalArgumentException( "ERROR: unable to read property: " + header + AuthgatewayService.PARAMPORT );
        }
        int port = Integer.parseInt( portString );
        String sslString = appProperties.getProperty( header + AuthgatewayService.PARAMSSL );
        if( sslString == null )
        {
            throw new IllegalArgumentException( "ERROR: unable to read property: " + header + AuthgatewayService.PARAMSSL );
        }
        boolean ssl = false;
        if( sslString.equals( "true" ) )
        {
            ssl = true;
        }
        String baseDNString = appProperties.getProperty( header + AuthgatewayService.PARAMBASEDN );
        if( baseDNString == null )
        {
            throw new IllegalArgumentException( "ERROR: unable to read property: " + header + AuthgatewayService.PARAMBASEDN );
        }
        LdapName baseDN;
        try
        {
            baseDN = new LdapName( baseDNString );
        }catch( InvalidNameException e )
        {
            throw new IllegalArgumentException( "ERROR: baseDN " + baseDNString + " is an invalid ldap name: " + e.getMessage() );
        }
        String userDNString = appProperties.getProperty( header + AuthgatewayService.PARAMUSERDN );
        if( userDNString == null )
        {
            throw new IllegalArgumentException( "ERROR: unable to read property: " + header + AuthgatewayService.PARAMUSERDN );
        }
        LdapName userDN;
        try
        {
            userDN = new LdapName( userDNString );
        }catch( InvalidNameException e )
        {
            throw new IllegalArgumentException( "ERROR: userDN " + userDNString + " is an invalid ldap name: " + e.getMessage() );
        }
        String userDNPassword = appProperties.getProperty( header + AuthgatewayService.PARAMUSERDNPASSWORD );
        if( userDNPassword == null )
        {
            throw new IllegalArgumentException( "ERROR: unable to read property: " + header + AuthgatewayService.PARAMUSERDNPASSWORD );
        }
        String searchBaseString = appProperties.getProperty( header + AuthgatewayService.PARAMSEARCHBASE );
        if( searchBaseString == null )
        {
            throw new IllegalArgumentException( "ERROR: unable to read property: " + header + AuthgatewayService.PARAMSEARCHBASE );
        }
        LdapName searchBase;
        try
        {
            searchBase = new LdapName( searchBaseString );
        }catch( InvalidNameException e )
        {
            throw new IllegalArgumentException( "ERROR: searchBase " + searchBaseString + " is an invalid ldap name: " + e.getMessage() );
        }
        String ldapUserAttribute = appProperties.getProperty( header + AuthgatewayService.PARAMLDAPUSERATTRIBUTE );
        if( ldapUserAttribute == null )
        {
            throw new IllegalArgumentException( "ERROR: unable to read property: " + header + AuthgatewayService.PARAMLDAPUSERATTRIBUTE );
        }
        String searchSubtreeTemp = appProperties.getProperty( header + AuthgatewayService.PARAMSEARCHSUBTREE );
        if( searchSubtreeTemp == null )
        {
            throw new IllegalArgumentException( "ERROR: unable to read property: " + header + AuthgatewayService.PARAMSEARCHSUBTREE );
        }
        boolean searchSubtree = false;
        if( searchSubtreeTemp.equals( "true" ) )
        {
            searchSubtree = true;
        }

        AuthgatewayService result = new AuthgatewayService.Builder().active( active ).hostname( host ).port( port ).ldapSSL( ssl ).ldapBaseDn( baseDN )
                .ldapManagerUserDn( userDN ).ldapManagerPassword( userDNPassword ).ldapUserAttribute( ldapUserAttribute ).ldapUserSearchBase( searchBase )
                .ldapSearchSubtree( searchSubtree ).build();
        return result;
    }

    protected void writeConfigurationToFile() throws IOException , NumberFormatException
    {
        String header = "authgateway.";
        String newline = System.getProperty( "line.separator" );
        StringBuilder strb = new StringBuilder();

        header = "authgateway.service1.";
        strb.append( authgatewayService1.getString( header ) + newline );
        header = "authgateway.service2.";
        strb.append( authgatewayService2.getString( header ) + newline );
        header = "authgateway.service3.";
        strb.append( authgatewayService3.getString( header ) + newline );

        String sharedSecret = appProperties.getProperty( header + SHAREDSECRETIDENTIFIER );
        String networkIdAuthenticationURL = appProperties.getProperty( header + NETWORKIDAUTHENTICATIONURLIDENTIFIER );
        String rsaActive = appProperties.getProperty( header + RSAACTIVEIDENTIFIER );
        String rsaAgentPath = appProperties.getProperty( header + RSAAGENTPATHIDENTIFIER );
        String preAuthGatewayService = appProperties.getProperty( header + PREAUTHGATEWAYSERVICEIDENTIFIER );
        String loginExpirationInMinutesString = appProperties.getProperty( header + LOGINEXPIRATIONINMINUTESIDENTIFIER );
        String networkIdAuthenticationTimeoutInMsString = appProperties.getProperty( header + NETWORKIDAUTHENTICATIONTIMEOUTINMSIDENTIFIER );

        strb.append( header + SHAREDSECRETIDENTIFIER + "=" + sharedSecret + newline );
        strb.append( header + NETWORKIDAUTHENTICATIONURLIDENTIFIER + "=" + networkIdAuthenticationURL + newline );
        strb.append( header + RSAACTIVEIDENTIFIER + "=" + rsaActive + newline );
        strb.append( header + RSAAGENTPATHIDENTIFIER + "=" + rsaAgentPath + newline );
        strb.append( header + PREAUTHGATEWAYSERVICEIDENTIFIER + "=" + preAuthGatewayService + newline );
        strb.append( header + LOGINEXPIRATIONINMINUTESIDENTIFIER + "=" + loginExpirationInMinutesString + newline );
        strb.append( header + NETWORKIDAUTHENTICATIONTIMEOUTINMSIDENTIFIER + "=" + networkIdAuthenticationTimeoutInMsString + newline + newline );

        FileSystem fs = new FileSystem();
        String fileLocation = fs.convertFileSystemPath( AUTHGATEWAYAPPPROPERTIES );
        fs.writeStringToFile( strb.toString() , fileLocation );
    }

    protected String getHTMLForm()
    {
        StringBuilder strb = new StringBuilder();
        String newline = System.getProperty( "line.separator" );
        if( this.authgatewayService1 != null )
        {
            strb.append( "<h2>Service 1</h2>" + newline );
            strb.append( buildHTMLForm( this.authgatewayService1 ) );
        }
        return strb.toString();
    }

    // TODO: Multi AD Config
    private String buildHTMLForm( AuthgatewayService service )
    {
        StringBuilder strb = new StringBuilder();
        if( service != null )
        {
            String newline = "<br /><br />" + System.getProperty( "line.separator" );
            strb.append( "<input type='hidden' name='" + AuthgatewayService.PARAMACTIVE + "' value='on'" + newline );
            // strb.append( "<label>Active: </label><input name='" + PARAMACTIVE + "' type='checkbox' ");
            // if( active ){ strb.append( " checked='checked' " ); }
            // strb.append( " />" + newline );
            strb.append( service.getHTMLForm() );

        }else
        {
            strb.append( "ERROR: Unable to retrieve data from " + service );
        }
        return strb.toString();
    }

    protected String getHTMLFormOnSubmitFunctionCall()
    {
        String functionCall = " onsubmit='return validateFormOnSubmit()' ";
        return functionCall;
    }

    protected String getValidateFormOnSubmit()
    {
        return ( AuthgatewayService.getValidateFormOnSubmit() );
    }

    // TODO: gets the current settings, then only overrides one specific service
    protected void saveService( HashMap <String , String> parameterMap ) throws IllegalArgumentException , InstantiationException , IOException
    {
        if( parameterMap.size() != 9 ) // magic number?
        {
            throw new IllegalArgumentException( parameterMap.size() + " is the wrong number of Parameters" );
        }

        String activeString = parameterMap.get( AuthgatewayService.PARAMACTIVE );
        boolean active = false;
        if( activeString != null && activeString.equals( "on" ) )
        {
            active = true;
        }else
        {
            active = false;
        }
        String host = parameterMap.get( AuthgatewayService.PARAMHOST );
        String portString = parameterMap.get( AuthgatewayService.PARAMPORT );
        int port = Integer.parseInt( portString );
        boolean ssl = false;
        if( port == 636 )
        {
            ssl = true;
        }

        String baseDNString = parameterMap.get( AuthgatewayService.PARAMBASEDN );
        LdapName baseDN;
        try
        {
            baseDN = new LdapName( baseDNString );
        }catch( InvalidNameException e )
        {
            throw new IllegalArgumentException( "ERROR: baseDN " + baseDNString + " is an invalid ldap name: " + e.getMessage() );
        }

        String userDNString = parameterMap.get( AuthgatewayService.PARAMUSERDN );
        LdapName userDN;
        try
        {
            userDN = new LdapName( userDNString );
        }catch( InvalidNameException e )
        {
            throw new IllegalArgumentException( "ERROR: userDN " + userDNString + " is an invalid ldap name: " + e.getMessage() );
        }

        String userDNPassword = parameterMap.get( AuthgatewayService.PARAMUSERDNPASSWORD );
        String searchBaseString = parameterMap.get( AuthgatewayService.PARAMSEARCHBASE );
        LdapName searchBase;
        try
        {
            searchBase = new LdapName( searchBaseString );
        }catch( InvalidNameException e )
        {
            throw new IllegalArgumentException( "ERROR: searchBase " + searchBaseString + " is an invalid ldap name: " + e.getMessage() );
        }

        String ldapUserAttribute = parameterMap.get( AuthgatewayService.PARAMLDAPUSERATTRIBUTE );
        String searchSubtreeString = parameterMap.get( AuthgatewayService.PARAMSEARCHSUBTREE ); // checkbox, on = true
        boolean searchSubtree = false;
        if( searchSubtreeString != null && searchSubtreeString.equals( "on" ) )
        {
            searchSubtree = true;
        }else
        {
            searchSubtree = false;
        }
        AuthgatewayService newSettings = new AuthgatewayService.Builder().active( active ).hostname( host ).port( port ).ldapSSL( ssl ).ldapBaseDn( baseDN )
                .ldapManagerUserDn( userDN ).ldapManagerPassword( userDNPassword ).ldapUserAttribute( ldapUserAttribute ).ldapUserSearchBase( searchBase )
                .ldapSearchSubtree( searchSubtree ).build();
        AuthgatewayServiceDAO authProperties = new AuthgatewayServiceDAO();
        authProperties.authgatewayService1 = newSettings;
        authProperties.writeConfigurationToFile(); // potential race condition for showing the updated settings?

    }

    protected String TestConnection( String serviceNumber ) throws IllegalArgumentException
    {
        StringBuilder strb = new StringBuilder();
        if( serviceNumber.equals( "service1" ) )
        {
            strb.append( authgatewayService1.TestConnection() );
            if( strb.toString().isEmpty() )
            {
                strb.append( "ERROR: AD/LDAP Connection Test Failed" );
            }
            if( strb.toString().contains( "java.net.SocketTimeoutException" ) )
            {
                strb.append( "<br /> ERROR: AD/LDAP Connection Test Failed, please check the connectivity to your AD/LDAP Server" );
            }
            if( strb.toString().contains( "AcceptSecurityContext error" ) )
            {
                strb.append( "<br /> ERROR: AD/LDAP Connection Test Failed, please check your username and password" );
            }

        }
        return strb.toString();
    }

    private void LoadPropertiesFromFile( String pathFile ) throws FileNotFoundException , IOException
    {
        if( pathFile == null )
        {
            throw new IllegalArgumentException( "ERROR: pathFile cannot be null" );
        }
        if( pathFile.isEmpty() )
        {
            throw new IllegalArgumentException( "ERROR: pathFile cannot be empty" );
        }
        FileInputStream fileInputStream = new FileInputStream( pathFile );
        InputStreamReader inStream = new InputStreamReader( fileInputStream );
        appProperties = new Properties();
        appProperties.load( inStream );
    }

} // end class

  • « AppProperties AuthgatewayService
  • AppProperties CIFSService »

Published

Aug 30, 2012

Category

java-servlet

~1079 words

Tags

  • appproperties 18
  • authgatewayservicedao 1
  • java-servlet 61