2 /* -----------------------------------------------------------------------
3 * <copyright file="PithosAccount.cs" company="GRNet">
5 * Copyright 2011-2012 GRNET S.A. All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * 1. Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the following
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * The views and conclusions contained in the software and
35 * documentation are those of the authors and should not be
36 * interpreted as representing official policies, either expressed
37 * or implied, of GRNET S.A.
39 * -----------------------------------------------------------------------
44 // -----------------------------------------------------------------------
50 namespace Pithos.Client.WPF
53 using System.Collections.Generic;
57 using System.Threading.Tasks;
58 using System.Diagnostics.Contracts;
59 using System.Diagnostics;
60 using System.Net.Sockets;
63 /// TODO: Update summary.
66 /// Retrieves an account name and token from PITHOS
68 public static class PithosAccount
70 private static readonly ILog Log = LogManager.GetLogger(typeof(PithosAccount));
73 /// Asynchronously retrieves PITHOS credentials
75 /// <param name="loginUrl">URL to retrieve the account info from PITHOS. Must end with =</param>
76 /// <returns>The credentials wrapped in a Task</returns>
77 public static Task<NetworkCredential> RetrieveCredentials(string loginUrl)
79 Contract.Requires(Uri.IsWellFormedUriString(loginUrl, UriKind.Absolute));
81 if (!Uri.IsWellFormedUriString(loginUrl, UriKind.Absolute))
82 throw new ArgumentException("The pithosSite parameter must be a valid absolute URL", "loginUrl");
84 int port = GetFreePort();
86 //TODO:Force logout here to take care of existing cookies
89 var listenerUrl = String.Format("http://127.0.0.1:{0}/", port);
93 var receiveCredentials = ListenForRedirectAsync(port);
95 var logoutUrl = loginUrl.Replace("login", "im/logout");
96 var logoutProcess=Process.Start(logoutUrl);
97 TaskEx.Delay(2000).Wait();
98 var uriBuilder=new UriBuilder(loginUrl);
99 uriBuilder.Query="next=" + listenerUrl;
101 var retrieveUri = uriBuilder.Uri;
102 Log.InfoFormat("[RETRIEVE] Open Browser at {0}", retrieveUri);
103 Process.Start(retrieveUri.ToString());
105 return receiveCredentials;
109 private static async Task<NetworkCredential> ListenHttpForRedirectAsync(string listenerUrl)
111 using (var listener = new HttpListener())
113 listener.Prefixes.Add(listenerUrl);
115 Log.InfoFormat("[RETRIEVE] Listening at {0}", listenerUrl);
122 var context = await listener.GetContextAsync()
123 .WithTimeout(TimeSpan.FromMinutes(5));
125 var request = context.Request;
126 Log.InfoFormat("[RETRIEVE] Got Connection {0}", request.RawUrl);
128 var query = request.QueryString;
129 var userName = query["user"];
130 var token = query["token"];
131 if (String.IsNullOrWhiteSpace(userName) ||
132 String.IsNullOrWhiteSpace(token))
134 Respond(context, "Failure", "The server did not return a username or token");
135 Log.ErrorFormat("[RETRIEVE] No credentials returned by server");
136 throw new Exception("The server did not return a username or token");
140 Log.InfoFormat("[RETRIEVE] Credentials retrieved user:{0} token:{1}", userName, token);
141 Respond(context, "Authenticated", "Got It");
143 return new NetworkCredential(userName, token);
146 catch (Exception exc)
148 Log.Error("[RETRIEVE][ERROR] Receive connection {0}", exc);
155 private static async Task<NetworkCredential> ListenForRedirectAsync(int port)
157 var listener = new TcpListener(IPAddress.Any, port);
158 Log.InfoFormat("[RETRIEVE] Listening at {0}", port);
164 using (var client = await listener.AcceptTcpClientAsync()
165 .WithTimeout(TimeSpan.FromMinutes(5)))
167 using (var stream = client.GetStream())
168 using (var reader=new StreamReader(stream))
170 var request = await reader.ReadLineAsync();
172 //TODO: Add proper warnings here if the content is empty, don't just throw an exception
173 //This may be a common occurence
175 if (String.IsNullOrWhiteSpace(request))
176 throw new PithosException("The server did send any information");
177 Log.InfoFormat("[RETRIEVE] Got Connection {0}", request);
180 var items = ParseResponse(request);
182 var userName = items["user"];
183 var token = items["token"];
184 if (String.IsNullOrWhiteSpace(userName) ||
185 String.IsNullOrWhiteSpace(token))
187 Respond(stream, "Failure", "The server did not return a username or token");
188 Log.ErrorFormat("[RETRIEVE] No credentials returned by server");
189 throw new PithosException("The server did not return a username or token");
193 Log.InfoFormat("[RETRIEVE] Credentials retrieved user:{0} token:{1}", userName, token);
194 Respond(stream, "Authenticated", "Got It");
196 return new NetworkCredential(userName, token);
200 catch (Exception exc)
202 Log.Error("[RETRIEVE][ERROR] Receive connection {0}", exc);
203 throw new PithosException("An error occured while retrieving credentials",exc);
207 private static Dictionary<string, string> ParseResponse(string request)
209 var parts = request.Split(' ');
210 var query = parts[1].TrimStart('/', '?');
212 var items = query.Split('&')
213 .Select(pair => pair.Split('='))
214 .ToDictionary(arr => arr[0].ToLower(), arr => Uri.UnescapeDataString(arr[1]));
219 private static void Respond(HttpListenerContext context,string title,string message)
221 var response = context.Response;
222 var html = String.Format("<html><head><title>{0}</title></head><body><h1>{1}</h1></body></html>", title, message);
223 var outBuffer = Encoding.UTF8.GetBytes(html);
224 response.ContentLength64 = outBuffer.Length;
225 using (var stream = response.OutputStream)
227 stream.Write(outBuffer, 0, outBuffer.Length);
230 Log.InfoFormat("[RETRIEVE] Responded");
233 private static void Respond(Stream stream,string title,string message)
236 var html = String.Format("<html><head><title>{0}</title></head><body><h1>{1}</h1></body></html>", title, message);
238 var response = new StringBuilder();
239 response.AppendLine("HTTP/1.1 200 OK");
240 response.AppendFormat("Content-Length: {0}\n", html.Length);
241 response.AppendLine("Server: Microsoft-HTTPAPI/2.0");
242 response.AppendFormat("Date: {0}\n\n", DateTime.UtcNow);
243 response.AppendLine(html);
245 var outBuffer=Encoding.UTF8.GetBytes(response.ToString());
247 stream.Write(outBuffer, 0, outBuffer.Length);
249 Log.InfoFormat("[RETRIEVE] Responded");
254 /// Locates a free local port
256 /// <returns>A free port</returns>
257 /// <remarks>The method starts and stops a TcpListener on port 0 to locate a free port.</remarks>
258 public static int GetFreePort()
260 //The TcpListener will locate a free port
261 var listener = new TcpListener(IPAddress.Any, 0);
263 var port = ((IPEndPoint)listener.LocalEndpoint).Port;