2 using System.Collections.Generic;
3 using System.ComponentModel;
5 using System.Linq.Expressions;
6 using System.Reflection;
8 using System.Threading.Tasks;
10 using System.Windows.Controls;
11 using System.Windows.Data;
12 using System.Windows.Documents;
13 using System.Windows.Input;
14 using System.Windows.Media;
15 using System.Windows.Media.Imaging;
16 using System.Windows.Shapes;
18 using Pithos.Client.WPF.Properties;
21 namespace Pithos.Client.WPF.Preferences
24 /// Interaction logic for LoginView.xaml
26 public partial class LoginView : Window,INotifyPropertyChanged
28 private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
31 private Uri _logoutUri;
32 private bool _loggingOut;
43 RaisePropertyChanged(() => Uri);
47 private void RaisePropertyChanged(Expression<Func<Uri>> property)
49 if (PropertyChanged != null)
51 PropertyChanged(this, new PropertyChangedEventArgs(property.GetMemberInfo().Name));
55 public LoginView(Uri loginUri, Uri logoutUri, string account = null)
57 InitializeComponent();
58 if (String.IsNullOrWhiteSpace(account))
59 this.Title = "Retrieve Pithos credentials";
61 this.Title = "Retrieve Pithos credentials for " + account;
65 _logoutUri = logoutUri;
69 LoginBrowser.Navigate(logoutUri ?? loginUri);
73 public string Token { get; set; }
74 public string Account { get; set; }
77 private void LoginBrowser_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
80 LoginBrowser.Visibility=Visibility.Hidden;
82 if (Log.IsDebugEnabled)
83 Log.Debug(e.ToString());
85 if (e.Uri.Scheme == "pithos")
87 Log.DebugFormat("Authentication response [{0}]",e.Uri);
89 var response = ParseResponse(e.Uri.Query);
90 Account = response["user"];
91 Token = response["token"];
92 Log.InfoFormat("Token received for [{0}]", Account);
98 private static Dictionary<string, string> ParseResponse(string request)
100 //var parts = request.Split(' ');
101 var query = request.TrimStart('?');
103 var items = query.Split('&')
104 .Select(pair => pair.Split('='))
105 .ToDictionary(arr => arr[0].ToLower(), arr => Uri.UnescapeDataString(arr[1]));
109 public event PropertyChangedEventHandler PropertyChanged;
111 private void Retry_Click(object sender, RoutedEventArgs e)
113 LoginBrowser.Navigate(Uri);
116 private void LoginBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
121 IsBusy.BusyContent = "Opening login screen";
122 LoginBrowser.Navigate(Uri);
126 IsBusy.IsBusy = false;
127 LoginBrowser.Visibility = Visibility.Visible;