SecondaryAuthData.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.data.xml.impl;
  20. import java.io.File;
  21. import java.util.HashSet;
  22. import java.util.Set;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.Node;
  25. import com.l2jserver.util.data.xml.IXmlReader;
  26. /**
  27. * Secondary Auth data.
  28. * @author NosBit
  29. */
  30. public class SecondaryAuthData implements IXmlReader
  31. {
  32. private final Set<String> _forbiddenPasswords = new HashSet<>();
  33. private boolean _enabled = false;
  34. private int _maxAttempts = 5;
  35. private int _banTime = 480;
  36. private String _recoveryLink = "";
  37. protected SecondaryAuthData()
  38. {
  39. load();
  40. }
  41. @Override
  42. public synchronized void load()
  43. {
  44. _forbiddenPasswords.clear();
  45. parseFile(new File("config/SecondaryAuth.xml"));
  46. LOGGER.info("{}: Loaded {} forbidden passwords.", getClass().getSimpleName(), _forbiddenPasswords.size());
  47. }
  48. @Override
  49. public void parseDocument(Document doc)
  50. {
  51. try
  52. {
  53. for (Node node = doc.getFirstChild(); node != null; node = node.getNextSibling())
  54. {
  55. if ("list".equalsIgnoreCase(node.getNodeName()))
  56. {
  57. for (Node list_node = node.getFirstChild(); list_node != null; list_node = list_node.getNextSibling())
  58. {
  59. if ("enabled".equalsIgnoreCase(list_node.getNodeName()))
  60. {
  61. _enabled = Boolean.parseBoolean(list_node.getTextContent());
  62. }
  63. else if ("maxAttempts".equalsIgnoreCase(list_node.getNodeName()))
  64. {
  65. _maxAttempts = Integer.parseInt(list_node.getTextContent());
  66. }
  67. else if ("banTime".equalsIgnoreCase(list_node.getNodeName()))
  68. {
  69. _banTime = Integer.parseInt(list_node.getTextContent());
  70. }
  71. else if ("recoveryLink".equalsIgnoreCase(list_node.getNodeName()))
  72. {
  73. _recoveryLink = list_node.getTextContent();
  74. }
  75. else if ("forbiddenPasswords".equalsIgnoreCase(list_node.getNodeName()))
  76. {
  77. for (Node forbiddenPasswords_node = list_node.getFirstChild(); forbiddenPasswords_node != null; forbiddenPasswords_node = forbiddenPasswords_node.getNextSibling())
  78. {
  79. if ("password".equalsIgnoreCase(forbiddenPasswords_node.getNodeName()))
  80. {
  81. _forbiddenPasswords.add(forbiddenPasswords_node.getTextContent());
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. catch (Exception e)
  90. {
  91. LOGGER.warn("Failed to load secondary auth data from xml.", e);
  92. }
  93. }
  94. public boolean isEnabled()
  95. {
  96. return _enabled;
  97. }
  98. public int getMaxAttempts()
  99. {
  100. return _maxAttempts;
  101. }
  102. public int getBanTime()
  103. {
  104. return _banTime;
  105. }
  106. public String getRecoveryLink()
  107. {
  108. return _recoveryLink;
  109. }
  110. public Set<String> getForbiddenPasswords()
  111. {
  112. return _forbiddenPasswords;
  113. }
  114. public boolean isForbiddenPassword(String password)
  115. {
  116. return _forbiddenPasswords.contains(password);
  117. }
  118. public static SecondaryAuthData getInstance()
  119. {
  120. return SingletonHolder._instance;
  121. }
  122. private static class SingletonHolder
  123. {
  124. protected static final SecondaryAuthData _instance = new SecondaryAuthData();
  125. }
  126. }