View Javadoc

1   // ////////////////////////////////////////////////////////////////////////////
2   // dexterIM - Instant Messaging Framework
3   // Copyright (C) 2003 Christoph Walcher
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or
8   // (at your option) any later version.
9   //
10  // This program is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  // GNU General Public License for more details.
14  //
15  // You should have received a copy of the GNU General Public License
16  // along with this program; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  //////////////////////////////////////////////////////////////////////////////
19  package net.sf.dexterim.msn.tweener;
20  
21  import java.io.IOException;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  import org.apache.commons.httpclient.Header;
26  import org.apache.commons.httpclient.HttpClient;
27  import org.apache.commons.httpclient.HttpException;
28  import org.apache.commons.httpclient.methods.GetMethod;
29  
30  /***
31   * @author christoph
32   *  
33   */
34  public class LoginServer {
35  
36  	private static final String URL_PREPEND = "Passport1.4 OrgVerb=GET,OrgURL=http%3A%2F%2Fmessenger%2Emsn%2Ecom";
37  
38  	private static final Pattern ticketPattern = Pattern
39  			.compile("from-PP='([a-zA-Z0-9_=//!//$//&//*//./]*)'");
40  
41  	private String password;
42  	private String challenge;
43  	private String serverUrl;
44  	private String username;
45  
46  	public LoginServer() {
47  
48  	}
49  
50  	public LoginServer(String serverUrl) {
51  		this.serverUrl = serverUrl;
52  	}
53  
54  	public String authTicket() {
55  		HttpClient client = new HttpClient();
56  		GetMethod get = new GetMethod(serverUrl);
57  
58  		get.addRequestHeader("Authorization", URL_PREPEND + ",sign-in=" + username
59  				+ ",pwd=" + password + "," + challenge);
60  		
61  		System.out.println(URL_PREPEND + ",sign-in=" + username
62  				+ ",pwd=" + password + "," + challenge);
63  		
64  		get.setFollowRedirects(false);
65  
66  		try {
67  			client.executeMethod(get);
68  
69  			Header redirect = get.getResponseHeader("Location");
70  
71  			if (redirect != null) {
72  				System.out.println("Redirect");
73  				System.out.println("Location: " + redirect.getValue());
74  
75  				this.serverUrl = redirect.getValue();
76  
77  				return authTicket();
78  			}
79  		}
80  		catch (HttpException e) {
81  			e.printStackTrace();
82  		}
83  		catch (IOException e) {
84  			e.printStackTrace();
85  		}
86  
87  		Header headers[] = get.getResponseHeaders();
88  
89  		for (int i = 0; i < headers.length; i++) {
90  			System.out.println(headers[i]);
91  		}
92  
93  		Header header = get.getResponseHeader("Authentication-Info");
94  
95  		if (header != null) {
96  			return matchTicket(header.getValue());
97  		}
98  
99  		return null;
100 	}
101 
102 	private String matchTicket(String authenticationInfo) {
103 		Matcher matcher = ticketPattern.matcher(authenticationInfo);
104 
105 		while (matcher.find()) {
106 			if (matcher.group(1) != null) {
107 				return matcher.group(1);
108 			}
109 		}
110 
111 		return null;
112 	}
113 
114 	/***
115 	 * @return Returns the challenge.
116 	 */
117 	public String getChallenge() {
118 		return challenge;
119 	}
120 
121 	/***
122 	 * @param challenge
123 	 *          The challenge to set.
124 	 */
125 	public void setChallenge(String challenge) {
126 		this.challenge = challenge;
127 	}
128 
129 	/***
130 	 * @return Returns the password.
131 	 */
132 	public String getPassword() {
133 		return password;
134 	}
135 
136 	/***
137 	 * @param password
138 	 *          The password to set.
139 	 */
140 	public void setPassword(String password) {
141 		this.password = password;
142 	}
143 
144 	/***
145 	 * @return Returns the serverUrl.
146 	 */
147 	public String getServerUrl() {
148 		return serverUrl;
149 	}
150 
151 	/***
152 	 * @param serverUrl
153 	 *          The serverUrl to set.
154 	 */
155 	public void setServerUrl(String serverUrl) {
156 		this.serverUrl = serverUrl;
157 	}
158 
159 	/***
160 	 * @return Returns the username.
161 	 */
162 	public String getUsername() {
163 		return username;
164 	}
165 
166 	/***
167 	 * @param username
168 	 *          The username to set.
169 	 */
170 	public void setUsername(String username) {
171 		this.username = username;
172 	}
173 }