Created login and error handling to get auth token. Refreshing not handled.
This commit is contained in:
14
BodyshopUploader/Utils/AppMetaData.cs
Normal file
14
BodyshopUploader/Utils/AppMetaData.cs
Normal 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";
|
||||
|
||||
}
|
||||
}
|
||||
69
BodyshopUploader/Utils/Auth.cs
Normal file
69
BodyshopUploader/Utils/Auth.cs
Normal 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?
|
||||
}
|
||||
}
|
||||
}
|
||||
30
BodyshopUploader/Utils/LoginHelpers.cs
Normal file
30
BodyshopUploader/Utils/LoginHelpers.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
44
BodyshopUploader/Utils/UiConverters.cs
Normal file
44
BodyshopUploader/Utils/UiConverters.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user