package org.apereo.cas.config;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.security.GeneralSecurityException;

import javax.security.auth.login.AccountNotFoundException;
import javax.security.auth.login.FailedLoginException;

import org.apache.commons.lang.StringUtils;
import org.apereo.cas.authentication.HandlerResult;
import org.apereo.cas.authentication.PreventedException;
import org.apereo.cas.authentication.UsernamePasswordCredential;
import org.apereo.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler;
import org.apereo.cas.authentication.principal.PrincipalFactory;
import org.apereo.cas.services.ServicesManager;
import org.springframework.core.io.Resource;

public class MyAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler 
{
	
	public static final String DEFAULT_SEPARATOR = "::";

    /** The separator to use. */
    private final String separator;

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


    public MyAuthenticationHandler(final String name, final ServicesManager servicesManager, final PrincipalFactory principalFactory,
            final Resource fileName, final String separator) {
		super(name, servicesManager, principalFactory, null);
		this.fileName = fileName;
        this.separator = separator;
		// TODO Auto-generated constructor stub
	}

    @Override
    protected HandlerResult authenticateUsernamePasswordInternal(final UsernamePasswordCredential transformedCredential, 
                                                                 final String originalPassword)
            throws GeneralSecurityException, PreventedException {
        try {
            if (this.fileName == null) {
                throw new FileNotFoundException("Filename does not exist");
            }
            final String username = transformedCredential.getUsername();
            final String passwordOnRecord = getPasswordOnRecord(username);
            if (StringUtils.isBlank(passwordOnRecord)) {
                throw new AccountNotFoundException(username + " not found in backing file.");
            }
            if (matches(originalPassword, passwordOnRecord)) {
                return createHandlerResult(transformedCredential, this.principalFactory.createPrincipal(username), null);
            }
        } catch (final IOException e) {
            throw new PreventedException("IO error reading backing file", e);
        }
        throw new FailedLoginException();
    }
    
    private String getPasswordOnRecord(final String username) throws IOException {
        return Files.lines(fileName.getFile().toPath())
                .map(line -> line.split(this.separator))
                .filter(lineFields -> {
                    final String userOnRecord = lineFields[0];
                    return username.equals(userOnRecord);
                })
                .map(lineFields -> lineFields[1])
                .findFirst()
                .orElse(null);
    }
}