WeaverHttpAuthenticationHandler.java 5.08 KB
/*
 * Licensed to Jasig under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Jasig licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License.  You may obtain a
 * copy of the License at the following location:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package weaver.loginsso;

import org.jasig.cas.authentication.handler.AuthenticationHandler;
import org.jasig.cas.authentication.principal.Credentials;
import org.jasig.cas.authentication.principal.UsernamePasswordCredentials;
import org.jasig.cas.util.HttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import javax.validation.constraints.NotNull;

/**
 * Class to validate the credentials presented by communicating with the web
 * server and checking the certificate that is returned against the hostname,
 * etc.
 * <p>
 * This class is concerned with ensuring that the protocol is HTTPS and that a
 * response is returned. The SSL handshake that occurs automatically by opening
 * a connection does the heavy process of authenticating.
 * 
 * @author Scott Battaglia
 * @version $Revision$ $Date$
 * @since 3.0
 */
public final class WeaverHttpAuthenticationHandler implements
		AuthenticationHandler {

	/** The string representing the HTTPS protocol. */
	private static final String PROTOCOL_HTTPS = "https";

	/** Boolean variable denoting whether secure connection is required or not. */
	private boolean requireSecure = true;

	/** Log instance. */
	private final Logger log = LoggerFactory.getLogger(getClass());

    private String ts;

    private String appid;

    private String loginid;

    public String getLoginid() {
        return loginid;
    }

    public void setLoginid(String loginid) {
        this.loginid = loginid;
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }



    public String getTs() {
        return ts;
    }

    public void setTs(String ts) {
        this.ts = ts;
    }

    /** Instance of Apache Commons HttpClient */
	@NotNull
	private HttpClient httpClient;

	public boolean authenticate(final Credentials credentials) {
		final UsernamePasswordCredentials serviceCredentials = (UsernamePasswordCredentials) credentials;
		final String username = serviceCredentials.getUsername();
		String password = serviceCredentials.getPassword();
		String url = serviceCredentials.getPassword();
		String ts="";
		try {
			String[] info = AES.decrypt(password, "yjcust").split("-f-g-");
			url = info[1];
			ts=   info[0];
            setTs(ts);
        } catch (Exception e) {
			e.printStackTrace();
		}

		//System.out.println(username + " " + password + " " + url);
		String re = senddata(url, username, ts);

        if (re.indexOf("true_") >= 0) {
            String realLoginid = re.replace("true_", "");
            setLoginid(realLoginid);
            return true;
        }else{
            setLoginid("");
            return false;
        }
	}

	public String senddata(String url, String username, String ts) {
		log.info("url:" + url + " " + "username:" + username + ",ts:"
				+ ts);
		PostMethod postMethod = new PostMethod(url);
		String response = "";
		try {
			NameValuePair[] params = { new NameValuePair("username", username),
					new NameValuePair("ts", ts)

			};
			postMethod.setRequestHeader("Content-Type",
					"application/x-www-form-urlencoded;charset=utf-8");

			postMethod.setRequestBody(params);
			org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
			int statusCode = httpClient.executeMethod(postMethod);
			if (statusCode == 200) {
				response = postMethod.getResponseBodyAsString();
				log.info("response:" + response);
			} else {
				log.info("response-->http resp code:" + statusCode);
			}

		} catch (Exception e) {
			log.info("", e);

			e.printStackTrace();
		}

		return response;
	}

	/**
	 * @return true if the credentials provided are not null and the credentials
	 *         are a subclass of (or equal to) HttpBasedServiceCredentials.
	 */
	public boolean supports(final Credentials credentials) {
		return true;
	}

	/** Sets the HttpClient which will do all of the connection stuff. */
	public void setHttpClient(final HttpClient httpClient) {
		this.httpClient = httpClient;
	}

	/**
	 * Set whether a secure url is required or not.
	 * 
	 * @param requireSecure
	 *            true if its required, false if not. Default is true.
	 */
	public void setRequireSecure(final boolean requireSecure) {
		this.requireSecure = requireSecure;
	}
}