root / trunk / Pithos.Client.WPF / PithosAccount.cs @ 84b880b5
History | View | Annotate | Download (8.2 kB)
1 |
#region |
---|---|
2 |
/* ----------------------------------------------------------------------- |
3 |
* <copyright file="PithosAccount.cs" company="GRNet"> |
4 |
* |
5 |
* Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 |
* |
7 |
* Redistribution and use in source and binary forms, with or |
8 |
* without modification, are permitted provided that the following |
9 |
* conditions are met: |
10 |
* |
11 |
* 1. Redistributions of source code must retain the above |
12 |
* copyright notice, this list of conditions and the following |
13 |
* disclaimer. |
14 |
* |
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. |
19 |
* |
20 |
* |
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. |
33 |
* |
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. |
38 |
* </copyright> |
39 |
* ----------------------------------------------------------------------- |
40 |
*/ |
41 |
#endregion |
42 |
|
43 |
// </copyright> |
44 |
// ----------------------------------------------------------------------- |
45 |
|
46 |
using System.Reflection; |
47 |
using Pithos.Client.WPF.Preferences; |
48 |
using Pithos.Client.WPF.Properties; |
49 |
using Pithos.Interfaces; |
50 |
using Pithos.Network; |
51 |
using log4net; |
52 |
using System.Windows.Forms; |
53 |
using System.IO; |
54 |
using System.Text; |
55 |
using Newtonsoft.Json; |
56 |
using System.Collections.Generic; |
57 |
using System.Collections.Specialized; |
58 |
|
59 |
|
60 |
namespace Pithos.Client.WPF |
61 |
{ |
62 |
using System; |
63 |
using System.Linq; |
64 |
using System.Net; |
65 |
using System.Diagnostics.Contracts; |
66 |
using System.Diagnostics; |
67 |
using System.Net.Sockets; |
68 |
|
69 |
/// <summary> |
70 |
/// TODO: Update summary. |
71 |
/// </summary> |
72 |
/// <summary> |
73 |
/// Retrieves an account name and token from PITHOS |
74 |
/// </summary> |
75 |
public static class PithosAccount |
76 |
{ |
77 |
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
78 |
|
79 |
/// <summary> |
80 |
/// Asynchronously retrieves PITHOS credentials |
81 |
/// </summary> |
82 |
/// <param name="loginUrl">URL to retrieve the account info from PITHOS. Must end with =</param> |
83 |
/// <returns>The credentials wrapped in a Task</returns> |
84 |
public static NetworkCredential RetrieveCredentials(string loginUrl,string accountName=null) |
85 |
{ |
86 |
Contract.Requires(Uri.IsWellFormedUriString(loginUrl, UriKind.Absolute)); |
87 |
|
88 |
if (!Uri.IsWellFormedUriString(loginUrl, UriKind.Absolute)) |
89 |
throw new ArgumentException("The pithos site parameter must be a valid absolute URL", "loginUrl"); |
90 |
|
91 |
//int port = GetFreePort(); |
92 |
|
93 |
//TODO:Force logout here to take care of existing cookies |
94 |
|
95 |
|
96 |
//var listenerUrl = String.Format("http://127.0.0.1:{0}", port); |
97 |
var listenerUrl = "pithos://127.0.0.1"; |
98 |
|
99 |
|
100 |
//var receiveCredentials = ListenForRedirectAsync(port); |
101 |
|
102 |
//var logoutUrl = loginUrl.Replace("login", "im/logout"); |
103 |
//var logoutProcess=Process.Start(logoutUrl); |
104 |
//TaskEx.Delay(2000).Wait(); |
105 |
|
106 |
//<= 0.13 |
107 |
var uriBuilder = new UriBuilder(loginUrl) |
108 |
{ |
109 |
Query = String.Format("next={0}&force=", listenerUrl) |
110 |
}; |
111 |
|
112 |
var retrieveUri = uriBuilder.Uri; |
113 |
|
114 |
var logoutUrl= (from server in Settings.Default.Servers |
115 |
where server.LoginUri == loginUrl |
116 |
select new Uri(server.LogoutUri)).FirstOrDefault(); |
117 |
|
118 |
var browser = new LoginView(retrieveUri,logoutUrl,accountName); |
119 |
if (true == browser.ShowDialog()) |
120 |
{ |
121 |
return new NetworkCredential(browser.Account, browser.Token); |
122 |
} |
123 |
return null; |
124 |
|
125 |
/* |
126 |
|
127 |
Log.InfoFormat("[RETRIEVE] Open Browser at {0}", retrieveUri); |
128 |
Process.Start(retrieveUri.ToString()); |
129 |
|
130 |
return receiveCredentials; |
131 |
*/ |
132 |
} |
133 |
|
134 |
public static Uri GetLoginUri(string serverUrl) |
135 |
{ |
136 |
return GetLoginUri(new Uri(serverUrl)); |
137 |
} |
138 |
|
139 |
public static Uri GetLoginUri(Uri serverUri) |
140 |
{ |
141 |
//<=0.13 |
142 |
var loginUri = serverUri.Combine("login"); |
143 |
|
144 |
// >0.14 |
145 |
WebRequest webRequest = WebRequest.Create(serverUri); |
146 |
webRequest.Method = "POST"; |
147 |
|
148 |
try |
149 |
{ |
150 |
using (WebResponse response = webRequest.GetResponse()) |
151 |
{ |
152 |
try |
153 |
{ |
154 |
using (Stream stream = response.GetResponseStream()) |
155 |
{ |
156 |
StreamReader read = new StreamReader(stream, true); |
157 |
String responseStr = read.ReadToEnd(); |
158 |
|
159 |
dynamic okeanosServices = JsonConvert.DeserializeObject(responseStr); |
160 |
|
161 |
Uri webLogin = new Uri(okeanosServices["access"]["serviceCatalog"][0]["endpoints"][0]["publicURL"].ToString()); |
162 |
|
163 |
webLogin = webLogin.Combine("login"); |
164 |
|
165 |
//MessageBox.Show(okeanosServices["access"]["serviceCatalog"][0]["endpoints"][0]["publicURL"].ToString()); |
166 |
|
167 |
//MessageBox.Show(GetUserID(serverUri, "mkavGw6HOZymWR3k1u2clwOP1poOYukBl5P80PQ_nzE")); |
168 |
|
169 |
return webLogin; |
170 |
} |
171 |
} |
172 |
|
173 |
catch (Exception ex) |
174 |
{ |
175 |
//MessageBox.Show("Stream Failed"); |
176 |
return loginUri; |
177 |
} |
178 |
} |
179 |
} |
180 |
|
181 |
catch (Exception ex) |
182 |
{ |
183 |
//MessageBox.Show("Service URL Failed"); |
184 |
return loginUri; |
185 |
} |
186 |
} |
187 |
|
188 |
public static string GetUserID(Uri serverUri, string token) |
189 |
{ |
190 |
string User = ""; |
191 |
string postData = "{\"auth\":{\"token\":{\"id\":\"" + token + "\"}}}"; |
192 |
ASCIIEncoding encoding = new ASCIIEncoding(); |
193 |
|
194 |
byte[] postSize = encoding.GetBytes(postData); |
195 |
|
196 |
WebRequest webRequest = WebRequest.Create(serverUri); |
197 |
webRequest.Method = "POST"; |
198 |
webRequest.ContentType = "application/json"; |
199 |
|
200 |
Stream newPost = webRequest.GetRequestStream(); |
201 |
newPost.Write(postSize, 0, postSize.Length); |
202 |
|
203 |
using (WebResponse response = webRequest.GetResponse()) |
204 |
{ |
205 |
using (Stream stream = response.GetResponseStream()) |
206 |
{ |
207 |
StreamReader read = new StreamReader(stream, true); |
208 |
String responseStr = read.ReadToEnd(); |
209 |
|
210 |
dynamic tokenreponse = JsonConvert.DeserializeObject(responseStr); |
211 |
|
212 |
//MessageBox.Show(tokenreponse.ToString()); |
213 |
User = tokenreponse["access"]["token"]["tenant"]["id"].ToString(); |
214 |
//MessageBox.Show(tokenreponse["access"]["token"]["tenant"]["id"].ToString()); |
215 |
|
216 |
read.Close(); |
217 |
} |
218 |
} |
219 |
|
220 |
return User; |
221 |
} |
222 |
|
223 |
} |
224 |
} |