140 lines
4.3 KiB
C#
140 lines
4.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AyaNovaQBI
|
|
{
|
|
public partial class auth : Form
|
|
{
|
|
public auth()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void auth_Load(object sender, EventArgs e)
|
|
{
|
|
edServerUrl.Text = Properties.Settings.Default.serverurl;
|
|
}
|
|
|
|
private async void btnTest_Click(object sender, EventArgs e)
|
|
{
|
|
if (!ValidateAndCleanServerAddress()) return;
|
|
btnLogin.Enabled = btnTest.Enabled = false;
|
|
var result = await util.InitAndConfirmAddressAsync(edServerUrl.Text);
|
|
btnLogin.Enabled = btnTest.Enabled = true;
|
|
if (result == "OK")
|
|
{
|
|
MessageBox.Show("Server URL is GOOD!");
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Server could not be reached at that URL\n" + result);
|
|
}
|
|
}
|
|
|
|
private async void btnLogin_Click(object sender, EventArgs e)
|
|
{
|
|
if (!ValidateAndCleanServerAddress()) return;
|
|
btnLogin.Enabled = btnTest.Enabled = false;
|
|
|
|
var result = await util.InitAndConfirmAddressAsync(edServerUrl.Text);
|
|
if (result != "OK")
|
|
{
|
|
MessageBox.Show("Server could not be reached at that URL\n" + result);
|
|
btnLogin.Enabled = btnTest.Enabled = true;
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
var res = await util.AuthenticateAsync(edUserName.Text, edPassword.Text);
|
|
if (!res)
|
|
{
|
|
MessageBox.Show("Login failed");
|
|
btnLogin.Enabled = btnTest.Enabled = true;
|
|
this.DialogResult = DialogResult.Cancel;
|
|
return;
|
|
}
|
|
}catch(Exception ex)
|
|
{
|
|
// ---------------------------
|
|
|
|
//---------------------------
|
|
//Error attempting to login:
|
|
|
|
// POST error, code: 401, route: auth / tfa - authenticate
|
|
|
|
//{ "error":{ "code":"2003","message":"ErrorAPI2003"} }
|
|
|
|
// Unauthorized
|
|
|
|
// POSTED OBJECT:
|
|
|
|
// { "pin":"123456","tempToken":"YvIS2IovMIWUYc4NaovwhNcFXviWwEOx4qvpzt8vfQQ"}
|
|
// ---------------------------
|
|
// OK
|
|
// -------------------------- -
|
|
|
|
|
|
MessageBox.Show("Error attempting to login:\r\n" + ex.Message);
|
|
this.DialogResult = DialogResult.Cancel;
|
|
return;
|
|
|
|
}
|
|
|
|
MessageBox.Show($@"TEST - LOGIN SUCCEEDED: \r\
|
|
AyaNovaUserName: {util.AyaNovaUserName}\r\n
|
|
JWT: {util.JWT}\r\n
|
|
AyaNovaUserId: {util.AyaNovaUserId}\r\n
|
|
AyaNovaUserTranslationId: {util.AyaNovaUserTranslationId}\r\n
|
|
AyaNovaUserRoles: {util.AyaNovaUserRoles}\r\n
|
|
AyaNovaUserType: {util.AyaNovaUserType}");
|
|
|
|
|
|
btnLogin.Enabled = btnTest.Enabled = true;
|
|
|
|
this.DialogResult = DialogResult.OK;
|
|
this.Close();
|
|
}
|
|
|
|
private bool ValidateAndCleanServerAddress()
|
|
{
|
|
var serverUrl = edServerUrl.Text;
|
|
Uri u;
|
|
try
|
|
{
|
|
u = new Uri(serverUrl);
|
|
var scheme = u.Scheme;
|
|
var host = u.Host;
|
|
var port = u.Port;
|
|
|
|
edServerUrl.Text = scheme + "://" + host + ":" + port + "/" + util.API_BASE_ROUTE;
|
|
|
|
//client url for notification default links, help links etc
|
|
if ((scheme == "https" && port == 443) || (scheme == "http" && port == 80))
|
|
util.GuessClientUrl = scheme + "://" + host + "/";
|
|
else
|
|
util.GuessClientUrl = scheme + "://" + host + ":" + port + "/";
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("Server address not valid\n" + ex.Message);
|
|
return false;
|
|
}
|
|
|
|
//ok, it's some kind of url, now format it for api access
|
|
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
}
|
|
}
|