Advertisement
Guest User

Untitled

a guest
May 7th, 2017
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.87 KB | None | 0 0
  1. package com.ruseps.world.content;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. import com.messagebird.MessageBirdClient;
  10. import com.messagebird.MessageBirdService;
  11. import com.messagebird.MessageBirdServiceImpl;
  12. import com.ruseps.model.input.Input;
  13. import com.ruseps.util.CaseInsensitiveHashMap;
  14. import com.ruseps.util.MathUtils;
  15. import com.ruseps.world.content.dialogue.Dialogue;
  16. import com.ruseps.world.content.dialogue.DialogueExpression;
  17. import com.ruseps.world.content.dialogue.DialogueManager;
  18. import com.ruseps.world.content.dialogue.DialogueType;
  19. import com.ruseps.world.entity.impl.player.Player;
  20.  
  21. /**
  22. *
  23. * @author Nick Hartskeerl <apachenick@hotmail.com>
  24. *
  25. */
  26. public class TwoFactorAuth {
  27.  
  28. /**
  29. * The name of the message's original, this is
  30. * limited to a maximum of 11 characters.
  31. */
  32. public static final String ORIGINATOR = "Simplicity";
  33.  
  34. /**
  35. *
  36. */
  37. public static final CaseInsensitiveHashMap<TwoFactorEntry> WHITE_LIST = new CaseInsensitiveHashMap<>();
  38.  
  39. /**
  40. *
  41. */
  42. private static MessageBirdService service;
  43.  
  44. /**
  45. *
  46. */
  47. private static MessageBirdClient client;
  48.  
  49. /**
  50. *
  51. */
  52. private int pinCode;
  53.  
  54. /**
  55. *
  56. */
  57. private BigInteger phoneNumber;
  58.  
  59. /**
  60. *
  61. */
  62. private boolean verified;
  63.  
  64. /**
  65. *
  66. */
  67. private TwoFactorStage stage = TwoFactorStage.DEFAULT;
  68.  
  69. /**
  70. *
  71. */
  72. private Player player;
  73.  
  74. /**
  75. *
  76. */
  77. private int tempPinCode;
  78.  
  79. /**
  80. *
  81. */
  82. private BigInteger tempPhoneNumber;
  83.  
  84. /**
  85. *
  86. */
  87. private int attempts = 0;
  88.  
  89. /**
  90. *
  91. */
  92. private long lastAttempt;
  93.  
  94. /**
  95. *
  96. * @param player
  97. */
  98. public TwoFactorAuth(Player player) {
  99. setPlayer(player);
  100. }
  101.  
  102. /**
  103. *
  104. * @param args
  105. */
  106. public static void main(String[] args) throws Exception {
  107. initialize();
  108. sendMessage(new BigInteger("31611753515"), "Test");
  109. }
  110.  
  111. /**
  112. *
  113. */
  114. public static void initialize() {
  115. setService(new MessageBirdServiceImpl("ieq83kdpa6lyhG7mubmi3T6nU"));
  116. setClient(new MessageBirdClient(getService()));
  117. }
  118.  
  119. /**
  120. *
  121. * @param phone
  122. * @param message
  123. * @throws Exception
  124. */
  125. public static void sendMessage(BigInteger phone, String message) throws Exception {
  126.  
  127. List<BigInteger> phones = new ArrayList<>(1);
  128.  
  129. phones.add(phone);
  130.  
  131. sendMessage(phones, message);
  132.  
  133. }
  134.  
  135. /**
  136. *
  137. * @param phones
  138. * @param message
  139. * @throws Exception
  140. */
  141. public static void sendMessage(List<BigInteger> phones, String message) throws Exception {
  142.  
  143. if(getClient() == null) {
  144. throw new RuntimeException("Invalid client instance");
  145. }
  146.  
  147. getClient().sendMessage(ORIGINATOR, message, phones);
  148.  
  149. }
  150.  
  151. /**
  152. *
  153. * @param player
  154. */
  155. public static void whitelist(Player player) {
  156.  
  157. TwoFactorEntry entry = WHITE_LIST.get(player.getUsername());
  158.  
  159. if(entry != null) {
  160. entry.setIpAddress(player.getHostAddress());
  161. return;
  162. }
  163.  
  164. String ipAddress = player.getHostAddress();
  165. long timestamp = System.currentTimeMillis();
  166.  
  167. entry = new TwoFactorEntry(ipAddress, timestamp);
  168.  
  169. WHITE_LIST.put(player.getUsername(), entry);
  170.  
  171. }
  172.  
  173. /**
  174. *
  175. * @param player
  176. * @param host
  177. * @return
  178. */
  179. public static boolean isWhitelisted(Player player, String host) {
  180.  
  181. TwoFactorEntry entry = WHITE_LIST.get(player.getUsername());
  182.  
  183. if(entry == null) {
  184. return false;
  185. }
  186.  
  187. if(entry.getIpAddress().equals(host) &&
  188. System.currentTimeMillis() - entry.getTimestamp() <= 43200000 * 5) {
  189. return true;
  190. }
  191.  
  192. return false;
  193.  
  194. }
  195.  
  196. /**
  197. *
  198. */
  199. public void generatePin(boolean temp) {
  200.  
  201. int random = 1000 + MathUtils.random(8000);
  202.  
  203. if(temp) {
  204. setTempPinCode(random);
  205. } else {
  206. setPinCode(random);
  207. }
  208.  
  209. }
  210.  
  211. /**
  212. *
  213. * @return
  214. */
  215. public boolean sendPin() {
  216. return sendPin(false);
  217. }
  218.  
  219. /**
  220. *
  221. * @return
  222. */
  223. public boolean sendPin(boolean temp) {
  224.  
  225. int pinCode = temp ? getTempPinCode() : getPinCode();
  226.  
  227. if(pinCode < 1000 || pinCode > 9999) {
  228. generatePin(temp);
  229. pinCode = temp ? getTempPinCode() : getPinCode();
  230. }
  231.  
  232. StringBuilder builder = new StringBuilder();
  233.  
  234. builder.append("Your Simplicity RSPS authentication code is ");
  235. builder.append(pinCode);
  236. builder.append(".");
  237.  
  238. return sendMessage(builder.toString(), temp);
  239.  
  240. }
  241.  
  242. /**
  243. *
  244. * @param message
  245. * @return
  246. */
  247. public boolean sendMessage(String message) {
  248. return sendMessage(message, false);
  249. }
  250.  
  251. /**
  252. *
  253. * @param message
  254. * @param temp
  255. * @return
  256. */
  257. public boolean sendMessage(String message, boolean temp) {
  258.  
  259. BigInteger phoneNumber = temp ? getTempPhoneNumber() : getPhoneNumber();
  260.  
  261. if(phoneNumber == null) {
  262. return false;
  263. }
  264.  
  265. try {
  266.  
  267. sendMessage(phoneNumber, message);
  268.  
  269. return true;
  270.  
  271. } catch (Exception e) {
  272. e.printStackTrace();
  273. }
  274.  
  275. return false;
  276.  
  277. }
  278.  
  279. /**
  280. *
  281. */
  282. public void showOptions() {
  283. DialogueManager.start(getPlayer(), new TwoFactorOptions(getPlayer()));
  284. }
  285.  
  286. /**
  287. *
  288. * @return
  289. */
  290. public BigInteger getPhoneNumber() {
  291. return phoneNumber;
  292. }
  293.  
  294. /**
  295. *
  296. * @param phoneNumber
  297. */
  298. public void setPhoneNumber(BigInteger phoneNumber) {
  299. this.phoneNumber = phoneNumber;
  300. }
  301.  
  302. /**
  303. *
  304. * @return
  305. */
  306. public static MessageBirdService getService() {
  307. return service;
  308. }
  309.  
  310. /**
  311. *
  312. * @param service
  313. */
  314. public static void setService(MessageBirdService service) {
  315. TwoFactorAuth.service = service;
  316. }
  317.  
  318. /**
  319. *
  320. * @return
  321. */
  322. public static MessageBirdClient getClient() {
  323. return client;
  324. }
  325.  
  326. /**
  327. *
  328. * @param client
  329. */
  330. public static void setClient(MessageBirdClient client) {
  331. TwoFactorAuth.client = client;
  332. }
  333.  
  334. /**
  335. *
  336. * @return
  337. */
  338. public int getPinCode() {
  339. return pinCode;
  340. }
  341.  
  342. /**
  343. *
  344. * @param pinCode
  345. */
  346. public void setPinCode(int pinCode) {
  347. this.pinCode = pinCode;
  348. }
  349.  
  350. /**
  351. *
  352. * @return
  353. */
  354. public boolean isVerified() {
  355. return verified;
  356. }
  357.  
  358. /**
  359. *
  360. * @param verified
  361. */
  362. public void setVerified(boolean verified) {
  363. this.verified = verified;
  364. }
  365.  
  366. /**
  367. *
  368. * @return
  369. */
  370. public TwoFactorStage getStage() {
  371. return stage;
  372. }
  373.  
  374. /**
  375. *
  376. * @param stage
  377. */
  378. public void setStage(TwoFactorStage stage) {
  379. this.stage = stage;
  380. }
  381.  
  382. /**
  383. *
  384. * @return
  385. */
  386. public Player getPlayer() {
  387. return player;
  388. }
  389.  
  390. /**
  391. *
  392. * @param player
  393. */
  394. public void setPlayer(Player player) {
  395. this.player = player;
  396. }
  397.  
  398. /**
  399. *
  400. * @return
  401. */
  402. public BigInteger getTempPhoneNumber() {
  403. return tempPhoneNumber;
  404. }
  405.  
  406. /**
  407. *
  408. * @param tempPhoneNumber
  409. */
  410. public void setTempPhoneNumber(BigInteger tempPhoneNumber) {
  411. this.tempPhoneNumber = tempPhoneNumber;
  412. }
  413.  
  414. /**
  415. *
  416. * @return
  417. */
  418. public int getTempPinCode() {
  419. return tempPinCode;
  420. }
  421.  
  422. /**
  423. *
  424. * @param tempPinCode
  425. */
  426. public void setTempPinCode(int tempPinCode) {
  427. this.tempPinCode = tempPinCode;
  428. }
  429.  
  430. /**
  431. *
  432. * @return
  433. */
  434. public int getAttempts() {
  435. return attempts;
  436. }
  437.  
  438. /**
  439. *
  440. * @param attempts
  441. */
  442. public void setAttempts(int attempts) {
  443. this.attempts = attempts;
  444. }
  445.  
  446. /**
  447. *
  448. * @return
  449. */
  450. public long getLastAttempt() {
  451. return lastAttempt;
  452. }
  453.  
  454. /**
  455. *
  456. * @param lastAttempt
  457. */
  458. public void setLastAttempt(long lastAttempt) {
  459. this.lastAttempt = lastAttempt;
  460. }
  461.  
  462. /**
  463. *
  464. * @author Nick Hartskeerl <apachenick@hotmail.com>
  465. *
  466. */
  467. public static enum TwoFactorStage {
  468.  
  469. DEFAULT,
  470.  
  471. SENT_PIN
  472.  
  473. }
  474.  
  475. /**
  476. *
  477. * @author Nick Hartskeerl <apachenick@hotmail.com>
  478. *
  479. */
  480. public static class TwoFactorOptions extends Dialogue {
  481.  
  482. /**
  483. *
  484. */
  485. private Player player;
  486.  
  487. /**
  488. *
  489. */
  490. public TwoFactorOptions() {
  491. /*
  492. * Empty
  493. */
  494. }
  495.  
  496. /**
  497. *
  498. * @param player
  499. */
  500. public TwoFactorOptions(Player player) {
  501. setPlayer(player);
  502. }
  503.  
  504. @Override
  505. public DialogueType type() {
  506. return DialogueType.OPTION;
  507. }
  508.  
  509. @Override
  510. public DialogueExpression animation() {
  511. return null;
  512. }
  513.  
  514. @Override
  515. public String[] dialogue() {
  516.  
  517. if(getPlayer().getTwoFactorAuth().isVerified()) {
  518.  
  519. return new String[] {
  520. "Change phone number",
  521. "Disable two factor authentication",
  522. "Cancel"
  523. };
  524.  
  525. }
  526.  
  527. List<String> dialogue = new ArrayList<>();
  528.  
  529. dialogue.add("Set up two factor authentication");
  530.  
  531. if(getPlayer().getTwoFactorAuth().getStage() == TwoFactorStage.SENT_PIN) {
  532. dialogue.add("Enter received PIN code");
  533. }
  534.  
  535. dialogue.add("Cancel");
  536.  
  537. return dialogue.toArray(new String[] { });
  538.  
  539. }
  540.  
  541. @Override
  542. public void specialAction() {
  543. getPlayer().setDialogueActionId(!getPlayer().getTwoFactorAuth().isVerified() ? 420 : 421);
  544. }
  545.  
  546. /**
  547. *
  548. * @return
  549. */
  550. public Player getPlayer() {
  551. return player;
  552. }
  553.  
  554. /**
  555. *
  556. * @param player
  557. */
  558. public void setPlayer(Player player) {
  559. this.player = player;
  560. }
  561.  
  562. }
  563.  
  564. /**
  565. *
  566. * @author Nick Hartskeerl <apachenick@hotmail.com>
  567. *
  568. */
  569. public static class TwoFactorDialogue extends Dialogue {
  570.  
  571. /**
  572. *
  573. */
  574. public static TwoFactorDialogue[] DIALOGUES = {
  575.  
  576. statement(1, "Enter a valid phone number with international notation. Ex: (1)800.."),
  577.  
  578. action(2, new TwoFactorAction() {
  579.  
  580. @Override
  581. public void handle() {
  582.  
  583. getPlayer().getPacketSender().sendInterfaceRemoval();
  584.  
  585. getPlayer().setInputHandling(new Input() {
  586.  
  587. @Override
  588. public void handleSyntax(Player player, String text) {
  589.  
  590. player.getPacketSender().sendInterfaceRemoval();
  591.  
  592. final String input = text.replaceAll("[^0-9]", "");
  593.  
  594. String regex = "^\\+(?:[0-9] ?){6,14}[0-9]$";
  595. Pattern pattern = Pattern.compile(regex);
  596. Matcher matcher = pattern.matcher("+"+input);
  597.  
  598. if(player.getTwoFactorAuth().getAttempts() >= 3 && (System.currentTimeMillis() - player.getTwoFactorAuth().getLastAttempt()) < 300000) {
  599. DialogueManager.start(player, statement(-1, "You have reached the maximum attempts of 3 within 15 minutes."));
  600. } else if(matcher.matches()) {
  601.  
  602. BigInteger phoneNumber = new BigInteger(input);
  603.  
  604. DialogueManager.start(player, statement(3, "Sending authentication code as SMS to +"+input+"."));
  605.  
  606. new Thread(new Runnable() {
  607.  
  608. @Override
  609. public void run() {
  610.  
  611. try {
  612. Thread.sleep(600);
  613. } catch (InterruptedException e) {
  614. e.printStackTrace();
  615. }
  616.  
  617. player.getTwoFactorAuth().setAttempts(0);
  618. player.getTwoFactorAuth().setTempPhoneNumber(phoneNumber);
  619. player.getTwoFactorAuth().sendPin(true);
  620. player.getTwoFactorAuth().setStage(TwoFactorStage.SENT_PIN);
  621.  
  622. DialogueManager.start(player, statement(2, "An authentication SMS code has been sent to +"+input+"."));
  623.  
  624. }
  625.  
  626. }).start();
  627.  
  628. } else {
  629. player.getTwoFactorAuth().setAttempts(player.getTwoFactorAuth().getAttempts() + 1);
  630. player.getTwoFactorAuth().setLastAttempt(System.currentTimeMillis());
  631. DialogueManager.start(player, statement(-1, "The phone number you entered was invalid."));
  632. }
  633.  
  634. }
  635.  
  636. });
  637.  
  638. getPlayer().getPacketSender().sendEnterInputPrompt("Enter your mobile phone number:");
  639.  
  640. }
  641.  
  642. }),
  643.  
  644. action(3, new TwoFactorAction() {
  645.  
  646. @Override
  647. public void handle() {
  648.  
  649. getPlayer().getPacketSender().sendInterfaceRemoval();
  650.  
  651. getPlayer().setInputHandling(new Input() {
  652.  
  653. @Override
  654. public void handleAmount(Player player, int number) {
  655.  
  656. player.getPacketSender().sendInterfaceRemoval();
  657.  
  658. int pinCode = player.getTwoFactorAuth().getTempPinCode();
  659.  
  660. if(number == pinCode) {
  661. player.getTwoFactorAuth().setTempPinCode(-1);
  662. player.getTwoFactorAuth().setPhoneNumber(new BigInteger(player.getTwoFactorAuth().getTempPhoneNumber().toString()));
  663. player.getTwoFactorAuth().setVerified(true);
  664. player.getTwoFactorAuth().setAttempts(0);
  665. DialogueManager.start(player, statement(3, "You have successfully set up two factor authentication."));
  666. } else {
  667. DialogueManager.start(player, statement(2, "The authentication PIN code you entered was invalid. Try again."));
  668. }
  669.  
  670. }
  671.  
  672. });
  673.  
  674. getPlayer().getPacketSender().sendEnterAmountPrompt("Enter the authentication you received:");
  675.  
  676. }
  677.  
  678. }),
  679.  
  680. action(-1, new TwoFactorAction() {
  681.  
  682. @Override
  683. public void handle() {
  684. getPlayer().getTwoFactorAuth().showOptions();
  685. }
  686.  
  687. }),
  688.  
  689. action(-1, new TwoFactorAction() {
  690.  
  691. @Override
  692. public void handle() {
  693. player.getTwoFactorAuth().setVerified(false);
  694. WHITE_LIST.remove(player.getUsername());
  695. DialogueManager.start(player, statement(-1, "Two factor authentication has been disabled on your account."));
  696. }
  697.  
  698. }),
  699.  
  700. };
  701.  
  702. /**
  703. *
  704. */
  705. private DialogueType type;
  706.  
  707. /**
  708. *
  709. */
  710. private DialogueExpression expression;
  711.  
  712. /**
  713. *
  714. */
  715. private String[] dialogue;
  716.  
  717. /**
  718. *
  719. */
  720. private int nextDialogue;
  721.  
  722. /**
  723. *
  724. */
  725. private Player player;
  726.  
  727. /**
  728. *
  729. */
  730. private TwoFactorAction action;
  731.  
  732. /**
  733. *
  734. * @param nextDialogue
  735. */
  736. public TwoFactorDialogue(int nextDialogue) {
  737. this(null, nextDialogue);
  738. }
  739.  
  740. /**
  741. *
  742. * @param type
  743. * @param nextDialogue
  744. * @param dialogue
  745. */
  746. public TwoFactorDialogue(DialogueType type, int nextDialogue, String... dialogue) {
  747. this(type, null, nextDialogue, dialogue);
  748. }
  749.  
  750. /**
  751. *
  752. * @param type
  753. * @param expression
  754. * @param nextDialogue
  755. * @param dialogue
  756. */
  757. public TwoFactorDialogue(DialogueType type, DialogueExpression expression, int nextDialogue, String... dialogue) {
  758. this.type = type;
  759. this.expression = expression;
  760. this.nextDialogue = nextDialogue;
  761. this.dialogue = dialogue;
  762. }
  763.  
  764. /**
  765. *
  766. * @param player
  767. * @param dialogue
  768. */
  769. public static void start(Player player, int dialogue) {
  770. DialogueManager.start(player, DIALOGUES[dialogue]);
  771. }
  772.  
  773. /**
  774. *
  775. * @param next
  776. * @param expression
  777. * @param dialogue
  778. * @return
  779. */
  780. public static TwoFactorDialogue player(int next, String... dialogue) {
  781. return player(next, DialogueExpression.NORMAL, dialogue);
  782. }
  783.  
  784. /**
  785. *
  786. * @param next
  787. * @param expression
  788. * @param dialogue
  789. * @return
  790. */
  791. public static TwoFactorDialogue player(int next, DialogueExpression expression, String... dialogue) {
  792. return new TwoFactorDialogue(DialogueType.PLAYER_STATEMENT, expression, next, dialogue);
  793. }
  794.  
  795. /**
  796. *
  797. * @param next
  798. * @param dialogue
  799. * @return
  800. */
  801. public static TwoFactorDialogue statement(int next, String... dialogue) {
  802. return new TwoFactorDialogue(DialogueType.STATEMENT, next, dialogue);
  803. }
  804.  
  805. /**
  806. *
  807. * @param next
  808. * @return
  809. */
  810. public static TwoFactorDialogue action(int next, TwoFactorAction action) {
  811.  
  812. TwoFactorDialogue dialogue = new TwoFactorDialogue(next) {
  813.  
  814. @Override
  815. public void specialAction(Player player) {
  816. if(getAction() != null) {
  817. getAction().setPlayer(player);
  818. getAction().handle();
  819. }
  820. }
  821.  
  822. };
  823.  
  824. dialogue.setAction(action);
  825.  
  826. return dialogue;
  827.  
  828. }
  829.  
  830. @Override
  831. public DialogueType type() {
  832. return type;
  833. }
  834.  
  835. @Override
  836. public DialogueExpression animation() {
  837. return expression;
  838. }
  839.  
  840. @Override
  841. public String[] dialogue() {
  842. return dialogue;
  843. }
  844.  
  845. @Override
  846. public Dialogue nextDialogue() {
  847.  
  848. if(nextDialogue == -1) {
  849. return null;
  850. }
  851.  
  852. return DIALOGUES[nextDialogue];
  853.  
  854. }
  855.  
  856. /**
  857. *
  858. * @return
  859. */
  860. public Player getPlayer() {
  861. return player;
  862. }
  863.  
  864. /**
  865. *
  866. * @param player
  867. */
  868. public void setPlayer(Player player) {
  869. this.player = player;
  870. }
  871.  
  872. /**
  873. *
  874. * @return
  875. */
  876. public TwoFactorAction getAction() {
  877. return action;
  878. }
  879.  
  880. /**
  881. *
  882. * @param action
  883. */
  884. public void setAction(TwoFactorAction action) {
  885. this.action = action;
  886. }
  887.  
  888. }
  889.  
  890. /**
  891. *
  892. * @author Nick Hartskeerl <apachenick@hotmail.com>
  893. *
  894. */
  895. public static abstract class TwoFactorAction {
  896.  
  897. /**
  898. *
  899. */
  900. protected Player player;
  901.  
  902. /**
  903. *
  904. */
  905. public abstract void handle();
  906.  
  907. /**
  908. *
  909. * @return
  910. */
  911. public Player getPlayer() {
  912. return player;
  913. }
  914.  
  915. /**
  916. *
  917. * @param player
  918. */
  919. public void setPlayer(Player player) {
  920. this.player = player;
  921. }
  922.  
  923. }
  924.  
  925. /**
  926. *
  927. * @author Nick Hartskeerl <apachenick@hotmail.com>
  928. *
  929. */
  930. public static class TwoFactorEntry {
  931.  
  932. /**
  933. *
  934. */
  935. private String ipAddress;
  936.  
  937. /**
  938. *
  939. */
  940. private long timestamp;
  941.  
  942. /**
  943. *
  944. * @param ipAddress
  945. * @param timestamp
  946. */
  947. public TwoFactorEntry(String ipAddress, long timestamp) {
  948. setIpAddress(ipAddress);
  949. setTimestamp(timestamp);
  950. }
  951.  
  952. /**
  953. *
  954. * @return
  955. */
  956. public String getIpAddress() {
  957. return ipAddress;
  958. }
  959.  
  960. /**
  961. *
  962. * @param ipAddress
  963. */
  964. public void setIpAddress(String ipAddress) {
  965. this.ipAddress = ipAddress;
  966. }
  967.  
  968. /**
  969. *
  970. * @return
  971. */
  972. public long getTimestamp() {
  973. return timestamp;
  974. }
  975.  
  976. /**
  977. *
  978. * @param timestamp
  979. */
  980. public void setTimestamp(long timestamp) {
  981. this.timestamp = timestamp;
  982. }
  983.  
  984. }
  985.  
  986. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement