Created login and error handling to get auth token. Refreshing not handled.

This commit is contained in:
Patrick Fic
2020-01-16 18:43:50 -08:00
parent 0daf17a3f0
commit cfec5327dd
17 changed files with 625 additions and 47 deletions

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BodyshopUploader.Utils
{
public static class AppMetaData
{
public static string FirebaseAPIKey_DEV = "AIzaSyDV9MsSHZmpLtjoaTK_ObvjFaJ-nMSd2KA";
}
}

View File

@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Firebase.Auth;
using System.Timers;
namespace BodyshopUploader.Utils
{
public static class Auth
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public static FirebaseAuthLink authlink;
public static string authToken;
static string refreshToken;
static int tokenExpiration;
static FirebaseAuthProvider ap = new FirebaseAuthProvider(new FirebaseConfig(Utils.AppMetaData.FirebaseAPIKey_DEV)); //TODO: Update this to be a dynamic key. Perhaps a function that fetches?
private static Timer tokenTimer = new Timer();
public async static Task<(bool, string)> LoginAsync(string Username, string Password)
{
try
{
authlink = await ap.SignInWithEmailAndPasswordAsync(Username, Password);
authToken = authlink.FirebaseToken;
refreshToken = authlink.RefreshToken;
tokenExpiration = authlink.ExpiresIn;
logger.Trace("Firebase Auth Token {0}.", authToken);
logger.Trace("Firebase Refresh Token {0}.", refreshToken);
logger.Trace("Firebase Auth Token expires in {0} seconds.", tokenExpiration);
tokenTimer.Interval = (tokenExpiration - 600) * 1000; //Set the token to refresh 10 minutes before it has to.
tokenTimer.Elapsed += TokenTimer_Elapsed;
return (true, null);
}
catch (FirebaseAuthException Ex)
{
switch (Ex.Reason)
{
case AuthErrorReason.WrongPassword:
logger.Error("Incorrect password provided for user.");
return (false, Properties.Resources.Error_Login);
case AuthErrorReason.UnknownEmailAddress:
logger.Error("User does not exist.");
return (false, Properties.Resources.Error_Login);
case AuthErrorReason.TooManyAttemptsTryLater:
logger.Error("Too many attempts logging in.");
return (false, Properties.Resources.Error_Login_Attempts);
default:
logger.Error(Ex, "Unknown error occured while logging in. {0}", Ex.Reason);
return (false, Properties.Resources.Error_Login_Unknown);
}
}
catch (Exception Ex)
{
logger.Error(Ex, "Unknown error encountered while obtaining auth token.");
return (false, Properties.Resources.Error_Generic);
}
}
private static void TokenTimer_Elapsed(object sender, ElapsedEventArgs e)
{
//Gotta do some stuff now that i got a new token!
//Maybe the token auto refreshes?
}
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
namespace BodyshopUploader.Utils
{
public static class LoginHelpers
{
public static void SaveLoginSettings(string Username, SecureString Password)
{
Properties.Settings.Default.Username = Username;
//Todo: Figure out how to save the secure string. Perhaps serialize to JSON?
Properties.Settings.Default.Password = Password;
Properties.Settings.Default.Save();
}
public static string DecodePassword (SecureString s)
{
//Manage the secure string for the password.
IntPtr stringPointer = Marshal.SecureStringToBSTR(s);
string UP = Marshal.PtrToStringBSTR(stringPointer);
Marshal.ZeroFreeBSTR(stringPointer);
return UP;
}
}
}

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
namespace BodyshopUploader.Utils
{
public class BoolVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException("Two-way binding not supported by BoolVisibilityConverter");
}
}
public class NullVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return Visibility.Collapsed;
else
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException("Two-way binding not supported by ZeroVisibilityConverter");
}
}
}