From f482c27cd73db033642767be9b6de8410b5bc9be Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Wed, 2 Oct 2019 23:55:46 +0000 Subject: [PATCH] --- source/Plugins/AyaNova.Plugin.QBOI/Util.cs | 45 +++++++++++++++++++--- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/source/Plugins/AyaNova.Plugin.QBOI/Util.cs b/source/Plugins/AyaNova.Plugin.QBOI/Util.cs index a55d738..43a32c4 100644 --- a/source/Plugins/AyaNova.Plugin.QBOI/Util.cs +++ b/source/Plugins/AyaNova.Plugin.QBOI/Util.cs @@ -296,12 +296,20 @@ namespace AyaNova.PlugIn.QBOI const string KAT = "xZ\v\nQ\a(\fN\nX\0\a}\vO[\vZTV_\fWQ,\vV\v-"; const string KOACS = "Y?,V\f1^\0\r+\v!<0-1$\n7-9\fV\0?%#3"; + //oAuth2 new variables + public static Intuit.Ipp.OAuth2PlatformClient.OAuth2Client oac = null; const string Q2_CLIENT_ID = "ABj70Wv5gDauFd9KgKFwuvpQjfzTwEgodEG8tnBbS8mSQhNrZJ"; const string Q2_CLIENT_SECRET = "XUmJyvEcEuwQuyhARUAm0a8G3gzbEAeMiATCLyFZ"; const string Q2_REDIRECT_URL = "https://qboauth.ayanova.com/redirect"; const string Q2_ENVIRONMENT = "Sandbox"; + const double Q2_TOKEN_LIFESPAN_MINUTES = 45;//Minutes to hold on to access token before refreshing. Right now oct 2019 it's one hour lifespan. static string QBOI2_SESSION_TOKEN = ""; + public static string ACCESS_TOKEN { get; set; } + public static string REFRESH_TOKEN { get; set; } + public static string REALM_ID { get; set; } + public static DateTime TOKEN_BIRTHDAY { get; set; } + //simple xor encryption private static string Ec(string text, string key) @@ -363,6 +371,31 @@ namespace AyaNova.PlugIn.QBOI } } + /// + /// Refresh token if necessary, call this regularly + /// will check if access token is approaching expiry (1 hour) + /// and will refresh as necessary + /// + public static void RefreshTokens() + { + DateTime dtExpireAfter=DateTime.Now.AddMinutes( Q2_TOKEN_LIFESPAN_MINUTES); + + if (TOKEN_BIRTHDAY < dtExpireAfter) + { + return; + } + + //refresh token here + + var v= oac.RefreshTokenAsync(REFRESH_TOKEN).Result; + TOKEN_BIRTHDAY = DateTime.Now; + ACCESS_TOKEN = v.AccessToken; + REFRESH_TOKEN = v.RefreshToken; + + } + + + static ServiceContext SC = null; //static DesktopIppOAuth.OAuthConnector CN; @@ -411,13 +444,15 @@ namespace AyaNova.PlugIn.QBOI } * */ - string ACCESS_TOKEN = j["access_token"].Value(); - string REFRESH_TOKEN = j["refresh_token"].Value(); - string REALM_ID = j["realmId"].Value(); + ACCESS_TOKEN = j["access_token"].Value(); + REFRESH_TOKEN = j["refresh_token"].Value(); + REALM_ID = j["realmId"].Value(); + ///***************************************** TESTING FIX THIS LATER *********************** + TOKEN_BIRTHDAY = DateTime.Now.AddMinutes(-90);//TESTING TESTING TESTING //NOTE: this app will handle refreshing the token in-session as apparently that's possible from here, maybe should test that or could simply just refresh via our server - Intuit.Ipp.OAuth2PlatformClient.OAuth2Client oac = new OAuth2Client(Q2_CLIENT_ID, Q2_CLIENT_SECRET, Q2_REDIRECT_URL, Q2_ENVIRONMENT); + oac = new OAuth2Client(Q2_CLIENT_ID, Q2_CLIENT_SECRET, Q2_REDIRECT_URL, Q2_ENVIRONMENT); //case 3669 //https://help.developer.intuit.com/s/question/0D50f00005gFeqpCAC/how-do-i-suddenly-get-there-was-an-error-while-communicating-with-the-ids-server @@ -563,7 +598,7 @@ namespace AyaNova.PlugIn.QBOI } } - + RefreshTokens(); IntegrationLog.Log(QBID, "PFC: Authentication completed, validating company data");