· 9 years ago · Feb 20, 2017, 03:02 PM
1public class MainApplication {
2
3 private static User userLoggedIn;
4 private static Device deviceLoggedIn;
5 private static PrivateKey privateKey;
6 private static AbePrivateKey secretKey;
7
8 public static void main(String[] args) {
9 // TODO Auto-generated method stub
10 Scanner scanner = new Scanner(System.in);
11// CpabeDAO.em.getTransaction().begin();
12
13
14 System.out.println("Welcome to CPABE Cloud");
15
16
17
18 int test = 0;
19 while (true){
20 switch(test){
21
22
23 case 0:
24 System.out.println("Please choose the option by typing the option number:");
25 System.out.println("(1) Login");
26 System.out.println("(2) Register User");
27 System.out.println("(3) Exit");
28 int input = scanner.nextInt();
29 if(input < 0 || input > 3){
30 System.out.println("Unrecognized input.");
31 test = 0;
32 }
33 else{
34 test=input;
35 }
36 break;
37 case 1:
38 System.out.println("Login");
39 List<User> userRetrieve = login();
40 //check if it is successfully logged in
41 boolean loggedIn = (userRetrieve.size()==0);
42 if(!loggedIn){
43 for(User user : userRetrieve){
44 userLoggedIn = user;
45 }
46 //Retrieve private key & secret key from cpabe dir, if it is not stored then register new device. After that go to case 4;
47
48
49
50
51
52
53 test = 4;
54 }
55 else{
56 System.out.println("Your username or password is incorrect. Please choose what to do next:");
57 System.out.println("(1) Login");
58 System.out.println("(2) Back to main menu");
59 int input2 = scanner.nextInt();
60 if(input2 == 1){
61 test = 1;
62 }
63 else if(input2 == 2){
64 test = 0;
65 }
66 }
67 break;
68 case 2:
69 System.out.println("Register User");
70
71 userLoggedIn = UserRegistration.registerUser();
72 DeviceRegistration.registerNewDeviceforNewUser(userLoggedIn);
73
74
75 test = scanner.nextInt();
76 break;
77 case 3:
78 System.out.println("Exit");
79 System.out.println("Press 0 to go back to menu");
80 test = scanner.nextInt();
81 break;
82 case 4:
83 System.out.println("Welcome " + userLoggedIn.getFirstname());
84 System.out.println("Please select the option by typing the option number:");
85 break;
86 default :
87 System.out.println("Invalid input. Please insert the correct input.");
88 test=0;
89 }
90 }
91}
92
93
94 private static List<User> login(){
95 Scanner scanner = new Scanner(System.in);
96 System.out.println("Please enter your username:");
97 String username = scanner.nextLine();
98 System.out.println("Please enter your password:");
99 String password = scanner.nextLine();
100 String hashPassword = Encryption.generateHash(password.getBytes());
101 Query q = CpabeDAO.em.createQuery("SELECT u FROM User u WHERE u.username = '" + username + "' and u.hashPassword = '" + hashPassword + "'");
102 List<User> userRetrieve = q.getResultList();
103 scanner.close();
104 return userRetrieve;
105
106 }