using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Caliburn.Micro;
using Pithos.Client.WPF.Properties;
namespace Pithos.Client.WPF.Preferences
{
///
/// Interaction logic for LoginView.xaml
///
public partial class LoginView : Window,INotifyPropertyChanged
{
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private Uri _uri;
private Uri _logoutUri;
private bool _loggingOut;
public Uri Uri
{
get
{
return _uri;
}
set
{
_uri = value;
RaisePropertyChanged(() => Uri);
}
}
private void RaisePropertyChanged(Expression> property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property.GetMemberInfo().Name));
}
}
public LoginView(Uri loginUri, Uri logoutUri, string account = null)
{
InitializeComponent();
if (String.IsNullOrWhiteSpace(account))
this.Title = "Retrieve Pithos credentials";
else
this.Title = "Retrieve Pithos credentials for " + account;
Uri = loginUri;
_logoutUri = logoutUri;
_loggingOut = true;
//GetPage(loginUri);
LoginBrowser.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e\n", baseUri),
page.Substring(headIndex + 6));
LoginBrowser.NavigateToString(finalPage);
IsBusy.IsBusy = false;
LoginBrowser.Visibility = Visibility.Visible;
}
}
public string Token { get; set; }
public string Account { get; set; }
private void LoginBrowser_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
{
IsBusy.BusyContent = "Please Wait";
IsBusy.IsBusy = true;
LoginBrowser.Visibility=Visibility.Hidden;
if (Log.IsDebugEnabled)
Log.Debug(e.ToString());
/*
if (e.Uri == null)
return;
*/
if (e.Uri.Scheme == "pithos")
{
Log.DebugFormat("Authentication response [{0}]",e.Uri);
e.Cancel = true;
var response = ParseResponse(e.Uri.Query);
Account = response["user"];
Token = response["token"];
Log.InfoFormat("Token received for [{0}]", Account);
DialogResult = true;
this.Close();
}
}
private static Dictionary ParseResponse(string request)
{
//var parts = request.Split(' ');
var query = request.TrimStart('?');
var items = query.Split('&')
.Select(pair => pair.Split('='))
.ToDictionary(arr => arr[0].ToLower(), arr => Uri.UnescapeDataString(arr[1]));
return items;
}
public event PropertyChangedEventHandler PropertyChanged;
private void Retry_Click(object sender, RoutedEventArgs e)
{
LoginBrowser.Navigate(Uri);
}
private void LoginBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
if (_loggingOut)
{
_loggingOut = false;
IsBusy.BusyContent = "Opening login screen";
LoginBrowser.Navigate(Uri);
}
else
{
IsBusy.IsBusy = false;
LoginBrowser.Visibility = Visibility.Visible;
}
}
}
}