Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / PithosAccount.cs @ 7964bdc9

History | View | Annotate | Download (5 kB)

1 7964bdc9 George Pantazis
#region
2 7964bdc9 George Pantazis
/* -----------------------------------------------------------------------
3 7964bdc9 George Pantazis
 * <copyright file="PithosAccount.cs" company="GRNet">
4 7964bdc9 George Pantazis
 * 
5 7964bdc9 George Pantazis
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
6 7964bdc9 George Pantazis
 *
7 7964bdc9 George Pantazis
 * Redistribution and use in source and binary forms, with or
8 7964bdc9 George Pantazis
 * without modification, are permitted provided that the following
9 7964bdc9 George Pantazis
 * conditions are met:
10 7964bdc9 George Pantazis
 *
11 7964bdc9 George Pantazis
 *   1. Redistributions of source code must retain the above
12 7964bdc9 George Pantazis
 *      copyright notice, this list of conditions and the following
13 7964bdc9 George Pantazis
 *      disclaimer.
14 7964bdc9 George Pantazis
 *
15 7964bdc9 George Pantazis
 *   2. Redistributions in binary form must reproduce the above
16 7964bdc9 George Pantazis
 *      copyright notice, this list of conditions and the following
17 7964bdc9 George Pantazis
 *      disclaimer in the documentation and/or other materials
18 7964bdc9 George Pantazis
 *      provided with the distribution.
19 7964bdc9 George Pantazis
 *
20 7964bdc9 George Pantazis
 *
21 7964bdc9 George Pantazis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22 7964bdc9 George Pantazis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 7964bdc9 George Pantazis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 7964bdc9 George Pantazis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25 7964bdc9 George Pantazis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 7964bdc9 George Pantazis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 7964bdc9 George Pantazis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 7964bdc9 George Pantazis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 7964bdc9 George Pantazis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 7964bdc9 George Pantazis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 7964bdc9 George Pantazis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 7964bdc9 George Pantazis
 * POSSIBILITY OF SUCH DAMAGE.
33 7964bdc9 George Pantazis
 *
34 7964bdc9 George Pantazis
 * The views and conclusions contained in the software and
35 7964bdc9 George Pantazis
 * documentation are those of the authors and should not be
36 7964bdc9 George Pantazis
 * interpreted as representing official policies, either expressed
37 7964bdc9 George Pantazis
 * or implied, of GRNET S.A.
38 7964bdc9 George Pantazis
 * </copyright>
39 7964bdc9 George Pantazis
 * -----------------------------------------------------------------------
40 7964bdc9 George Pantazis
 */
41 7964bdc9 George Pantazis
#endregion
42 7964bdc9 George Pantazis
43 7964bdc9 George Pantazis
// </copyright>
44 c010b69c pkanavos
// -----------------------------------------------------------------------
45 c010b69c pkanavos
46 7964bdc9 George Pantazis
using System.Reflection;
47 7964bdc9 George Pantazis
using Pithos.Client.WPF.Preferences;
48 2f5fcd2f pkanavos
using Pithos.Client.WPF.Properties;
49 2f5fcd2f pkanavos
using Pithos.Interfaces;
50 7964bdc9 George Pantazis
using Pithos.Network;
51 7964bdc9 George Pantazis
using log4net;
52 7964bdc9 George Pantazis
53 7964bdc9 George Pantazis
namespace Pithos.Client.WPF
54 7964bdc9 George Pantazis
{
55 c010b69c pkanavos
    using System;
56 c010b69c pkanavos
    using System.Linq;
57 c010b69c pkanavos
    using System.Net;
58 7964bdc9 George Pantazis
    using System.Diagnostics.Contracts;
59 7964bdc9 George Pantazis
    using System.Diagnostics;
60 7964bdc9 George Pantazis
    using System.Net.Sockets;
61 7964bdc9 George Pantazis
62 7964bdc9 George Pantazis
    /// <summary>
63 7964bdc9 George Pantazis
    /// TODO: Update summary.
64 7964bdc9 George Pantazis
    /// </summary>
65 7964bdc9 George Pantazis
    /// <summary>
66 7964bdc9 George Pantazis
    /// Retrieves an account name and token from PITHOS
67 7964bdc9 George Pantazis
    /// </summary>
68 7964bdc9 George Pantazis
    public static class PithosAccount
69 7964bdc9 George Pantazis
    {
70 7964bdc9 George Pantazis
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
71 7964bdc9 George Pantazis
72 7964bdc9 George Pantazis
        /// <summary>
73 7964bdc9 George Pantazis
        /// Asynchronously retrieves PITHOS credentials
74 7964bdc9 George Pantazis
        /// </summary>
75 7964bdc9 George Pantazis
        /// <param name="loginUrl">URL to retrieve the account info from PITHOS. Must end with =</param>
76 7964bdc9 George Pantazis
        /// <returns>The credentials wrapped in a Task</returns>
77 7964bdc9 George Pantazis
        public static NetworkCredential RetrieveCredentials(string loginUrl,string accountName=null)
78 7964bdc9 George Pantazis
        {
79 7964bdc9 George Pantazis
            Contract.Requires(Uri.IsWellFormedUriString(loginUrl, UriKind.Absolute));
80 7964bdc9 George Pantazis
81 7964bdc9 George Pantazis
            if (!Uri.IsWellFormedUriString(loginUrl, UriKind.Absolute))
82 7964bdc9 George Pantazis
                throw new ArgumentException("The pithos site parameter must be a valid absolute URL", "loginUrl");
83 7964bdc9 George Pantazis
            
84 7964bdc9 George Pantazis
            //int port = GetFreePort();
85 7964bdc9 George Pantazis
            
86 7964bdc9 George Pantazis
            //TODO:Force logout here to take care of existing cookies
87 7964bdc9 George Pantazis
88 7964bdc9 George Pantazis
89 7964bdc9 George Pantazis
            //var listenerUrl = String.Format("http://127.0.0.1:{0}", port);
90 7964bdc9 George Pantazis
            var listenerUrl = "pithos://127.0.0.1";
91 7964bdc9 George Pantazis
92 7964bdc9 George Pantazis
            
93 7964bdc9 George Pantazis
94 7964bdc9 George Pantazis
            //var receiveCredentials = ListenForRedirectAsync(port);
95 7964bdc9 George Pantazis
96 7964bdc9 George Pantazis
            //var logoutUrl = loginUrl.Replace("login", "im/logout");
97 7964bdc9 George Pantazis
            //var logoutProcess=Process.Start(logoutUrl);            
98 7964bdc9 George Pantazis
            //TaskEx.Delay(2000).Wait();
99 7964bdc9 George Pantazis
            var uriBuilder = new UriBuilder(loginUrl)
100 7964bdc9 George Pantazis
                                 {
101 7964bdc9 George Pantazis
                                     Query = String.Format("next={0}&force=", listenerUrl)
102 7964bdc9 George Pantazis
                                 };
103 7964bdc9 George Pantazis
104 7964bdc9 George Pantazis
            var retrieveUri = uriBuilder.Uri;
105 7964bdc9 George Pantazis
106 7964bdc9 George Pantazis
            var logoutUrl= (from server in Settings.Default.Servers
107 7964bdc9 George Pantazis
                           where server.LoginUri == loginUrl
108 7964bdc9 George Pantazis
                               select new Uri(server.LogoutUri)).FirstOrDefault();
109 7964bdc9 George Pantazis
110 7964bdc9 George Pantazis
111 7964bdc9 George Pantazis
112 7964bdc9 George Pantazis
            var browser = new LoginView(retrieveUri,logoutUrl,accountName);            
113 7964bdc9 George Pantazis
            if (true == browser.ShowDialog())
114 7964bdc9 George Pantazis
            {
115 7964bdc9 George Pantazis
                return new NetworkCredential(browser.Account, browser.Token);
116 7964bdc9 George Pantazis
            }
117 7964bdc9 George Pantazis
            return null;
118 7964bdc9 George Pantazis
119 7964bdc9 George Pantazis
/*
120 7964bdc9 George Pantazis
121 7964bdc9 George Pantazis
            Log.InfoFormat("[RETRIEVE] Open Browser at {0}", retrieveUri);
122 7964bdc9 George Pantazis
            Process.Start(retrieveUri.ToString());
123 7964bdc9 George Pantazis
124 7964bdc9 George Pantazis
            return receiveCredentials;
125 7964bdc9 George Pantazis
*/
126 7964bdc9 George Pantazis
        }
127 7964bdc9 George Pantazis
128 2f5fcd2f pkanavos
        public static Uri GetLoginUri(string serverUrl)
129 2f5fcd2f pkanavos
        {
130 2f5fcd2f pkanavos
            return GetLoginUri(new Uri(serverUrl));
131 2f5fcd2f pkanavos
        }
132 c010b69c pkanavos
133 2f5fcd2f pkanavos
        public static Uri GetLoginUri(Uri serverUri)
134 2f5fcd2f pkanavos
        {
135 7964bdc9 George Pantazis
            var loginUri = serverUri.Combine("/login");
136 7964bdc9 George Pantazis
            return loginUri;
137 2f5fcd2f pkanavos
        }
138 c010b69c pkanavos
139 c010b69c pkanavos
140 7964bdc9 George Pantazis
    }
141 7964bdc9 George Pantazis
}