Files
ayanova7/source/Plugins/AyaNova.Plugin.V8/Auth.cs
2021-08-13 18:36:54 +00:00

109 lines
3.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.Windows.Forms;
namespace AyaNova.PlugIn.V8
{
public partial class Auth : Form
{
public Auth()
{
InitializeComponent();
}
private async void btnTest_Click(object sender, EventArgs e)
{
if (!ValidateAndCleanServerAddress()) return;
btnOk.Enabled = btnTest.Enabled = false;
var result = await util.InitAndConfirmAddressAsync(edServerUrl.Text);
btnOk.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 btnOk_Click(object sender, EventArgs e)
{
if (!ValidateAndCleanServerAddress()) return;
btnOk.Enabled = btnTest.Enabled = false;
// if (!util.Initialized)
//{
var result = await util.InitAndConfirmAddressAsync(edServerUrl.Text);
if (result != "OK")
{
MessageBox.Show("Server could not be reached at that URL\n" + result);
btnOk.Enabled = btnTest.Enabled = true;
return;
}
// }
var res = await util.AuthenticateAsync(edUserName.Text, edPassword.Text);
if (!res)
{
MessageBox.Show("AyaNova 8 SuperUser account login failed");
btnOk.Enabled = btnTest.Enabled = true;
return;
}
btnOk.Enabled = btnTest.Enabled = true;
this.DialogResult = DialogResult.OK;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.Close();
}
private void Auth_Load(object sender, EventArgs e)
{
//case 3875
System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)3072;
lblPreRelease.Text = util.PRE_RELEASE_VERSION_STRING;
#if (DEBUG)
edServerUrl.Text = "http://192.168.1.56:7575";
edUserName.Text = "superuser";
edPassword.Text = "l3tm3in";
#endif
}
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;
}
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;
}
}
}