· 7 years ago · Oct 14, 2018, 01:10 AM
1package us.demipvp.hub.events;
2
3import com.warrenstrange.googleauth.GoogleAuthenticator;
4import com.warrenstrange.googleauth.GoogleAuthenticatorKey;
5import org.bukkit.entity.Player;
6import org.bukkit.event.EventHandler;
7import org.bukkit.event.Listener;
8import org.bukkit.event.player.*;
9import us.demipvp.hub.Hub;
10import us.demipvp.hub.utils.Utils;
11
12public class TwoFactorEvent implements Listener{
13
14 @EventHandler
15 public void onJoin(PlayerJoinEvent e){
16 Player p = e.getPlayer();
17 if(p.hasPermission("demipvp.staff")){
18 if(!Hub.getInstance().getConfig().contains("authcodes." + p.getUniqueId())){
19 // does not contain
20 GoogleAuthenticator gAuth = new GoogleAuthenticator();
21 GoogleAuthenticatorKey key = gAuth.createCredentials();
22 Utils.translateMSG(p, "&7Your &6Google Auth Code &7is &a" + key.getKey());
23 Utils.translateMSG(p, "&7You must enter this code in the Google Authenticator app before exiting the server.");
24
25 Hub.getInstance().getConfig().set("authcodes." + p.getUniqueId(), key.getKey());
26 Hub.getInstance().saveConfig();
27 }else{
28 // contains
29 Utils.authlock.add(p.getUniqueId());
30 Utils.translateMSG(p, "&cPlease open the Google Authenticator app and provide the six digit code in order to be authorized");
31 }
32 }
33 }
34
35 @EventHandler
36 public void onChat(AsyncPlayerChatEvent e){
37 Player p = e.getPlayer();
38 String msg = e.getMessage();
39
40 if(Utils.authlock.contains(p.getUniqueId())){
41 try{
42 Integer code = Integer.parseInt(msg);
43 if(authenticateInput(p, code)){
44 Utils.authlock.remove(p.getUniqueId());
45 Utils.translateMSG(p, "&aYou have been authorized.");
46 }else{
47 Utils.translateMSG(p, "&cIncorrect or expired code (The code will only contain numbers)");
48 }
49 }catch(Exception ex){
50 Utils.translateMSG(p, "&cIncorrect or expired code (The code will only contain numbers)");
51 }
52 }
53 }
54
55// @EventHandler
56// public void onCommand(PlayerCommandPreprocessEvent e){
57// if(Utils.authlock.contains(e.getPlayer().getUniqueId())){
58// e.setCancelled(true);
59// Utils.translateMSG(e.getPlayer(), "&cYou must be authorized first.");
60// Utils.translateMSG(e.getPlayer(), "&cType your 6 digit code in chat");
61// }
62// }
63//
64// @EventHandler
65// public void onInteract(PlayerInteractEvent e){
66// if(Utils.authlock.contains(e.getPlayer().getUniqueId())){
67// e.setCancelled(true);
68// Utils.translateMSG(e.getPlayer(), "&cYou must be authorized first.");
69// Utils.translateMSG(e.getPlayer(), "&cType your 6 digit code in chat");
70// }
71// }
72//
73// @EventHandler
74// public void onMove(PlayerMoveEvent e){
75// if(Utils.authlock.contains(e.getPlayer().getUniqueId())){
76// e.setCancelled(true);
77// Utils.translateMSG(e.getPlayer(), "&cYou must be authorized first.");
78// Utils.translateMSG(e.getPlayer(), "&cType your 6 digit code in chat");
79// }
80// }
81//
82// @EventHandler
83// public void onBlockBreak(BlockBreakEvent e){
84// if(Utils.authlock.contains(e.getPlayer().getUniqueId())){
85// e.setCancelled(true);
86// Utils.translateMSG(e.getPlayer(), "&cYou must be authorized first.");
87// Utils.translateMSG(e.getPlayer(), "&cType your 6 digit code in chat");
88// }
89// }
90//
91// @EventHandler
92// public void onBlockPlace(BlockPlaceEvent e){
93// if(Utils.authlock.contains(e.getPlayer().getUniqueId())){
94// e.setCancelled(true);
95// Utils.translateMSG(e.getPlayer(), "&cYou must be authorized first.");
96// Utils.translateMSG(e.getPlayer(), "&cType your 6 digit code in chat");
97// }
98// }
99
100 private boolean authenticateInput(Player p, int code){
101 String secretkey = Hub.getInstance().getConfig().getString("authcodes." + p.getUniqueId());
102
103 GoogleAuthenticator gAuth = new GoogleAuthenticator();
104 boolean codeIsValid = gAuth.authorize(secretkey, code);
105
106 if(codeIsValid){
107 Utils.authlock.remove(p.getUniqueId());
108 return codeIsValid;
109 }
110 return codeIsValid;
111 }
112}