package org.apereo.cas.adaptors.generic;

import org.apache.commons.lang3.StringUtils;
import org.apereo.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler;
import org.apereo.cas.authentication.HandlerResult;
import org.apereo.cas.authentication.PreventedException;
import org.apereo.cas.authentication.UsernamePasswordCredential;
import org.springframework.core.io.Resource;

import javax.security.auth.login.AccountNotFoundException;
import javax.security.auth.login.FailedLoginException;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;

/**
 * Class designed to read data from a file in the format of USERNAME SEPARATOR
 * PASSWORD that will go line by line and look for the username. If it finds the
 * username it will compare the supplied password (first put through a
 * PasswordTranslator) that is compared to the password provided in the file. If
 * there is a match, the user is authenticated. Note that the default password
 * translator is a plaintext password translator and the default separator is
 * "::" (without quotes).
 *
 * @author Scott Battaglia
 * @author Marvin S. Addison
 * @since 3.0.0
 */
public class FileAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler {

    /** The default separator in the file. */
    private static final String DEFAULT_SEPARATOR = "::";

    /** The separator to use. */
    
    private String separator = DEFAULT_SEPARATOR;

    /** The filename to read the list of usernames from. */
    private Resource fileName;


    @Override
    protected HandlerResult authenticateUsernamePasswordInternal(final UsernamePasswordCredential credential)
            throws GeneralSecurityException, PreventedException {

        try {
            if (this.fileName == null) {
                throw new FileNotFoundException("Filename does not exist");
            }

            final String username = credential.getUsername();
            final String passwordOnRecord = getPasswordOnRecord(username);
            if (StringUtils.isBlank(passwordOnRecord)) {
                throw new AccountNotFoundException(username + " not found in backing file.");
            }
            final String password = credential.getPassword();
            if (StringUtils.equals(password, passwordOnRecord)) {
                return createHandlerResult(credential, this.principalFactory.createPrincipal(username), null);
            }
        } catch (final IOException e) {
            throw new PreventedException("IO error reading backing file", e);
        }
        throw new FailedLoginException();
    }

    /**
     * @param fileName The fileName to set.
     */

    public void setFileName(final Resource fileName) {
        this.fileName = fileName;
    }

    /**
     * @param separator The separator to set.
     */
    public void setSeparator(final String separator) {
        this.separator = separator;
    }

    /**
     * Gets the password on record.
     *
     * @param username the username
     * @return the password on record
     * @throws IOException Signals that an I/O exception has occurred.
     */
    private String getPasswordOnRecord(final String username) throws IOException {

        try (BufferedReader bufferedReader =
                     new BufferedReader(new InputStreamReader(this.fileName.getInputStream(), Charset.defaultCharset()))) {
            String line = bufferedReader.readLine();
            while (line != null) {
                final String[] lineFields = line.split(this.separator);
                final String userOnRecord = lineFields[0];
                final String passOnRecord = lineFields[1];
                if (username.equals(userOnRecord)) {
                    return passOnRecord;
                }
                line = bufferedReader.readLine();
            }
        }
        return null;
    }
}

@@ -84,108 +84,8 @@
 ls;%0A
-import org.apereo.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler;%0A
 impo
@@ -132,16 +132,16 @@
 Result;%0A
+
 import o
@@ -254,16 +254,116 @@
 ential;%0A
+import org.apereo.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler;%0A
 import o
@@ -518,39 +518,8 @@
 on;%0A
-import java.io.BufferedReader;%0A
 impo
@@ -596,61 +596,22 @@
 ava.
+n
 io.
-InputStreamReader;%0Aimport java.nio.charset.Charset
+file.Files
 ;%0Aim
@@ -3286,33 +3286,32 @@
 Exception %7B%0A
-%0A
         
 try (Buffere
@@ -3302,300 +3302,84 @@
     
-try (BufferedReader bufferedReader =%0A                     new BufferedReader(new InputStreamReader(this.fileName.getInputStream(), Charset.defaultCharset()))) %7B%0A            String line = bufferedReader.readLine();%0A            while (line != null) %7B%0A                final String%5B%5D lineFields =
+return Files.lines(fileName.getFile().toPath())%0A                .map(line -%3E
  lin
@@ -3401,17 +3401,17 @@
 parator)
-;
+)
 %0A       
@@ -3423,51 +3423,36 @@
     
+.
 fi
-nal String userOnRecord = lineFields%5B0%5D;%0A
+lter(lineFields -%3E %7B%0A    
     
@@ -3476,20 +3476,20 @@
  String 
-pass
+user
 OnRecord
@@ -3506,9 +3506,9 @@
 lds%5B
-1
+0
 %5D;%0A 
@@ -3522,20 +3522,27 @@
         
-if (
+    return 
 username
@@ -3566,51 +3566,8 @@
 ord)
-) %7B%0A                    return passOnRecord
 ;%0A  
@@ -3573,32 +3573,33 @@
                %7D
+)
 %0A               
@@ -3603,55 +3603,70 @@
     
-line = bufferedReader.readLine();%0A            %7D
+.map(lineFields -%3E lineFields%5B1%5D)%0A                .findFirst()
 %0A   
@@ -3670,26 +3670,24 @@
         
-%7D%0A
         
 return n
@@ -3682,19 +3682,21 @@
     
-return 
+.orElse(
 null
+)
 ;%0A  

Read file the Java 8 way (#2177)