Question: I am working on a java code and here is the info on the project: For the zoo, please develop an authentication system that manages
I am working on a java code and here is the info on the project:
For the zoo, please develop an authentication system that manages both authentication and authorization. You have been given a credentials file that contains credential information for authorized users. You have also been given three files, one for each role: zookeeper, veterinarian, and admin. Each role file describes the data the particular role should be authorized to access. Create an authentication system that does all of the following: Asks the user for a username Asks the user for a password Converts the password using a message digest five (MD5) hash
It is not required that you write the MD5 from scratch. Use the code located in this document and follow the comments in it to perform this operation. Checks the credentials against the valid credentials provided in the credentials file
Use the hashed passwords in the second column; the third column contains the actual passwords for testing and the fourth row contains the role of each user. Limits failed attempts to three before notifying the user and exiting the program Gives authenticated users access to the correct role file after successful authentication
The system information stored in the role file should be displayed. For example, if a zookeepers credentials is successfully authenticated, then the contents from the zookeeper file will be displayed. If an admins credentials is successfully authenticated, then the contents from the admin file will be displayed. Allows a user to log out Stays on the credential screen until either a successful attempt has been made, three unsuccessful attempts have been made, or a user chooses to exit
Here are the five text files I was given:
admin.txt
Hello, System Admin!
As administrator, you have access to the zoo's main computer system. This allows you to monitor users in the system and their roles.
credentials.txt
griffin.keyes 108de81c31bf9c622f76876b74e9285f "alphabet soup" zookeeper rosario.dawson 3e34baa4ee2ff767af8c120a496742b5 "animal doctor" admin bernie.gorilla a584efafa8f9ea7fe5cf18442f32b07b "secret password" veterinarian donald.monkey 17b1b7d8a706696ed220bc414f729ad3 "M0nk3y business" zookeeper jerome.grizzlybear 3adea92111e6307f8f2aae4721e77900 "grizzly1234" veterinarian bruce.grizzlybear 0d107d09f5bbe40cade3de5c71e9e9b7 "letmein" admin
validateCredentials.txt
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package authentication; import java.security.MessageDigest; import java.io.FileInputStream; import java.io.IOException; import java.util.Scanner; /** * * @author JoeP */ public class ValidateCredentials { private boolean isValid; private String filePath; private String credentialsFileName; public ValidateCredentials() { isValid = false; //filePath = "C:\\Users\\joep\\Documents\\NetBeansProjects\\ Authentication\\"; filePath = ""; credentialsFileName = "credentials"; } public boolean isCredentialsValid(String userName, String passWord) throws Exception { String original = passWord; MessageDigest md = MessageDigest.getInstance("MD5"); md.update(original.getBytes()); byte[] digest = md.digest(); StringBuffer sb = new StringBuffer(); for (byte b : digest) { sb.append(String.format("%02x", b & 0xff)); } System.out.println(""); System.out.println("original:" + original); System.out.println("digested:" + sb.toString()); //sb.toString() is what you'll need to compare password strings isValid = readDataFiles(userName, sb.toString()); return isValid; } public boolean readDataFiles(String userName, String passWord) throws IOException { FileInputStream fileByteStream1 = null; // File input stream FileInputStream fileByteStream2 = null; // File input stream Scanner inFS1 = null; // Scanner object Scanner inFS2 = null; // Scanner object String textLine = null; boolean foundCredentials = false; // Try to open file System.out.println(""); System.out.println("Opening file " + credentialsFileName + ".txt"); fileByteStream1 = new FileInputStream(filePath + "credentials.txt"); inFS1 = new Scanner(fileByteStream1); System.out.println(""); System.out.println("Reading lines of text."); while (inFS1.hasNextLine()) { textLine = inFS1.nextLine(); System.out.println(textLine); if (textLine.contains(userName) && textLine.contains(passWord)) { foundCredentials = true; break; } } // Done with file, so try to close it System.out.println(""); System.out.println("Closing file " + credentialsFileName + ".txt"); if (textLine != null) { fileByteStream1.close(); // close() may throw IOException if fails } if (foundCredentials == true) { // Try to open file System.out.println(""); System.out.println("Opening file " + userName + ".txt"); fileByteStream2 = new FileInputStream(filePath + userName + ".txt"); inFS2 = new Scanner(fileByteStream2); System.out.println(""); while (inFS2.hasNextLine()) { textLine = inFS2.nextLine(); System.out.println(textLine); } // Done with file, so try to close it System.out.println(""); System.out.println("Closing file " + userName + ".txt"); if (textLine != null) { fileByteStream2.close(); // close() may throw IOException if fails } } return foundCredentials; } } veterinarian.txt
Hello, Veterinarian!
As veterinarian, you have access to all of the animals' health records. This allows you to view each animal's medical history, current treatments/illnesses (if any), and maintain a vaccination log.
****zookeeper.txt****
Hello, Zookeeper!
As zookeeper, you have access to all of the animals information and their daily monitoring logs. This allows you to track their feeding habits, habitat conditions, and general welfare.
Finally, this is the code that I have so far, but when I try to run it in Netbeans it just won't work.
import java.io.File; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; import java.security.MessageDigest; import java.util.List; import java.util.Scanner; public class Authentication { public static void main(String args[]) { try { Scanner scan = null; scan = new Scanner(new File("credentials.txt")); String credentials[][] = new String[100][4]; int count = 0; while (scan.hasNextLine()) { //read name and hased pass credentials[count][0] = scan.next(); credentials[count][1] = scan.next(); //get original pass from file String l[] = scan.nextLine().split("\"[ ]+"); l[0] = l[0].trim(); l[0] = l[0].replace("\"", ""); credentials[count][2] = l[0]; credentials[count][3] = l[1].trim(); count++; } //ask for user input Scanner scanio = new Scanner(System.in); boolean RUN = true; int tries = 0; while (RUN) { System.out.println("**WELCOME**"); System.out.println("1) Login"); System.out.println("2) Exit"); int ch = Integer.parseInt(scanio.nextLine().trim()); if (ch == 1) { //increment number of attempts tries++; //ask for user and pass System.out.print("Input username:"); String username = scanio.nextLine(); System.out.print("Input password:"); String password = scanio.nextLine(); //generate hash MessageDigest md; md = MessageDigest.getInstance("MD5"); md.update(password.getBytes()); byte[] digest = md.digest(); StringBuilder sb = new StringBuilder(); for (byte b : digest) { sb.append(String.format("%02x", b & 0xff)); } String hPassword = sb.toString(); boolean badUser = true; for (int i = 0; i < count; i++) { if (username.contentEquals(credentials[i][0])) { if (hPassword.contentEquals(credentials[i][1])) { //everything looks good. login List data = null; //check type of user and print switch (credentials[i][3]) { case "zookeeper": data = Files.readAllLines(Paths.get("zookeeper.txt"), Charset.defaultCharset()); break; case "admin": data = Files.readAllLines(Paths.get("admin.txt"), Charset.defaultCharset()); break; case "veterinarian": data = Files.readAllLines(Paths.get("veterinarian.txt"), Charset.defaultCharset()); break; default: break; } for (String s : data) { System.out.println(s); } //reset tries tries = 0; //now what to do? System.out.println(" 1) Logout."); System.out.println("2) Exit."); ch = Integer.parseInt(scanio.nextLine().trim()); if (ch == 2) { RUN = false; } badUser = false; break; } } } if (badUser) { System.out.println("Invalid Username or password."); } } else { RUN = false; break; } //thief alert!! if (tries == 3) { RUN = false; System.out.println("Too many invlaid attempts."); } } } catch (Exception ex) { ex.printStackTrace(); } } } When I run the program in NetBeans it just says "run: BUILD SUCCESSFUL (total time: 0 seconds). I'm not sure why it doesn't prompt me for a username and then password here?
Thanks for the help here!!!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
