xSweeTs

Password

May 4th, 2016
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.24 KB | None | 0 0
  1. /*
  2.  * This program is free software: you can redistribute it and/or modify it under
  3.  * the terms of the GNU General Public License as published by the Free Software
  4.  * Foundation, either version 3 of the License, or (at your option) any later
  5.  * version.
  6.  *
  7.  * This program is distributed in the hope that it will be useful, but WITHOUT
  8.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9.  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10.  * details.
  11.  *
  12.  * You should have received a copy of the GNU General Public License along with
  13.  * this program. If not, see <http://www.gnu.org/licenses/>.
  14.  */
  15. package net.sf.l2j.gameserver.model.actor.instance;
  16.  
  17. import java.security.MessageDigest;
  18. import java.sql.Connection;
  19. import java.sql.PreparedStatement;
  20. import java.util.Base64;
  21. import java.util.StringTokenizer;
  22.  
  23. import net.sf.l2j.commons.concurrent.ThreadPool;
  24.  
  25. import net.sf.l2j.L2DatabaseFactory;
  26. import net.sf.l2j.gameserver.model.actor.template.NpcTemplate;
  27. import net.sf.l2j.gameserver.network.SystemMessageId;
  28. import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  29. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  30.  
  31. /**
  32.  * @author SweeTs
  33.  */
  34. public class L2PasswordInstance extends L2NpcInstance
  35. {
  36.     public L2PasswordInstance(int objectId, NpcTemplate template)
  37.     {
  38.         super(objectId, template);
  39.     }
  40.    
  41.     @Override
  42.     public void onBypassFeedback(L2PcInstance player, String command)
  43.     {
  44.         if (command.startsWith("change_password"))
  45.         {
  46.             StringTokenizer st = new StringTokenizer(command);
  47.             st.nextToken();
  48.            
  49.             String newPass = "";
  50.             String repeatNewPass = "";
  51.            
  52.             try
  53.             {
  54.                 if (st.hasMoreTokens())
  55.                 {
  56.                     newPass = st.nextToken();
  57.                     repeatNewPass = st.nextToken();
  58.                 }
  59.             }
  60.             catch (Exception e)
  61.             {
  62.                 player.sendMessage("Please fill all the blanks before requesting for a password change.");
  63.                 return;
  64.             }
  65.            
  66.             if (!conditions(newPass, repeatNewPass, player))
  67.                 return;
  68.            
  69.             changePassword(newPass, repeatNewPass, player);
  70.         }
  71.     }
  72.    
  73.     private static boolean conditions(String newPass, String repeatNewPass, L2PcInstance player)
  74.     {
  75.         if (newPass.length() < 3)
  76.         {
  77.             player.sendMessage("The new password is too short!");
  78.             return false;
  79.         }
  80.         else if (newPass.length() > 45)
  81.         {
  82.             player.sendMessage("The new password is too long!");
  83.             return false;
  84.         }
  85.         else if (!newPass.equals(repeatNewPass))
  86.         {
  87.             player.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.PASSWORD_ENTERED_INCORRECT2));
  88.             return false;
  89.         }
  90.        
  91.         return true;
  92.     }
  93.    
  94.     @Override
  95.     public void showChatWindow(L2PcInstance activeChar)
  96.     {
  97.         final NpcHtmlMessage html = new NpcHtmlMessage(0);
  98.         final StringBuilder sb = new StringBuilder();
  99.        
  100.         sb.append("<html><title>Account Manager</title>");
  101.         sb.append("<body><center>");
  102.         sb.append("<img src=\"L2UI_CH3.herotower_deco\" width=256 height=32><br>");
  103.         sb.append("New password: <edit var=\"new\" width=100 height=15><br>");
  104.         sb.append("Repeat: <edit var=\"repeatnew\" width=100 height=15><br>");
  105.         sb.append("<img src=\"L2UI_CH3.herotower_deco\" width=256 height=32><br><br>");
  106.         sb.append("<a action=\"bypass -h npc_%objectId%_change_password $new $repeatnew\">Change password</a>");
  107.         sb.append("</center></body></html>");
  108.        
  109.         html.setHtml(sb.toString());
  110.         html.replace("%objectId%", getObjectId());
  111.         activeChar.sendPacket(html);
  112.     }
  113.    
  114.     private static void changePassword(String newPass, String repeatNewPass, L2PcInstance activeChar)
  115.     {
  116.         try (Connection con = L2DatabaseFactory.getInstance().getConnection(); PreparedStatement ps = con.prepareStatement("UPDATE accounts SET password=? WHERE login=?"))
  117.         {
  118.             byte[] newPassword = MessageDigest.getInstance("SHA").digest(newPass.getBytes("UTF-8"));
  119.            
  120.             ps.setString(1, Base64.getEncoder().encodeToString(newPassword));
  121.             ps.setString(2, activeChar.getAccountName());
  122.             ps.executeUpdate();
  123.            
  124.             activeChar.sendMessage("Congratulations! Your password has been changed. You will now be disconnected for security reasons. Please login again.");
  125.             ThreadPool.schedule(() -> activeChar.logout(false), 3000);
  126.         }
  127.         catch (Exception e)
  128.         {
  129.             _log.warning("There was an error while updating account:" + e);
  130.         }
  131.     }
  132. }
Add Comment
Please, Sign In to add comment