Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / PithosAccount.cs @ 5120f3cb

History | View | Annotate | Download (4.6 kB)

1
// -----------------------------------------------------------------------
2
// <copyright file="PithosAccount.cs" company="Microsoft">
3
// TODO: Update copyright text.
4
// </copyright>
5
// -----------------------------------------------------------------------
6

    
7
using Pithos.Network;
8
using log4net;
9

    
10
namespace Pithos.Client.WPF
11
{
12
    using System;
13
    using System.Collections.Generic;
14
    using System.Linq;
15
    using System.Text;
16
    using System.Net;
17
    using System.Threading.Tasks;
18
    using System.Diagnostics.Contracts;
19
    using System.Diagnostics;
20
    using System.Net.Sockets;
21

    
22
    /// <summary>
23
    /// TODO: Update summary.
24
    /// </summary>
25
    /// <summary>
26
    /// Retrieves an account name and token from PITHOS
27
    /// </summary>
28
    public static class PithosAccount
29
    {
30
        private static readonly ILog Log = LogManager.GetLogger(typeof(PithosAccount));
31

    
32
        /// <summary>
33
        /// Asynchronously retrieves PITHOS credentials
34
        /// </summary>
35
        /// <param name="pithosSite">URL to retrieve the account info from PITHOS. Must end with =</param>
36
        /// <returns>The credentials wrapped in a Task</returns>
37
        public static Task<NetworkCredential> RetrieveCredentialsAsync(string pithosSite)
38
        {
39
            Contract.Requires(Uri.IsWellFormedUriString(pithosSite, UriKind.Absolute));
40

    
41
            if (!Uri.IsWellFormedUriString(pithosSite, UriKind.Absolute))
42
                throw new ArgumentException("The pithosSite parameter must be a valid absolute URL", "pithosSite");
43
            
44
            int port = GetFreePort();
45

    
46

    
47
            var listenerUrl = String.Format("http://127.0.0.1:{0}/", port);
48

    
49
            HttpListener listener = new HttpListener();
50
            listener.Prefixes.Add(listenerUrl);
51

    
52
            Log.InfoFormat("[RETRIEVE] Listening at {0}", listenerUrl);
53

    
54
            listener.Start();
55

    
56
            var task = Task.Factory.FromAsync<HttpListenerContext>(listener.BeginGetContext, listener.EndGetContext, null)
57
                .WithTimeout(TimeSpan.FromMinutes(1))
58
                .ContinueWith(tc =>
59
                {
60
                    try
61
                    {
62
                        if (tc.IsFaulted)
63
                        {
64
                            Log.Error("[RETRIEVE][ERROR] Receive connection {0}", tc.Exception);
65
                            throw tc.Exception;
66
                        }
67
                        else
68
                        {
69

    
70
                            var context = tc.Result;
71
                            var request = context.Request;
72
                            Log.InfoFormat("[RETRIEVE] Got Connection {0}", request.RawUrl);
73

    
74
                            var query = request.QueryString;
75
                            var userName = query["user"];
76
                            var token = query["token"];
77

    
78
                            Log.InfoFormat("[RETRIEVE] Credentials retrieved user:{0} token:{1}", userName, token);
79
                            Respond(context);
80
                            
81
                            return new NetworkCredential(userName, token);
82
                        }
83

    
84
                    }
85
                    finally
86
                    {
87
                        listener.Close();
88
                    }
89
                });
90

    
91
            var uriBuilder=new UriBuilder(pithosSite);
92
            uriBuilder.Path="login";            
93
            uriBuilder.Query="next=" + listenerUrl;
94

    
95
            var retrieveUri = uriBuilder.Uri;
96
            Log.InfoFormat("[RETRIEVE] Open Browser at {0}", retrieveUri);
97
            Process.Start(retrieveUri.ToString());
98

    
99
            return task;
100
        }
101

    
102
        private static void Respond(HttpListenerContext context)
103
        {
104
            var response = context.Response;
105
            var outBuffer = Encoding.UTF8.GetBytes("<html><head><title>Authenticated</title></head><body><h1>Got It</h1></body></html>");
106
            response.ContentLength64 = outBuffer.Length;
107
            using (var stream = response.OutputStream)
108
            {
109
                stream.Write(outBuffer, 0, outBuffer.Length);
110
            }
111

    
112
            Log.InfoFormat("[RETRIEVE] Responded");
113
        }
114
        /// <summary>
115
        /// Locates a free local port 
116
        /// </summary>
117
        /// <returns>A free port</returns>
118
        /// <remarks>The method starts and stops a TcpListener on port 0 to locate a free port.</remarks>
119
        public static int GetFreePort()
120
        {
121
            //The TcpListener will locate a free port             
122
            var listener = new TcpListener(IPAddress.Any, 0);
123
            listener.Start();
124
            var port = ((IPEndPoint)listener.LocalEndpoint).Port;
125
            listener.Stop();
126
            return port;
127
        }
128
    }
129
}