root / trunk / Pithos.Client.WPF / Preferences / LoginView.xaml.cs @ 2115e2a5
History | View | Annotate | Download (6.3 kB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.ComponentModel; |
4 |
using System.Linq; |
5 |
using System.Linq.Expressions; |
6 |
using System.Net; |
7 |
using System.Net.Http; |
8 |
using System.Reflection; |
9 |
using System.Text; |
10 |
using System.Threading.Tasks; |
11 |
using System.Windows; |
12 |
using System.Windows.Controls; |
13 |
using System.Windows.Data; |
14 |
using System.Windows.Documents; |
15 |
using System.Windows.Input; |
16 |
using System.Windows.Media; |
17 |
using System.Windows.Media.Imaging; |
18 |
using System.Windows.Shapes; |
19 |
using Caliburn.Micro; |
20 |
using Pithos.Client.WPF.Properties; |
21 |
|
22 |
|
23 |
namespace Pithos.Client.WPF.Preferences |
24 |
{ |
25 |
/// <summary> |
26 |
/// Interaction logic for LoginView.xaml |
27 |
/// </summary> |
28 |
public partial class LoginView : Window,INotifyPropertyChanged |
29 |
{ |
30 |
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
31 |
|
32 |
private Uri _uri; |
33 |
private Uri _logoutUri; |
34 |
private bool _loggingOut; |
35 |
|
36 |
public Uri Uri |
37 |
{ |
38 |
get |
39 |
{ |
40 |
return _uri; |
41 |
} |
42 |
set |
43 |
{ |
44 |
_uri = value; |
45 |
RaisePropertyChanged(() => Uri); |
46 |
} |
47 |
} |
48 |
|
49 |
private void RaisePropertyChanged(Expression<Func<Uri>> property) |
50 |
{ |
51 |
if (PropertyChanged != null) |
52 |
{ |
53 |
PropertyChanged(this, new PropertyChangedEventArgs(property.GetMemberInfo().Name)); |
54 |
} |
55 |
} |
56 |
|
57 |
public LoginView(Uri loginUri, Uri logoutUri, string account = null) |
58 |
{ |
59 |
InitializeComponent(); |
60 |
if (String.IsNullOrWhiteSpace(account)) |
61 |
this.Title = "Retrieve Pithos credentials"; |
62 |
else |
63 |
this.Title = "Retrieve Pithos credentials for " + account; |
64 |
Uri = loginUri; |
65 |
|
66 |
|
67 |
_logoutUri = logoutUri; |
68 |
|
69 |
_loggingOut = true; |
70 |
|
71 |
//GetPage(loginUri); |
72 |
|
73 |
LoginBrowser.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e<a.length&&a[e];e++){f++;for(b='.'+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,'')){for(c=location.pathname;c;c=c.replace(/.$/,'')){document.cookie=(a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())"); |
74 |
|
75 |
LoginBrowser.Navigate(logoutUri ?? loginUri); |
76 |
} |
77 |
|
78 |
private async Task GetPage(Uri url) |
79 |
{ |
80 |
IsBusy.BusyContent = "Opening login screen"; |
81 |
IsBusy.IsBusy = true; |
82 |
LoginBrowser.Visibility = Visibility.Hidden; |
83 |
|
84 |
var AstakosCookie = new CookieContainer(); |
85 |
|
86 |
//using (var handler = new HttpClientHandler() { |
87 |
// CookieContainer = AstakosCookie, |
88 |
//AllowAutoRedirect = true, |
89 |
//AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip, |
90 |
//UseCookies = false |
91 |
//}) ; |
92 |
|
93 |
var client = new HttpClient(new HttpClientHandler |
94 |
{ |
95 |
CookieContainer = AstakosCookie, |
96 |
AllowAutoRedirect=true, |
97 |
AutomaticDecompression=DecompressionMethods.Deflate|DecompressionMethods.GZip, |
98 |
UseCookies=false |
99 |
}); |
100 |
|
101 |
AstakosCookie.SetCookies(url, "sessionid="); |
102 |
AstakosCookie.SetCookies(url, "Expires=Thu, 01-Jan-1970 00:00:01 GMT;"); |
103 |
|
104 |
using (var response = await client.GetAsync(url)) |
105 |
{ |
106 |
var requestUri = response.RequestMessage.RequestUri; |
107 |
var baseUri = new UriBuilder(requestUri.Scheme, requestUri.DnsSafeHost); |
108 |
var page = await response.Content.ReadAsStringAsync(); |
109 |
var headIndex = page.IndexOf("<head>"); |
110 |
|
111 |
var finalPage = String.Concat(page.Substring(0, headIndex + 6), |
112 |
String.Format("\n<base href='{0}' >\n", baseUri), |
113 |
page.Substring(headIndex + 6)); |
114 |
|
115 |
LoginBrowser.NavigateToString(finalPage); |
116 |
|
117 |
IsBusy.IsBusy = false; |
118 |
LoginBrowser.Visibility = Visibility.Visible; |
119 |
} |
120 |
} |
121 |
|
122 |
public string Token { get; set; } |
123 |
public string Account { get; set; } |
124 |
|
125 |
|
126 |
private void LoginBrowser_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e) |
127 |
{ |
128 |
IsBusy.BusyContent = "Please Wait"; |
129 |
IsBusy.IsBusy = true; |
130 |
LoginBrowser.Visibility=Visibility.Hidden; |
131 |
|
132 |
if (Log.IsDebugEnabled) |
133 |
Log.Debug(e.ToString()); |
134 |
/* |
135 |
if (e.Uri == null) |
136 |
return; |
137 |
*/ |
138 |
if (e.Uri.Scheme == "pithos") |
139 |
{ |
140 |
Log.DebugFormat("Authentication response [{0}]",e.Uri); |
141 |
e.Cancel = true; |
142 |
var response = ParseResponse(e.Uri.Query); |
143 |
Account = response["user"]; |
144 |
Token = response["token"]; |
145 |
Log.InfoFormat("Token received for [{0}]", Account); |
146 |
DialogResult = true; |
147 |
this.Close(); |
148 |
} |
149 |
} |
150 |
|
151 |
private static Dictionary<string, string> ParseResponse(string request) |
152 |
{ |
153 |
//var parts = request.Split(' '); |
154 |
var query = request.TrimStart('?'); |
155 |
|
156 |
var items = query.Split('&') |
157 |
.Select(pair => pair.Split('=')) |
158 |
.ToDictionary(arr => arr[0].ToLower(), arr => Uri.UnescapeDataString(arr[1])); |
159 |
return items; |
160 |
} |
161 |
|
162 |
public event PropertyChangedEventHandler PropertyChanged; |
163 |
|
164 |
private void Retry_Click(object sender, RoutedEventArgs e) |
165 |
{ |
166 |
LoginBrowser.Navigate(Uri); |
167 |
} |
168 |
|
169 |
private void LoginBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e) |
170 |
{ |
171 |
if (_loggingOut) |
172 |
{ |
173 |
_loggingOut = false; |
174 |
IsBusy.BusyContent = "Opening login screen"; |
175 |
LoginBrowser.Navigate(Uri); |
176 |
} |
177 |
else |
178 |
{ |
179 |
IsBusy.IsBusy = false; |
180 |
LoginBrowser.Visibility = Visibility.Visible; |
181 |
} |
182 |
} |
183 |
} |
184 |
} |