1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
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
91
92
93 HttpState initialState = new HttpState();
94 initialState.addCookie(mycookie);
95
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
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 }