1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.sso;
18  
19  import org.apache.commons.httpclient.Cookie;
20  import org.apache.commons.httpclient.Header;
21  import org.apache.commons.httpclient.HttpClient;
22  import org.apache.commons.httpclient.HttpConnection;
23  import org.apache.commons.httpclient.HttpState;
24  import org.apache.commons.httpclient.UsernamePasswordCredentials;
25  import org.apache.commons.httpclient.auth.AuthScheme;
26  import org.apache.commons.httpclient.auth.HttpAuthenticator;
27  import org.apache.commons.httpclient.methods.GetMethod;
28  
29  import junit.framework.Test;
30  import junit.framework.TestCase;
31  import junit.framework.TestSuite;
32  
33  
34  /***
35   * TestBasicSSO
36   * 
37   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
38   * @version $Id: TestBasicSSO.java 517719 2007-03-13 15:05:48Z ate $
39   */
40  public class TestBasicSSO extends TestCase
41  {
42      public static Test suite()
43      {
44          return new TestSuite(TestBasicSSO.class);
45      }
46  
47      public void testBasicSSO() throws Exception
48      {
49          System.out.println("Testing SSO");                                      
50          // connect("http://localhost:8080/demo/sso-basic", "tomcat", "tomcat");
51      }
52      
53      public void connect(String server, String username, String password) throws Exception
54      {
55          HttpClient client = new HttpClient();
56          UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
57          StringBuffer authenticationHeader = new StringBuffer("BASIC");
58          authenticationHeader.append(" realm=\"");
59          authenticationHeader.append("jetspeed");
60          authenticationHeader.append("\"");
61          Header[] headers = { new Header("WWW-Authenticate", authenticationHeader.toString())};
62          AuthScheme scheme = null;
63  
64          client.getState().setCredentials(null, null, credentials);
65          GetMethod get = new GetMethod(server);
66          // post = new MultipartPostMethod(server);
67          get.setDoAuthentication(true);
68  
69          try
70          {
71              scheme = HttpAuthenticator.selectAuthScheme(headers);
72              HttpConnection conn = client.getHttpConnectionManager().getConnection(get.getHostConfiguration());
73              boolean authenticated = HttpAuthenticator.authenticate(scheme, get, conn, client.getState());
74              if (!authenticated)
75              {
76                  throw new Exception("Failed to create authentication headers to HTTP Connection");
77              }
78              client.executeMethod(get);
79              System.out.println("response = [" + get.getResponseBodyAsString() + "]");
80  
81              Cookie[] cookies = client.getState().getCookies();
82              Cookie mycookie = null;
83              // Display the cookies
84              System.out.println("Present cookies: ");
85              for (int i = 0; i < cookies.length; i++) 
86              {
87                  System.out.println(" - " + cookies[i].toExternalForm());
88                  mycookie = cookies[i];
89              }
90             // get.releaseConnection();
91             // client.endSession();
92              
93              HttpState initialState = new HttpState();            
94              initialState.addCookie(mycookie);
95              // client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
96              client = new HttpClient();
97              client.setState(initialState);
98              get = new GetMethod(server);
99              
100             client.executeMethod(get);
101             System.out.println("response = [" + get.getResponseBodyAsString() + "]");
102 
103             cookies = client.getState().getCookies();
104             // Display the cookies
105             System.out.println("Present cookies: ");
106             for (int i = 0; i < cookies.length; i++) 
107             {
108                 System.out.println(" - " + cookies[i].toExternalForm());
109             }
110             get.releaseConnection();
111         }
112         catch (Throwable t)
113         {
114             throw new Exception("Unexpected exception in creating HTTP authentication headers", t);
115         }
116     }
117     
118 }