package eu.dnetlib.enabling.ui.server.auth;

import org.springframework.beans.factory.annotation.Required;

/**
 * Handles the default user/pass configured in the property files. It authorizes the user by default.
 *
 * @author marko
 *
 */
public class AuthenticationManagerSimple implements AuthenticationManager {

	/**
	 * user name
	 */
	private String username;

	/**
	 * cleartext password.
	 */
	private String password;

	/**
	 * {@inheritDoc}
	 *
	 * @see eu.dnetlib.enabling.ui.server.auth.AuthenticationManager#authenticate(java.lang.String, java.lang.String)
	 */
	public Principal authenticate(final String login, final String pwd) {
		if (username == null || username.isEmpty())
			return null;
		if (password == null || password.isEmpty())
			return null;

		if (!username.equals(login))
			return null;
		if (!password.equals(pwd))
			return null;

		return new Principal(username, null);
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see eu.dnetlib.enabling.ui.server.auth.AuthenticationManager#authorize(eu.dnetlib.enabling.ui.server.auth.Principal)
	 */
	public boolean authorize(final Principal principal) {
		if (principal == null)
			return false;
		if (username == null || username.isEmpty())
			return false;
		if (username.equals(principal.getUserName()))
			return true;
		return false;
	}
	
	@Override
	public boolean authorize(Principal principal, String resource, String action) {
		return authorize(principal);
	}
	

	public String getUsername() {
		return username;
	}

	@Required
	public void setUsername(final String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	@Required
	public void setPassword(final String password) {
		this.password = password;
	}

	

}
