This commit is contained in:
2022-06-20 23:24:10 +00:00
parent 7ced9e377c
commit 0526dab167
5 changed files with 182 additions and 16 deletions

View File

@@ -25,7 +25,13 @@ namespace AyaNovaQBI
internal static string GuessClientUrl { get; set; }
internal static string JWT { get; set; }
internal static long AyaNovaUserId { get; set; }
internal static long AyaNovaUserTranslationId { get; set; }
internal static string AyaNovaUserName { get; set; }
internal static AuthorizationRoles AyaNovaUserRoles { get; set; }
internal static UserType AyaNovaUserType { get; set; }
public static void InitClient()
@@ -92,8 +98,8 @@ namespace AyaNovaQBI
var InnerErr = "";
if (ex.InnerException != null)
InnerErr = ex.InnerException.Message;
return false;
// throw new Exception("POST error, route: " + route + "\r\nError:" + Err + "\r\nInner error:" + InnerErr + "\r\nStack:" + ex.StackTrace + "\r\nPOSTED OBJECT:\r\n" + postJson);
throw new Exception("Authentication error, route: AUTH\r\nError:" + Err + "\r\nInner error:" + InnerErr);
}
//ApiResponse a = await PostAsync("auth", creds.ToString());
@@ -107,30 +113,65 @@ namespace AyaNovaQBI
//Get temp token from response
var tempToken = a.ObjectResponse["data"]["tt"].Value<string>();
string tfaPin = null;
bool keepTrying = true;
//get 2fa code and send it in
do
{
tfa t = new tfa();
if (t.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) return false;
tfaPin = t.TFAPin;
string tfaPin = t.TFAPin;
dynamic tfaCreds = new JObject();
tfaCreds.pin = tfaPin;
tfaCreds.tempToken = tempToken;
try
{
var tfaResponse = await TryPostAsync("auth/tfa-authenticate", tfaCreds.ToString(Newtonsoft.Json.Formatting.None));//trypost is no delay
if (ProcessLoginResponse(tfaResponse)) return true;
}
catch(Exception ex)
{
if (!ex.Message.Contains("2003"))//if not an authentication error (bad pin) then throw it back up for display
throw ex;
//otherwise eat it and let them re-enter the pin again to mirror how ayanova web client works
}
} while (keepTrying);
} while (true);
}
else
{
JWT = a.ObjectResponse["data"]["token"].Value<string>();
return true;
return ProcessLoginResponse(a);
}
}
return false;
}
private static bool ProcessLoginResponse(ApiResponse a)
{
if (a.ObjectResponse == null) return false;
if (!a.HttpResponse.IsSuccessStatusCode)
{
return false;
}
if(a.ObjectResponse["data"]["l"].Value<bool>())//license lockout
{
throw new Exception("Server login from QBI is disabled due to AyaNova license issue");
}
JWT = a.ObjectResponse["data"]["token"].Value<string>();
// AyaNovaUserId= a.ObjectResponse["data"]["id"].Value<long>();//nbot here maybe in jwt
//AyaNovaUserTranslationId = a.ObjectResponse["data"]["tid"].Value<long>();
AyaNovaUserName = a.ObjectResponse["data"]["name"].Value<string>();
AyaNovaUserRoles = (AuthorizationRoles)(int.Parse(a.ObjectResponse["data"]["roles"].Value<string>()));
AyaNovaUserType = (UserType)(int.Parse(a.ObjectResponse["data"]["usertype"].Value<string>()));
return true;
}
public async static Task<ApiResponse> GetAsync(string route)