/*
 * Copyright 2010, Red Hat, Inc. and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package net.openl10n.flies.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.security.auth.login.LoginException;
import javax.sql.DataSource;
import javax.transaction.SystemException;
import javax.transaction.Transaction;

import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.log.LogProvider;
import org.jboss.seam.log.Logging;
import org.jboss.security.auth.spi.DatabaseServerLoginModule;

public class FliesNukesLoginModule extends DatabaseServerLoginModule
{
   private static final LogProvider log = Logging.getLogProvider(FliesNukesLoginModule.class);
   public static final String AUTHENTICATED_NUKE_USER = "authenticated.nuke.user";
   public boolean validatePassword(String inputPassword, String expectedPassword)
   {
      inputPassword = toHexString(md5(inputPassword));
      boolean result = super.validatePassword(inputPassword, expectedPassword);
      return result;
   }

    public boolean login() throws LoginException
    {
    boolean success = super.login();
    getUserInfo();
    return success;
   
    }

   public static class NukeUser
   {
      public String username;
      public String password;
      public String email;
      public String name;

      public NukeUser(String username, String password, String email, String name)
      {
         this.username = username;
         this.password = password;
         this.email = email;
         this.name = name;
      }
   }


   public void getUserInfo()
    {
      Context context;
      Transaction tx = null;
      ResultSet rs = null;
      Connection con = null;
      DataSource ds = null;
      PreparedStatement ps = null;
      if (suspendResume)
      {
         try
         {
            if (tm == null)
               throw new IllegalStateException("Transaction Manager is null");
            tx = tm.suspend();
         }
         catch (SystemException e)
         {
            throw new RuntimeException(e);
         }
      }

    try
    {
         context = new InitialContext();
         ds = (DataSource) context.lookup(dsJndiName);
         con = ds.getConnection();
         String sqlQuery = "SELECT * FROM nuke_users WHERE pn_uname = ?";
         ps = con.prepareStatement(sqlQuery);
         ps.setString(1, getUsername());

         rs = ps.executeQuery();
         if (rs.next())
         {
            if (Contexts.isSessionContextActive())
            {
               Contexts.getSessionContext().set(AUTHENTICATED_NUKE_USER, new NukeUser(rs.getString("pn_uname"), rs.getString("pn_pass"), rs.getString("pn_email"), rs.getString("pn_name")));
            }
         }
    }
    catch (NamingException e)
    {
         log.warn(e.getMessage());
    }
    catch (SQLException e)
    {
         log.warn(e.getMessage());
    }
      finally
      {
         if (rs != null)
         {
            try
            {
               rs.close();
            }
            catch (SQLException e)
            {
            }
         }
         if (ps != null)
         {
            try
            {
               ps.close();
            }
            catch (SQLException e)
            {
            }
         }
         if (con != null)
         {
            try
            {
               con.close();
            }
            catch (SQLException ex)
            {
            }
         }
         if (suspendResume)
         {
            // TransactionDemarcationSupport.resumeAnyTransaction(tx);
            try
            {
               tm.resume(tx);
            }
            catch (Exception e)
            {
               throw new RuntimeException(e);
            }
         }
      }
    }


   private String toHexString(byte[] bytes)
   {
      if (bytes == null)
      {
         throw new IllegalArgumentException("byte array must not be null");
      }
      StringBuffer hex = new StringBuffer(bytes.length * 2);
      for (byte aByte : bytes)
      {
         hex.append(Character.forDigit((aByte & 0XF0) >> 4, 16));
         hex.append(Character.forDigit((aByte & 0X0F), 16));
      }
      return hex.toString();
   }


   private byte[] md5(String text)
   {
      // arguments check
      if (text == null)
      {
         throw new NullPointerException("null text");
      }

      try
      {
         MessageDigest md = MessageDigest.getInstance("MD5");
         md.update(text.getBytes());
         return md.digest();
      }
      catch (NoSuchAlgorithmException e)
      {
         throw new RuntimeException("Cannot find MD5 algorithm");
      }
   }

}

@@ -1107,493 +1107,46 @@
 java
-.sql.Connection;%0Aimport java.sql.PreparedStatement;%0Aimport java.sql.ResultSet;%0Aimport java.sql.SQLException;%0A%0Aimport javax.naming.Context;%0Aimport javax.naming.InitialContext;%0Aimport javax.naming.NamingException;%0Aimport javax.security.auth.login.LoginException;%0Aimport javax.sql.DataSource;%0Aimport javax.transaction.SystemException;%0Aimport javax.transaction.Transaction;%0A%0Aimport org.jboss.seam.contexts.Contexts;%0Aimport org.jboss.seam.log.LogProvider;%0Aimport org.jboss.seam.log.Logging;
+x.security.auth.login.LoginException;%0A
 %0Aimp
@@ -1278,16 +1278,19 @@
 ule%0A%7B%0A  
+ //
  private
@@ -1320,16 +1320,22 @@
 er log =
+%0A   //
  Logging
@@ -1808,27 +1808,8 @@
 ();%0A
-    getUserInfo();%0A
     
@@ -2222,2224 +2222,8 @@
 %7D%0A%0A%0A
-   public void getUserInfo()%0A    %7B%0A      Context context;%0A      Transaction tx = null;%0A      ResultSet rs = null;%0A      Connection con = null;%0A      DataSource ds = null;%0A      PreparedStatement ps = null;%0A      if (suspendResume)%0A      %7B%0A         try%0A         %7B%0A            if (tm == null)%0A               throw new IllegalStateException(%22Transaction Manager is null%22);%0A            tx = tm.suspend();%0A         %7D%0A         catch (SystemException e)%0A         %7B%0A            throw new RuntimeException(e);%0A         %7D%0A      %7D%0A%0A    try%0A    %7B%0A         context = new InitialContext();%0A         ds = (DataSource) context.lookup(dsJndiName);%0A         con = ds.getConnection();%0A         String sqlQuery = %22SELECT * FROM nuke_users WHERE pn_uname = ?%22;%0A         ps = con.prepareStatement(sqlQuery);%0A         ps.setString(1, getUsername());%0A%0A         rs = ps.executeQuery();%0A         if (rs.next())%0A         %7B%0A            if (Contexts.isSessionContextActive())%0A            %7B%0A               Contexts.getSessionContext().set(AUTHENTICATED_NUKE_USER, new NukeUser(rs.getString(%22pn_uname%22), rs.getString(%22pn_pass%22), rs.getString(%22pn_email%22), rs.getString(%22pn_name%22)));%0A            %7D%0A         %7D%0A    %7D%0A    catch (NamingException e)%0A    %7B%0A         log.warn(e.getMessage());%0A    %7D%0A    catch (SQLException e)%0A    %7B%0A         log.warn(e.getMessage());%0A    %7D%0A      finally%0A      %7B%0A         if (rs != null)%0A         %7B%0A            try%0A            %7B%0A               rs.close();%0A            %7D%0A            catch (SQLException e)%0A            %7B%0A            %7D%0A         %7D%0A         if (ps != null)%0A         %7B%0A            try%0A            %7B%0A               ps.close();%0A            %7D%0A            catch (SQLException e)%0A            %7B%0A            %7D%0A         %7D%0A         if (con != null)%0A         %7B%0A            try%0A            %7B%0A               con.close();%0A            %7D%0A            catch (SQLException ex)%0A            %7B%0A            %7D%0A         %7D%0A         if (suspendResume)%0A         %7B%0A            // TransactionDemarcationSupport.resumeAnyTransaction(tx);%0A            try%0A            %7B%0A               tm.resume(tx);%0A            %7D%0A            catch (Exception e)%0A            %7B%0A               throw new RuntimeException(e);%0A            %7D%0A         %7D%0A      %7D%0A    %7D%0A
 %0A%0A  

remove email info from nukes login in module