Fixed authg + added graphql client.

This commit is contained in:
Patrick Fic
2020-01-16 21:48:03 -08:00
parent cfec5327dd
commit d77e0d1c02
5 changed files with 44 additions and 15 deletions

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<configSections> <configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="BodyshopUploader.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> <section name="BodyshopUploader.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup> </sectionGroup>
</configSections> </configSections>

View File

@@ -58,6 +58,12 @@
<Reference Include="Firebase.Auth, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="Firebase.Auth, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\FirebaseAuthentication.net.3.4.0\lib\netstandard1.1\Firebase.Auth.dll</HintPath> <HintPath>..\packages\FirebaseAuthentication.net.3.4.0\lib\netstandard1.1\Firebase.Auth.dll</HintPath>
</Reference> </Reference>
<Reference Include="GraphQL.Client, Version=1.0.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\GraphQL.Client.1.0.3\lib\netstandard2.0\GraphQL.Client.dll</HintPath>
</Reference>
<Reference Include="GraphQL.Common, Version=1.0.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\GraphQL.Common.1.0.3\lib\netstandard2.0\GraphQL.Common.dll</HintPath>
</Reference>
<Reference Include="Hardcodet.Wpf.TaskbarNotification, Version=1.0.5.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="Hardcodet.Wpf.TaskbarNotification, Version=1.0.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Hardcodet.NotifyIcon.Wpf.1.0.8\lib\net451\Hardcodet.Wpf.TaskbarNotification.dll</HintPath> <HintPath>..\packages\Hardcodet.NotifyIcon.Wpf.1.0.8\lib\net451\Hardcodet.Wpf.TaskbarNotification.dll</HintPath>
</Reference> </Reference>

View File

@@ -12,9 +12,6 @@ namespace BodyshopUploader.Utils
{ {
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public static FirebaseAuthLink authlink; 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? 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(); private static Timer tokenTimer = new Timer();
@@ -23,15 +20,17 @@ namespace BodyshopUploader.Utils
try try
{ {
authlink = await ap.SignInWithEmailAndPasswordAsync(Username, Password); authlink = await ap.SignInWithEmailAndPasswordAsync(Username, Password);
authToken = authlink.FirebaseToken; authlink.FirebaseAuthRefreshed += Authlink_FirebaseAuthRefreshed;
refreshToken = authlink.RefreshToken;
tokenExpiration = authlink.ExpiresIn; logger.Trace("Firebase Auth Token {0}.", authlink.FirebaseToken);
logger.Trace("Firebase Auth Token {0}.", authToken); logger.Trace("Firebase Refresh Token {0}.", authlink.RefreshToken);
logger.Trace("Firebase Refresh Token {0}.", refreshToken); logger.Trace("Firebase Auth Token expires in {0} seconds.", authlink.ExpiresIn);
logger.Trace("Firebase Auth Token expires in {0} seconds.", tokenExpiration); //tokenTimer.Interval = (authlink.ExpiresIn - 600) * 1000; //Set the token to refresh 10 minutes before it has to.
tokenTimer.Interval = 10000; //Set the token to refresh 10 minutes before it has to.
logger.Trace("Refresh timer interval set to {0}ms", (authlink.ExpiresIn - 600) * 1000);
tokenTimer.Elapsed += TokenTimer_Tick;
tokenTimer.Start();
tokenTimer.Interval = (tokenExpiration - 600) * 1000; //Set the token to refresh 10 minutes before it has to.
tokenTimer.Elapsed += TokenTimer_Elapsed;
return (true, null); return (true, null);
} }
catch (FirebaseAuthException Ex) catch (FirebaseAuthException Ex)
@@ -60,10 +59,31 @@ namespace BodyshopUploader.Utils
} }
} }
private static void TokenTimer_Elapsed(object sender, ElapsedEventArgs e) public static async Task Refresh()
{
logger.Info("Old Token {0}", authlink.RefreshToken);
await authlink.GetFreshAuthAsync();
authlink = await ap.RefreshAuthAsync(authlink);
logger.Info("new Token {0}", authlink.FirebaseToken);
}
private static void Authlink_FirebaseAuthRefreshed(object sender, FirebaseAuthEventArgs e)
{
logger.Debug("Auth token refreshed!");
logger.Debug("New token: {0}", e.FirebaseAuth.FirebaseToken);
}
private static void TokenTimer_Tick(object sender, ElapsedEventArgs e)
{ {
//Gotta do some stuff now that i got a new token! //Gotta do some stuff now that i got a new token!
//Maybe the token auto refreshes? //Maybe the token auto refreshes?
logger.Info("Timer Old Token {0}", authlink.FirebaseToken);
//await authlink.RefreshUserDetails();
//logger.Info("new Token {0}", authlink.FirebaseToken);
//tokenTimer.Stop();
//tokenTimer.Interval = (authlink.ExpiresIn - 600) * 1000; //Set the token to refresh 10 minutes before it has to.
//tokenTimer.Interval = 10000;
//tokenTimer.Start();
} }
} }
} }

View File

@@ -14,9 +14,9 @@ namespace BodyshopUploader.ViewModels
{ {
_testCommand = new RelayCommand( _testCommand = new RelayCommand(
p => true, p => true,
p => async p =>
{ {
logger.Info("test command clicked"); await Utils.Auth.Refresh();
}); });
} }
return _testCommand; return _testCommand;

View File

@@ -1,9 +1,12 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="FirebaseAuthentication.net" version="3.4.0" targetFramework="net472" /> <package id="FirebaseAuthentication.net" version="3.4.0" targetFramework="net472" />
<package id="GraphQL.Client" version="1.0.3" targetFramework="net472" />
<package id="GraphQL.Common" version="1.0.3" targetFramework="net472" />
<package id="Hardcodet.NotifyIcon.Wpf" version="1.0.8" targetFramework="net472" /> <package id="Hardcodet.NotifyIcon.Wpf" version="1.0.8" targetFramework="net472" />
<package id="MaterialDesignColors" version="1.2.2" targetFramework="net472" /> <package id="MaterialDesignColors" version="1.2.2" targetFramework="net472" />
<package id="MaterialDesignThemes" version="3.0.1" targetFramework="net472" /> <package id="MaterialDesignThemes" version="3.0.1" targetFramework="net472" />
<package id="Microsoft.CSharp" version="4.4.1" targetFramework="net472" />
<package id="Microsoft.NETCore.Platforms" version="1.1.0" targetFramework="net472" /> <package id="Microsoft.NETCore.Platforms" version="1.1.0" targetFramework="net472" />
<package id="Microsoft.Win32.Primitives" version="4.3.0" targetFramework="net472" /> <package id="Microsoft.Win32.Primitives" version="4.3.0" targetFramework="net472" />
<package id="NETStandard.Library" version="1.6.1" targetFramework="net472" /> <package id="NETStandard.Library" version="1.6.1" targetFramework="net472" />