package eu.dnetlib.oauth.authenticate;

import eu.dnetlib.oauth.store.CredentialsStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.surfnet.oaaas.auth.principal.AuthenticatedPrincipal;

/**
 * Created by  Eri   on 13/2/2015.
 */
public class Authenticator {

    private CredentialsStore LDAPStore;
    private CredentialsStore DBStore;

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    public AuthenticatedPrincipal doAuthenticate(String username, String password) throws Exception {
        log.info("Authenticating against multiple stores..");

        AuthenticatedPrincipal principal = null;
        principal = DBStore.authenticate(username, password);

        if (principal == null) {
            log.error("User not found in database ");
            principal = LDAPStore.authenticate(username, password);
        }

        if (principal == null) {
            log.error("User not found ");
            throw new Exception("User not found ");
        }

        log.info("User successfully Logged in ");

        return principal;
    }

    public CredentialsStore getLDAPStore() {
        return LDAPStore;
    }

    public void setLDAPStore(CredentialsStore LDAPStore) {
        this.LDAPStore = LDAPStore;
    }

    public CredentialsStore getDBStore() {
        return DBStore;
    }

    public void setDBStore(CredentialsStore DBStore) {
        this.DBStore = DBStore;
    }
}
