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