8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: CLILogin.java,v 1.2 2008/06/25 05:41:27 qcheng Exp $
2265cfe8ee36d40dc946cde472ecd12c61f856b2Peter Major * Portions Copyrighted 2011-2014 ForgeRock AS
2265cfe8ee36d40dc946cde472ecd12c61f856b2Peter Majorimport com.sun.identity.common.HttpURLConnectionManager;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Later more constructors can be added, as per requirements.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public CLILogin(String host, String domain, String userId, String passwd) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Retunrs true if logged in, else returns false. It is just a convenience
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // method to check the login status.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Returns the SessionId associated with this Session
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Return the SSOToken associated with this login object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Returns true if the session associated with CLILogin object is valid,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // else returns false
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Returns the output of accessing a URL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Returns the cookies associated with the login. Will be implemented later
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // logout the user
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster new com.iplanet.sso.providers.dpro.SSOProviderImpl();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Returns true if login is successful else false.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (host == null || domain == null || userId == null || passwd == null)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean loggedin = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // URL url = new URL(protocol + "://"+ host + "/login/LDAP");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster URL url = new URL(protocol + "://" + host + "/login/LDAP?goto="
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookieValue = cookie.substring(index + 1, index1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println("CookieVal = " + cookieValue);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // authenticate user using LDAP module through POST
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster url = new URL(protocol + "://" + host + "/login/LDAP");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println("===== validate session after authenticate ===");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // check if really logged in or not
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Returns true if the session associated with CLILogin object is valid,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // else returns false
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private boolean validateSession(String cookieValue) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster new com.iplanet.sso.providers.dpro.SSOProviderImpl();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ssotoken = manager.createSSOToken(cookieValue);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String getCookie(URL url) throws IOException {
2265cfe8ee36d40dc946cde472ecd12c61f856b2Peter Major HttpURLConnection connection = HttpURLConnectionManager.getConnection(url);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Get the iPlanetDirectoryPro cookie from the header, strip off
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // the cookie name, domain, and path to get the value, it must be
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // included in the next post to the auth server, the cookie anme
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // should be taken from the platform.conf
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String cookie = connection.getHeaderField("Set-cookie");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println("No cookies in HTTP request, server down ?"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static void authenticate(URL url, String cookie, String userName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String passWord) throws IOException, ProtocolException {
2265cfe8ee36d40dc946cde472ecd12c61f856b2Peter Major HttpURLConnection connection = HttpURLConnectionManager.getConnection(url);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster connection.setRequestProperty("Cookie", cookie);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // don't follow redirects since we don't care about them
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // and the auth will do a final redirect after successful
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // authentication. Instead we will just check the session
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // to see if auth suceeded.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PrintWriter out = new PrintWriter(connection.getOutputStream());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster out.print("TOKEN0=" + userName + "&TOKEN1=" + passWord);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Must get the input stream in order to complete the post even
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // though we don't care about the response.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster new BufferedReader(new InputStreamReader(connection.getInputStream()));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String urlAccess(URL url, String cookie, boolean print)
2265cfe8ee36d40dc946cde472ecd12c61f856b2Peter Major HttpURLConnection connection = HttpURLConnectionManager.getConnection(url);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster connection.setRequestProperty("Cookie", cookie);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // read output from server
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster BufferedReader in = new BufferedReader(new InputStreamReader(connection
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ((len = in.read(buf, 0, buf.length)) != -1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String convertCookie(String cookie) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // System.out.println("cookie=" + cookie);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (true) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String newCookie = cookie.substring(0, temp) + "%"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // System.out.println("cookie=" + newCookie);