Question: I am currently working on an assignment for my Java programming class that requires me to create an authentication program that makes use of two

I am currently working on an assignment for my Java programming class that requires me to create an authentication program that makes use of two classes simultaniously. Here is the prompt:

For security-minded professionals, it is important that only the appropriate people gain access to data in a computer system. This is called authentication. Once users gain entry, it is also important that they only see data related to their role in a computer system. This is called authorization. For the zoo, you will 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 credentialsfile. Use the hashed passwordsin 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 theprogram.

- Gives authenticated users access to the correct role file after successfulauthentication o 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

I have been given the secondary class, ValidateCredentials which is found below:

package authentication;

import java.security.MessageDigest; import java.io.FileInputStream; import java.io.IOException; import java.util.Scanner;

public class ValidateCredentials {

private boolean isValid; private String filePath; private String credentialsFileName;

public ValidateCredentials() { filePath = ""; isValid = false; 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; String textFileName = 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);

String[] words = textLine.split("\\s");//splits the string based on whitespace

if (words[0].equals(userName) && textLine.contains(passWord)) { foundCredentials = true; int last = words.length - 1; textFileName = words[last]; 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 " + textFileName + ".txt"); fileByteStream2 = new FileInputStream(filePath + textFileName + ".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 " + textFileName + ".txt");

if (textLine != null) { fileByteStream2.close(); // close() may throw IOException if fails } }

return foundCredentials; }

void isCredentialsValid() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }

}

However, I am struggling greatly with creating the primary class, Authentication. I just can't seem to get my code to work and call upon the second class. I was fairly confident going into this assignment with my coding abilities, but my brain just refuses to work this problem out so I am seeking outside help so that I can see how this code is supposed to work as I am more of a visual learner.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!