Centralized logout into a message subscription in app class. Logout when API results are forbidden or unauthorized.

This commit is contained in:
Kyle Spearrin 2016-07-19 18:46:39 -04:00
parent a5d2ae9637
commit d07210c7dc
14 changed files with 119 additions and 26 deletions

View file

@ -12,6 +12,7 @@ using Plugin.Fingerprint.Abstractions;
using Plugin.Settings.Abstractions; using Plugin.Settings.Abstractions;
using Plugin.Connectivity.Abstractions; using Plugin.Connectivity.Abstractions;
using Acr.UserDialogs; using Acr.UserDialogs;
using PushNotification.Plugin.Abstractions;
namespace Bit.Android namespace Bit.Android
{ {
@ -32,7 +33,8 @@ namespace Bit.Android
Resolver.Resolve<IDatabaseService>(), Resolver.Resolve<IDatabaseService>(),
Resolver.Resolve<ISyncService>(), Resolver.Resolve<ISyncService>(),
Resolver.Resolve<IFingerprint>(), Resolver.Resolve<IFingerprint>(),
Resolver.Resolve<ISettings>())); Resolver.Resolve<ISettings>(),
Resolver.Resolve<IPushNotification>()));
} }
protected override void OnPause() protected override void OnPause()

View file

@ -13,6 +13,7 @@ using Bit.App.Controls;
using Plugin.Connectivity.Abstractions; using Plugin.Connectivity.Abstractions;
using System.Net; using System.Net;
using Acr.UserDialogs; using Acr.UserDialogs;
using PushNotification.Plugin.Abstractions;
namespace Bit.App namespace Bit.App
{ {
@ -25,6 +26,7 @@ namespace Bit.App
private readonly IAuthService _authService; private readonly IAuthService _authService;
private readonly IFingerprint _fingerprint; private readonly IFingerprint _fingerprint;
private readonly ISettings _settings; private readonly ISettings _settings;
private readonly IPushNotification _pushNotification;
public App( public App(
IAuthService authService, IAuthService authService,
@ -33,7 +35,8 @@ namespace Bit.App
IDatabaseService databaseService, IDatabaseService databaseService,
ISyncService syncService, ISyncService syncService,
IFingerprint fingerprint, IFingerprint fingerprint,
ISettings settings) ISettings settings,
IPushNotification pushNotification)
{ {
_databaseService = databaseService; _databaseService = databaseService;
_connectivity = connectivity; _connectivity = connectivity;
@ -42,6 +45,7 @@ namespace Bit.App
_authService = authService; _authService = authService;
_fingerprint = fingerprint; _fingerprint = fingerprint;
_settings = settings; _settings = settings;
_pushNotification = pushNotification;
SetStyles(); SetStyles();
@ -64,6 +68,11 @@ namespace Bit.App
{ {
await CheckLockAsync(args); await CheckLockAsync(args);
}); });
MessagingCenter.Subscribe<Application, string>(Current, "Logout", (sender, args) =>
{
Logout(args);
});
} }
protected async override void OnStart() protected async override void OnStart()
@ -166,6 +175,20 @@ namespace Bit.App
} }
} }
private void Logout(string logoutMessage)
{
_authService.LogOut();
Device.BeginInvokeOnMainThread(() =>
{
_pushNotification.Unregister();
});
Current.MainPage = new HomePage();
if(!string.IsNullOrWhiteSpace(logoutMessage))
{
_userDialogs.WarnToast("Logged out", logoutMessage);
}
}
private async Task CheckLockAsync(bool forceLock) private async Task CheckLockAsync(bool forceLock)
{ {
// Only lock if they are logged in // Only lock if they are logged in

View file

@ -14,7 +14,6 @@ namespace Bit.App.Pages
public class LockFingerprintPage : ExtendedContentPage public class LockFingerprintPage : ExtendedContentPage
{ {
private readonly IFingerprint _fingerprint; private readonly IFingerprint _fingerprint;
private readonly IAuthService _authService;
private readonly IUserDialogs _userDialogs; private readonly IUserDialogs _userDialogs;
private readonly ISettings _settings; private readonly ISettings _settings;
private readonly bool _checkFingerprintImmediately; private readonly bool _checkFingerprintImmediately;
@ -24,7 +23,6 @@ namespace Bit.App.Pages
{ {
_checkFingerprintImmediately = checkFingerprintImmediately; _checkFingerprintImmediately = checkFingerprintImmediately;
_fingerprint = Resolver.Resolve<IFingerprint>(); _fingerprint = Resolver.Resolve<IFingerprint>();
_authService = Resolver.Resolve<IAuthService>();
_userDialogs = Resolver.Resolve<IUserDialogs>(); _userDialogs = Resolver.Resolve<IUserDialogs>();
_settings = Resolver.Resolve<ISettings>(); _settings = Resolver.Resolve<ISettings>();
@ -79,9 +77,7 @@ namespace Bit.App.Pages
return; return;
} }
_authService.LogOut(); MessagingCenter.Send(Application.Current, "Logout", (string)null);
await Navigation.PopModalAsync();
Application.Current.MainPage = new HomePage();
} }
public async Task CheckFingerprintAsync() public async Task CheckFingerprintAsync()

View file

@ -123,9 +123,7 @@ namespace Bit.App.Pages
return; return;
} }
_authService.LogOut(); MessagingCenter.Send(Application.Current, "Logout", (string)null);
await Navigation.PopModalAsync();
Application.Current.MainPage = new HomePage();
} }
} }
} }

View file

@ -102,9 +102,7 @@ namespace Bit.App.Pages
return; return;
} }
_authService.LogOut(); MessagingCenter.Send(Application.Current, "Logout", (string)null);
await Navigation.PopModalAsync();
Application.Current.MainPage = new HomePage();
} }
} }
} }

View file

@ -7,6 +7,7 @@ using Bit.App.Resources;
using Plugin.Connectivity.Abstractions; using Plugin.Connectivity.Abstractions;
using Xamarin.Forms; using Xamarin.Forms;
using XLabs.Ioc; using XLabs.Ioc;
using System.Linq;
namespace Bit.App.Pages namespace Bit.App.Pages
{ {
@ -74,8 +75,16 @@ namespace Bit.App.Pages
await saveTask; await saveTask;
_userDialogs.HideLoading(); _userDialogs.HideLoading();
if(saveTask.Result.Succeeded)
{
await Navigation.PopModalAsync(); await Navigation.PopModalAsync();
_userDialogs.SuccessToast(nameCell.Entry.Text, "New folder created."); _userDialogs.SuccessToast(nameCell.Entry.Text, "New folder created.");
}
else if(saveTask.Result.Errors.Count() > 0)
{
await _userDialogs.AlertAsync(saveTask.Result.Errors.First().Message, AppResources.AnErrorHasOccurred);
}
}, ToolbarItemOrder.Default, 0); }, ToolbarItemOrder.Default, 0);
Title = "Add Folder"; Title = "Add Folder";

View file

@ -6,6 +6,7 @@ using Bit.App.Resources;
using Plugin.Connectivity.Abstractions; using Plugin.Connectivity.Abstractions;
using Xamarin.Forms; using Xamarin.Forms;
using XLabs.Ioc; using XLabs.Ioc;
using System.Linq;
namespace Bit.App.Pages namespace Bit.App.Pages
{ {
@ -87,8 +88,16 @@ namespace Bit.App.Pages
await saveTask; await saveTask;
_userDialogs.HideLoading(); _userDialogs.HideLoading();
if(saveTask.Result.Succeeded)
{
await Navigation.PopModalAsync(); await Navigation.PopModalAsync();
_userDialogs.SuccessToast(nameCell.Entry.Text, "Folder updated."); _userDialogs.SuccessToast(nameCell.Entry.Text, "Folder updated.");
}
else if(saveTask.Result.Errors.Count() > 0)
{
await _userDialogs.AlertAsync(saveTask.Result.Errors.First().Message, AppResources.AnErrorHasOccurred);
}
}, ToolbarItemOrder.Default, 0); }, ToolbarItemOrder.Default, 0);
Title = "Edit Folder"; Title = "Edit Folder";

View file

@ -199,9 +199,7 @@ namespace Bit.App.Pages
return; return;
} }
_authService.LogOut(); MessagingCenter.Send(Application.Current, "Logout", (string)null);
_pushNotification.Unregister();
Application.Current.MainPage = new HomePage();
} }
private async void ChangeMasterPasswordCell_Tapped(object sender, EventArgs e) private async void ChangeMasterPasswordCell_Tapped(object sender, EventArgs e)

View file

@ -134,8 +134,15 @@ namespace Bit.App.Pages
await saveTask; await saveTask;
_userDialogs.HideLoading(); _userDialogs.HideLoading();
if(saveTask.Result.Succeeded)
{
await Navigation.PopModalAsync(); await Navigation.PopModalAsync();
_userDialogs.SuccessToast(nameCell.Entry.Text, "New site created."); _userDialogs.SuccessToast(nameCell.Entry.Text, "New site created.");
}
else if(saveTask.Result.Errors.Count() > 0)
{
await _userDialogs.AlertAsync(saveTask.Result.Errors.First().Message, AppResources.AnErrorHasOccurred);
}
}, ToolbarItemOrder.Default, 0); }, ToolbarItemOrder.Default, 0);
Title = AppResources.AddSite; Title = AppResources.AddSite;

View file

@ -168,8 +168,16 @@ namespace Bit.App.Pages
await saveTask; await saveTask;
_userDialogs.HideLoading(); _userDialogs.HideLoading();
if(saveTask.Result.Succeeded)
{
await Navigation.PopModalAsync(); await Navigation.PopModalAsync();
_userDialogs.SuccessToast(nameCell.Entry.Text, "Site updated."); _userDialogs.SuccessToast(nameCell.Entry.Text, "Site updated.");
}
else if(saveTask.Result.Errors.Count() > 0)
{
await _userDialogs.AlertAsync(saveTask.Result.Errors.First().Message, AppResources.AnErrorHasOccurred);
}
}, ToolbarItemOrder.Default, 0); }, ToolbarItemOrder.Default, 0);
Title = "Edit Site"; Title = "Edit Site";

View file

@ -6,6 +6,7 @@ using Bit.App.Abstractions;
using Bit.App.Models; using Bit.App.Models;
using Bit.App.Models.Data; using Bit.App.Models.Data;
using Bit.App.Models.Api; using Bit.App.Models.Api;
using Xamarin.Forms;
namespace Bit.App.Services namespace Bit.App.Services
{ {
@ -71,6 +72,11 @@ namespace Bit.App.Services
await _folderRepository.UpdateAsync(data); await _folderRepository.UpdateAsync(data);
} }
} }
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
}
return response; return response;
} }
@ -82,6 +88,11 @@ namespace Bit.App.Services
{ {
await _folderRepository.DeleteAsync(folderId); await _folderRepository.DeleteAsync(folderId);
} }
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
}
return response; return response;
} }

View file

@ -6,6 +6,7 @@ using Bit.App.Abstractions;
using Bit.App.Models; using Bit.App.Models;
using Bit.App.Models.Api; using Bit.App.Models.Api;
using Bit.App.Models.Data; using Bit.App.Models.Data;
using Xamarin.Forms;
namespace Bit.App.Services namespace Bit.App.Services
{ {
@ -78,6 +79,11 @@ namespace Bit.App.Services
await _siteRepository.UpdateAsync(data); await _siteRepository.UpdateAsync(data);
} }
} }
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
}
return response; return response;
} }
@ -89,6 +95,11 @@ namespace Bit.App.Services
{ {
await _siteRepository.DeleteAsync(id); await _siteRepository.DeleteAsync(id);
} }
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
}
return response; return response;
} }

View file

@ -56,6 +56,13 @@ namespace Bit.App.Services
if(!cipher.Succeeded) if(!cipher.Succeeded)
{ {
SyncCompleted(false); SyncCompleted(false);
if(cipher.StatusCode == System.Net.HttpStatusCode.Forbidden
|| cipher.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
}
return false; return false;
} }
@ -136,6 +143,13 @@ namespace Bit.App.Services
if(!ciphers.Succeeded) if(!ciphers.Succeeded)
{ {
SyncCompleted(false); SyncCompleted(false);
if(ciphers.StatusCode == System.Net.HttpStatusCode.Forbidden
|| ciphers.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
}
return false; return false;
} }
@ -174,6 +188,13 @@ namespace Bit.App.Services
if(!ciphers.Succeeded) if(!ciphers.Succeeded)
{ {
SyncCompleted(false); SyncCompleted(false);
if(ciphers.StatusCode == System.Net.HttpStatusCode.Forbidden
|| ciphers.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
}
return false; return false;
} }

View file

@ -24,6 +24,7 @@ using PushNotification.Plugin;
using Plugin.DeviceInfo; using Plugin.DeviceInfo;
using Plugin.Connectivity.Abstractions; using Plugin.Connectivity.Abstractions;
using Bit.App.Pages; using Bit.App.Pages;
using PushNotification.Plugin.Abstractions;
namespace Bit.iOS namespace Bit.iOS
{ {
@ -48,7 +49,8 @@ namespace Bit.iOS
Resolver.Resolve<IDatabaseService>(), Resolver.Resolve<IDatabaseService>(),
Resolver.Resolve<ISyncService>(), Resolver.Resolve<ISyncService>(),
Resolver.Resolve<IFingerprint>(), Resolver.Resolve<IFingerprint>(),
Resolver.Resolve<ISettings>())); Resolver.Resolve<ISettings>(),
Resolver.Resolve<IPushNotification>()));
// Appearance stuff // Appearance stuff