summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/util/ShibbolethECP.java
blob: 2bc9494f707e06394ed41668ec014e490248ccb6 (plain) (tree)
1
2
3
4
5
6
7
8
9

             

                                   
 
                               
 

                                                          



                            


                                         
                                                                                   
 





                                         
                     




                                                                                                           
                 
                              

         








                                                                                      

                                                                                              


                                                                                                  
                                                   







                                                                                                                   



                                                                       



                                                                       


                                                                            
                     


                                                                                          
                 

                                                                                            
                                     









                                                                                     

         
package util;

import java.net.URI;
import java.net.URISyntaxException;

import org.apache.log4j.Logger;

import edu.kit.scc.dei.ecplean.ECPAuthenticationException;
import edu.kit.scc.dei.ecplean.ECPAuthenticator;


public class ShibbolethECP {

	/**
	 * Logger instance for this class
	 */
	private final static Logger LOGGER = Logger.getLogger(ShibbolethECP.class);

	/**
	 * Static URI to the SP.
	 */
	private final static URI BWLP_SP;
	static {
		URI tmp;
		try {
			tmp = new URI("https://bwlp-masterserver.ruf.uni-freiburg.de/secure-all/test.php");
		} catch (URISyntaxException e) {
			// should never happen!
			LOGGER.error("Bad URI syntax of the service provider, see trace: ", e);
			tmp = null;
		}
		BWLP_SP = tmp;
	}

	/**
	 * Fetches the resource
	 *
	 * @param idpUrl
	 *            URL of the identity provider to authenticate against, as String.
	 * @param user
	 *            Username as String.
	 * @param pass
	 *            Password as String.
	 * @return
	 * 						true if login worked, false otherwise.
	 */
	public static Boolean doLogin(final String idpUrl, final String user, final String pass) {

		// first lets do some sanity checks
		if (BWLP_SP == null) {
			LOGGER.error("URI to service provider is not set. Check the initialization of 'BWLP_SP'.");
			return false;
		}
		if (idpUrl == null) {
			LOGGER.error("Identity provider is not set, did you initialize this class correctly?");
			return false;
		}
		if (user == null) {
			LOGGER.error("No username given, aborting...");
			return false;
		}
		if (pass == null) {
			LOGGER.error("No password given, aborting...");
			return false;
		}

		// now init the authenticator for that idp and our static sp
    ECPAuthenticator auth = null;
		try {
			auth = new ECPAuthenticator(user, pass, new URI(idpUrl), BWLP_SP);
		} catch (URISyntaxException e) {
			LOGGER.error("Bad URI syntax, see trace: ", e);
		}
    if (auth == null) {
			LOGGER.error("Initialising ECP authentication failed, aborting...");
			return false;
    }
    try {
			auth.authenticate();
    } catch (ECPAuthenticationException e) {
			LOGGER.error("ECP Authentication Exception, see trace: ", e);
			return false;
    }
    // TODO: here we will need to parse the answer accordingly.
    // no errors, meaning everything worked fine.
    return true;
	}
}