Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ruseps.world.content;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import com.messagebird.MessageBirdClient;
- import com.messagebird.MessageBirdService;
- import com.messagebird.MessageBirdServiceImpl;
- import com.ruseps.model.input.Input;
- import com.ruseps.util.CaseInsensitiveHashMap;
- import com.ruseps.util.MathUtils;
- import com.ruseps.world.content.dialogue.Dialogue;
- import com.ruseps.world.content.dialogue.DialogueExpression;
- import com.ruseps.world.content.dialogue.DialogueManager;
- import com.ruseps.world.content.dialogue.DialogueType;
- import com.ruseps.world.entity.impl.player.Player;
- /**
- *
- * @author Nick Hartskeerl <apachenick@hotmail.com>
- *
- */
- public class TwoFactorAuth {
- /**
- * The name of the message's original, this is
- * limited to a maximum of 11 characters.
- */
- public static final String ORIGINATOR = "Simplicity";
- /**
- *
- */
- public static final CaseInsensitiveHashMap<TwoFactorEntry> WHITE_LIST = new CaseInsensitiveHashMap<>();
- /**
- *
- */
- private static MessageBirdService service;
- /**
- *
- */
- private static MessageBirdClient client;
- /**
- *
- */
- private int pinCode;
- /**
- *
- */
- private BigInteger phoneNumber;
- /**
- *
- */
- private boolean verified;
- /**
- *
- */
- private TwoFactorStage stage = TwoFactorStage.DEFAULT;
- /**
- *
- */
- private Player player;
- /**
- *
- */
- private int tempPinCode;
- /**
- *
- */
- private BigInteger tempPhoneNumber;
- /**
- *
- */
- private int attempts = 0;
- /**
- *
- */
- private long lastAttempt;
- /**
- *
- * @param player
- */
- public TwoFactorAuth(Player player) {
- setPlayer(player);
- }
- /**
- *
- * @param args
- */
- public static void main(String[] args) throws Exception {
- initialize();
- sendMessage(new BigInteger("31611753515"), "Test");
- }
- /**
- *
- */
- public static void initialize() {
- setService(new MessageBirdServiceImpl("ieq83kdpa6lyhG7mubmi3T6nU"));
- setClient(new MessageBirdClient(getService()));
- }
- /**
- *
- * @param phone
- * @param message
- * @throws Exception
- */
- public static void sendMessage(BigInteger phone, String message) throws Exception {
- List<BigInteger> phones = new ArrayList<>(1);
- phones.add(phone);
- sendMessage(phones, message);
- }
- /**
- *
- * @param phones
- * @param message
- * @throws Exception
- */
- public static void sendMessage(List<BigInteger> phones, String message) throws Exception {
- if(getClient() == null) {
- throw new RuntimeException("Invalid client instance");
- }
- getClient().sendMessage(ORIGINATOR, message, phones);
- }
- /**
- *
- * @param player
- */
- public static void whitelist(Player player) {
- TwoFactorEntry entry = WHITE_LIST.get(player.getUsername());
- if(entry != null) {
- entry.setIpAddress(player.getHostAddress());
- return;
- }
- String ipAddress = player.getHostAddress();
- long timestamp = System.currentTimeMillis();
- entry = new TwoFactorEntry(ipAddress, timestamp);
- WHITE_LIST.put(player.getUsername(), entry);
- }
- /**
- *
- * @param player
- * @param host
- * @return
- */
- public static boolean isWhitelisted(Player player, String host) {
- TwoFactorEntry entry = WHITE_LIST.get(player.getUsername());
- if(entry == null) {
- return false;
- }
- if(entry.getIpAddress().equals(host) &&
- System.currentTimeMillis() - entry.getTimestamp() <= 43200000 * 5) {
- return true;
- }
- return false;
- }
- /**
- *
- */
- public void generatePin(boolean temp) {
- int random = 1000 + MathUtils.random(8000);
- if(temp) {
- setTempPinCode(random);
- } else {
- setPinCode(random);
- }
- }
- /**
- *
- * @return
- */
- public boolean sendPin() {
- return sendPin(false);
- }
- /**
- *
- * @return
- */
- public boolean sendPin(boolean temp) {
- int pinCode = temp ? getTempPinCode() : getPinCode();
- if(pinCode < 1000 || pinCode > 9999) {
- generatePin(temp);
- pinCode = temp ? getTempPinCode() : getPinCode();
- }
- StringBuilder builder = new StringBuilder();
- builder.append("Your Simplicity RSPS authentication code is ");
- builder.append(pinCode);
- builder.append(".");
- return sendMessage(builder.toString(), temp);
- }
- /**
- *
- * @param message
- * @return
- */
- public boolean sendMessage(String message) {
- return sendMessage(message, false);
- }
- /**
- *
- * @param message
- * @param temp
- * @return
- */
- public boolean sendMessage(String message, boolean temp) {
- BigInteger phoneNumber = temp ? getTempPhoneNumber() : getPhoneNumber();
- if(phoneNumber == null) {
- return false;
- }
- try {
- sendMessage(phoneNumber, message);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
- /**
- *
- */
- public void showOptions() {
- DialogueManager.start(getPlayer(), new TwoFactorOptions(getPlayer()));
- }
- /**
- *
- * @return
- */
- public BigInteger getPhoneNumber() {
- return phoneNumber;
- }
- /**
- *
- * @param phoneNumber
- */
- public void setPhoneNumber(BigInteger phoneNumber) {
- this.phoneNumber = phoneNumber;
- }
- /**
- *
- * @return
- */
- public static MessageBirdService getService() {
- return service;
- }
- /**
- *
- * @param service
- */
- public static void setService(MessageBirdService service) {
- TwoFactorAuth.service = service;
- }
- /**
- *
- * @return
- */
- public static MessageBirdClient getClient() {
- return client;
- }
- /**
- *
- * @param client
- */
- public static void setClient(MessageBirdClient client) {
- TwoFactorAuth.client = client;
- }
- /**
- *
- * @return
- */
- public int getPinCode() {
- return pinCode;
- }
- /**
- *
- * @param pinCode
- */
- public void setPinCode(int pinCode) {
- this.pinCode = pinCode;
- }
- /**
- *
- * @return
- */
- public boolean isVerified() {
- return verified;
- }
- /**
- *
- * @param verified
- */
- public void setVerified(boolean verified) {
- this.verified = verified;
- }
- /**
- *
- * @return
- */
- public TwoFactorStage getStage() {
- return stage;
- }
- /**
- *
- * @param stage
- */
- public void setStage(TwoFactorStage stage) {
- this.stage = stage;
- }
- /**
- *
- * @return
- */
- public Player getPlayer() {
- return player;
- }
- /**
- *
- * @param player
- */
- public void setPlayer(Player player) {
- this.player = player;
- }
- /**
- *
- * @return
- */
- public BigInteger getTempPhoneNumber() {
- return tempPhoneNumber;
- }
- /**
- *
- * @param tempPhoneNumber
- */
- public void setTempPhoneNumber(BigInteger tempPhoneNumber) {
- this.tempPhoneNumber = tempPhoneNumber;
- }
- /**
- *
- * @return
- */
- public int getTempPinCode() {
- return tempPinCode;
- }
- /**
- *
- * @param tempPinCode
- */
- public void setTempPinCode(int tempPinCode) {
- this.tempPinCode = tempPinCode;
- }
- /**
- *
- * @return
- */
- public int getAttempts() {
- return attempts;
- }
- /**
- *
- * @param attempts
- */
- public void setAttempts(int attempts) {
- this.attempts = attempts;
- }
- /**
- *
- * @return
- */
- public long getLastAttempt() {
- return lastAttempt;
- }
- /**
- *
- * @param lastAttempt
- */
- public void setLastAttempt(long lastAttempt) {
- this.lastAttempt = lastAttempt;
- }
- /**
- *
- * @author Nick Hartskeerl <apachenick@hotmail.com>
- *
- */
- public static enum TwoFactorStage {
- DEFAULT,
- SENT_PIN
- }
- /**
- *
- * @author Nick Hartskeerl <apachenick@hotmail.com>
- *
- */
- public static class TwoFactorOptions extends Dialogue {
- /**
- *
- */
- private Player player;
- /**
- *
- */
- public TwoFactorOptions() {
- /*
- * Empty
- */
- }
- /**
- *
- * @param player
- */
- public TwoFactorOptions(Player player) {
- setPlayer(player);
- }
- @Override
- public DialogueType type() {
- return DialogueType.OPTION;
- }
- @Override
- public DialogueExpression animation() {
- return null;
- }
- @Override
- public String[] dialogue() {
- if(getPlayer().getTwoFactorAuth().isVerified()) {
- return new String[] {
- "Change phone number",
- "Disable two factor authentication",
- "Cancel"
- };
- }
- List<String> dialogue = new ArrayList<>();
- dialogue.add("Set up two factor authentication");
- if(getPlayer().getTwoFactorAuth().getStage() == TwoFactorStage.SENT_PIN) {
- dialogue.add("Enter received PIN code");
- }
- dialogue.add("Cancel");
- return dialogue.toArray(new String[] { });
- }
- @Override
- public void specialAction() {
- getPlayer().setDialogueActionId(!getPlayer().getTwoFactorAuth().isVerified() ? 420 : 421);
- }
- /**
- *
- * @return
- */
- public Player getPlayer() {
- return player;
- }
- /**
- *
- * @param player
- */
- public void setPlayer(Player player) {
- this.player = player;
- }
- }
- /**
- *
- * @author Nick Hartskeerl <apachenick@hotmail.com>
- *
- */
- public static class TwoFactorDialogue extends Dialogue {
- /**
- *
- */
- public static TwoFactorDialogue[] DIALOGUES = {
- statement(1, "Enter a valid phone number with international notation. Ex: (1)800.."),
- action(2, new TwoFactorAction() {
- @Override
- public void handle() {
- getPlayer().getPacketSender().sendInterfaceRemoval();
- getPlayer().setInputHandling(new Input() {
- @Override
- public void handleSyntax(Player player, String text) {
- player.getPacketSender().sendInterfaceRemoval();
- final String input = text.replaceAll("[^0-9]", "");
- String regex = "^\\+(?:[0-9] ?){6,14}[0-9]$";
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher("+"+input);
- if(player.getTwoFactorAuth().getAttempts() >= 3 && (System.currentTimeMillis() - player.getTwoFactorAuth().getLastAttempt()) < 300000) {
- DialogueManager.start(player, statement(-1, "You have reached the maximum attempts of 3 within 15 minutes."));
- } else if(matcher.matches()) {
- BigInteger phoneNumber = new BigInteger(input);
- DialogueManager.start(player, statement(3, "Sending authentication code as SMS to +"+input+"."));
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- Thread.sleep(600);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- player.getTwoFactorAuth().setAttempts(0);
- player.getTwoFactorAuth().setTempPhoneNumber(phoneNumber);
- player.getTwoFactorAuth().sendPin(true);
- player.getTwoFactorAuth().setStage(TwoFactorStage.SENT_PIN);
- DialogueManager.start(player, statement(2, "An authentication SMS code has been sent to +"+input+"."));
- }
- }).start();
- } else {
- player.getTwoFactorAuth().setAttempts(player.getTwoFactorAuth().getAttempts() + 1);
- player.getTwoFactorAuth().setLastAttempt(System.currentTimeMillis());
- DialogueManager.start(player, statement(-1, "The phone number you entered was invalid."));
- }
- }
- });
- getPlayer().getPacketSender().sendEnterInputPrompt("Enter your mobile phone number:");
- }
- }),
- action(3, new TwoFactorAction() {
- @Override
- public void handle() {
- getPlayer().getPacketSender().sendInterfaceRemoval();
- getPlayer().setInputHandling(new Input() {
- @Override
- public void handleAmount(Player player, int number) {
- player.getPacketSender().sendInterfaceRemoval();
- int pinCode = player.getTwoFactorAuth().getTempPinCode();
- if(number == pinCode) {
- player.getTwoFactorAuth().setTempPinCode(-1);
- player.getTwoFactorAuth().setPhoneNumber(new BigInteger(player.getTwoFactorAuth().getTempPhoneNumber().toString()));
- player.getTwoFactorAuth().setVerified(true);
- player.getTwoFactorAuth().setAttempts(0);
- DialogueManager.start(player, statement(3, "You have successfully set up two factor authentication."));
- } else {
- DialogueManager.start(player, statement(2, "The authentication PIN code you entered was invalid. Try again."));
- }
- }
- });
- getPlayer().getPacketSender().sendEnterAmountPrompt("Enter the authentication you received:");
- }
- }),
- action(-1, new TwoFactorAction() {
- @Override
- public void handle() {
- getPlayer().getTwoFactorAuth().showOptions();
- }
- }),
- action(-1, new TwoFactorAction() {
- @Override
- public void handle() {
- player.getTwoFactorAuth().setVerified(false);
- WHITE_LIST.remove(player.getUsername());
- DialogueManager.start(player, statement(-1, "Two factor authentication has been disabled on your account."));
- }
- }),
- };
- /**
- *
- */
- private DialogueType type;
- /**
- *
- */
- private DialogueExpression expression;
- /**
- *
- */
- private String[] dialogue;
- /**
- *
- */
- private int nextDialogue;
- /**
- *
- */
- private Player player;
- /**
- *
- */
- private TwoFactorAction action;
- /**
- *
- * @param nextDialogue
- */
- public TwoFactorDialogue(int nextDialogue) {
- this(null, nextDialogue);
- }
- /**
- *
- * @param type
- * @param nextDialogue
- * @param dialogue
- */
- public TwoFactorDialogue(DialogueType type, int nextDialogue, String... dialogue) {
- this(type, null, nextDialogue, dialogue);
- }
- /**
- *
- * @param type
- * @param expression
- * @param nextDialogue
- * @param dialogue
- */
- public TwoFactorDialogue(DialogueType type, DialogueExpression expression, int nextDialogue, String... dialogue) {
- this.type = type;
- this.expression = expression;
- this.nextDialogue = nextDialogue;
- this.dialogue = dialogue;
- }
- /**
- *
- * @param player
- * @param dialogue
- */
- public static void start(Player player, int dialogue) {
- DialogueManager.start(player, DIALOGUES[dialogue]);
- }
- /**
- *
- * @param next
- * @param expression
- * @param dialogue
- * @return
- */
- public static TwoFactorDialogue player(int next, String... dialogue) {
- return player(next, DialogueExpression.NORMAL, dialogue);
- }
- /**
- *
- * @param next
- * @param expression
- * @param dialogue
- * @return
- */
- public static TwoFactorDialogue player(int next, DialogueExpression expression, String... dialogue) {
- return new TwoFactorDialogue(DialogueType.PLAYER_STATEMENT, expression, next, dialogue);
- }
- /**
- *
- * @param next
- * @param dialogue
- * @return
- */
- public static TwoFactorDialogue statement(int next, String... dialogue) {
- return new TwoFactorDialogue(DialogueType.STATEMENT, next, dialogue);
- }
- /**
- *
- * @param next
- * @return
- */
- public static TwoFactorDialogue action(int next, TwoFactorAction action) {
- TwoFactorDialogue dialogue = new TwoFactorDialogue(next) {
- @Override
- public void specialAction(Player player) {
- if(getAction() != null) {
- getAction().setPlayer(player);
- getAction().handle();
- }
- }
- };
- dialogue.setAction(action);
- return dialogue;
- }
- @Override
- public DialogueType type() {
- return type;
- }
- @Override
- public DialogueExpression animation() {
- return expression;
- }
- @Override
- public String[] dialogue() {
- return dialogue;
- }
- @Override
- public Dialogue nextDialogue() {
- if(nextDialogue == -1) {
- return null;
- }
- return DIALOGUES[nextDialogue];
- }
- /**
- *
- * @return
- */
- public Player getPlayer() {
- return player;
- }
- /**
- *
- * @param player
- */
- public void setPlayer(Player player) {
- this.player = player;
- }
- /**
- *
- * @return
- */
- public TwoFactorAction getAction() {
- return action;
- }
- /**
- *
- * @param action
- */
- public void setAction(TwoFactorAction action) {
- this.action = action;
- }
- }
- /**
- *
- * @author Nick Hartskeerl <apachenick@hotmail.com>
- *
- */
- public static abstract class TwoFactorAction {
- /**
- *
- */
- protected Player player;
- /**
- *
- */
- public abstract void handle();
- /**
- *
- * @return
- */
- public Player getPlayer() {
- return player;
- }
- /**
- *
- * @param player
- */
- public void setPlayer(Player player) {
- this.player = player;
- }
- }
- /**
- *
- * @author Nick Hartskeerl <apachenick@hotmail.com>
- *
- */
- public static class TwoFactorEntry {
- /**
- *
- */
- private String ipAddress;
- /**
- *
- */
- private long timestamp;
- /**
- *
- * @param ipAddress
- * @param timestamp
- */
- public TwoFactorEntry(String ipAddress, long timestamp) {
- setIpAddress(ipAddress);
- setTimestamp(timestamp);
- }
- /**
- *
- * @return
- */
- public String getIpAddress() {
- return ipAddress;
- }
- /**
- *
- * @param ipAddress
- */
- public void setIpAddress(String ipAddress) {
- this.ipAddress = ipAddress;
- }
- /**
- *
- * @return
- */
- public long getTimestamp() {
- return timestamp;
- }
- /**
- *
- * @param timestamp
- */
- public void setTimestamp(long timestamp) {
- this.timestamp = timestamp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement