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