70 lines
3.0 KiB
C#
70 lines
3.0 KiB
C#
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?
|
|
}
|
|
}
|
|
}
|