diff --git a/source/Plugins/AyaNova.Plugin.V8/Auth.cs b/source/Plugins/AyaNova.Plugin.V8/Auth.cs index 4742074..000799a 100644 --- a/source/Plugins/AyaNova.Plugin.V8/Auth.cs +++ b/source/Plugins/AyaNova.Plugin.V8/Auth.cs @@ -28,14 +28,24 @@ namespace AyaNova.PlugIn.V8 MessageBox.Show("Server could not be reached at that URL\n" + result); } } - private void btnOk_Click(object sender, EventArgs e) + private async void btnOk_Click(object sender, EventArgs e) { + var res = await util.AuthenticateAsync(edUserName.Text,edPassword.Text); + if (!res) + { + MessageBox.Show("Login failed"); + return; + } + + 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) diff --git a/source/Plugins/AyaNova.Plugin.V8/V8.cs b/source/Plugins/AyaNova.Plugin.V8/V8.cs index f231b75..4fb1bfb 100644 --- a/source/Plugins/AyaNova.Plugin.V8/V8.cs +++ b/source/Plugins/AyaNova.Plugin.V8/V8.cs @@ -132,7 +132,14 @@ namespace AyaNova.PlugIn.V8 } Auth d = new Auth(); - d.ShowDialog(); + var res = d.ShowDialog(); ; + if (res == DialogResult.Cancel) + { + return; + } + //here because we logged in fine and can proceed + + MessageBox.Show("Login successful! JWT is " + util.JWT); //Only one command // DumpIt(); } diff --git a/source/Plugins/AyaNova.Plugin.V8/util.cs b/source/Plugins/AyaNova.Plugin.V8/util.cs index 8caae9b..acbb33a 100644 --- a/source/Plugins/AyaNova.Plugin.V8/util.cs +++ b/source/Plugins/AyaNova.Plugin.V8/util.cs @@ -6,6 +6,7 @@ using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; +using Newtonsoft.Json.Linq; namespace AyaNova.PlugIn.V8 @@ -15,9 +16,28 @@ namespace AyaNova.PlugIn.V8 const string TEST_ROUTE = "ServerInfo"; const string API_BASE_ROUTE = "api/v8/"; static HttpClient client = new HttpClient(); + //url once known to be good static string ApiBaseUrl { get; set; } + internal static string JWT { get; set; } + static bool Initialized { get; set; } + + public util() + { + Initialized = false; + JWT = string.Empty; + } + + private static void InitClient() + { + if (Initialized) return; + client.BaseAddress = new Uri(ApiBaseUrl); + client.DefaultRequestHeaders.Accept.Clear(); + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + Initialized = true; + } + /// /// Only a return value of "OK" is ok /// @@ -27,21 +47,22 @@ namespace AyaNova.PlugIn.V8 { if (string.IsNullOrEmpty(serverUrl)) return "Server url required"; - if (!serverUrl.Contains("/api/")) { - if (!serverUrl.EndsWith("/")) serverUrl+="/"; + if (!serverUrl.Contains("/api/")) + { + if (!serverUrl.EndsWith("/")) serverUrl += "/"; serverUrl += API_BASE_ROUTE; } if (!serverUrl.StartsWith("http")) serverUrl = "http://" + serverUrl; - //client.BaseAddress = new Uri("http://localhost:64195/"); - //client.DefaultRequestHeaders.Accept.Clear(); - //client.DefaultRequestHeaders.Accept.Add( - // new MediaTypeWithQualityHeaderValue("application/json")); //try to connect, ping the server api - client.BaseAddress = new Uri(serverUrl); - client.DefaultRequestHeaders.Accept.Clear(); - client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + if (!Initialized) + { + client.BaseAddress = new Uri(serverUrl); + client.DefaultRequestHeaders.Accept.Clear(); + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + Initialized = true; + } try { @@ -49,7 +70,12 @@ namespace AyaNova.PlugIn.V8 if (response.IsSuccessStatusCode) { var ret = await response.Content.ReadAsStringAsync(); - if (ret.Contains("AyaNova")) return "OK"; + if (ret.Contains("AyaNova")) + { + ApiBaseUrl = serverUrl; + return "OK"; + + } } else { @@ -62,7 +88,100 @@ namespace AyaNova.PlugIn.V8 return "failed"; } - //eoc - } + + + public async static Task AuthenticateAsync(string login, string password = null) + { + InitClient(); + + if (password == null) + password = login; + + dynamic creds = new JObject(); + creds.login = login; + creds.password = password; + + ApiResponse a = await PostAsync("Auth", creds.ToString()); + + if (a.HttpResponse.IsSuccessStatusCode) + { + JWT=a.ObjectResponse["data"]["token"].Value(); + return true; + } + + + return false; + } + + + + + + + public async static Task GetAsync(string route, string authToken = null, string bodyJsonData = null) + { + + + var requestMessage = new HttpRequestMessage(HttpMethod.Get, route); + if (!string.IsNullOrWhiteSpace(JWT)) + requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JWT); + + if (!string.IsNullOrWhiteSpace(bodyJsonData)) + requestMessage.Content = new StringContent(bodyJsonData, System.Text.Encoding.UTF8, "application/json"); + + HttpResponseMessage response = await client.SendAsync(requestMessage); + var responseAsString = await response.Content.ReadAsStringAsync(); + + return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) }; + } + + public async static Task PostAsync(string route, string postJson = null) + { + + var requestMessage = new HttpRequestMessage(HttpMethod.Post, route); + if (!string.IsNullOrWhiteSpace(JWT)) + requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JWT); + + if (!string.IsNullOrWhiteSpace(postJson)) + requestMessage.Content = new StringContent(postJson, System.Text.Encoding.UTF8, "application/json"); + + HttpResponseMessage response = await client.SendAsync(requestMessage); + var responseAsString = await response.Content.ReadAsStringAsync(); + + return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) }; + } + + + //eoc + public class ApiResponse + { + public HttpResponseMessage HttpResponse { get; set; } + public JObject ObjectResponse { get; set; } + public string CompactResponse + { + get + { + return ObjectResponse.ToString(Newtonsoft.Json.Formatting.None); + } + } + } + /// + /// + /// + /// + /// + public static JObject Parse(string jsonString) + { + if (string.IsNullOrWhiteSpace(jsonString)) + { + return null; + } + return JObject.Parse(jsonString); + } + + + }//eoc + + }//ens