From f630f07224908371b066670426fa63b7d89c8bf7 Mon Sep 17 00:00:00 2001 From: luke-else <52086083+luke-else@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:58:30 +0000 Subject: [PATCH] Added User credential authentication --- Controllers/API/APIInterface.cs | 26 +++++++----- Controllers/UserController.cs | 74 +++++++++++++++++++++++++++++++++ Models/JSON/Login.cs | 19 +++++++++ Models/JSON/LoginResponse.cs | 28 +++++++++++++ Views/Home/Index.cshtml | 2 +- 5 files changed, 137 insertions(+), 12 deletions(-) create mode 100644 Controllers/UserController.cs create mode 100644 Models/JSON/Login.cs create mode 100644 Models/JSON/LoginResponse.cs diff --git a/Controllers/API/APIInterface.cs b/Controllers/API/APIInterface.cs index bfefc8f..9977091 100644 --- a/Controllers/API/APIInterface.cs +++ b/Controllers/API/APIInterface.cs @@ -16,12 +16,15 @@ namespace EFB.Controllers.API this.HttpClient.DefaultRequestHeaders.Clear(); - foreach (var Header in Headers) + if (Headers != null) { - this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value); + foreach (var Header in Headers) + { + this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value); + } } - if (!Form.FormAuthenticator.ValidateEndpoint(Endpoint)) + if (Form.FormAuthenticator.ValidateEndpoint(Endpoint)) { var pendingResult = this.HttpClient.GetAsync(Endpoint); @@ -41,22 +44,23 @@ namespace EFB.Controllers.API } - public async Task Post(string Endpoint, Dictionary Headers, object Body){ + public async Task Post(string Endpoint, Dictionary Headers, HttpContent Body){ this.HttpClient = new HttpClient(); - this.HttpClient.DefaultRequestHeaders.Clear(); + //this.HttpClient.DefaultRequestHeaders.Clear(); - foreach (var Header in Headers) + if (Headers != null) { - this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value); + foreach (var Header in Headers) + { + this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value); + } } - StringContent content = new StringContent(JsonConvert.SerializeObject(Body), Encoding.UTF8, "application/json"); - - if (!Form.FormAuthenticator.ValidateEndpoint(Endpoint)) + if (Form.FormAuthenticator.ValidateEndpoint(Endpoint)) { - var pendingResult = this.HttpClient.PostAsync(Endpoint, content); + var pendingResult = this.HttpClient.PostAsync(Endpoint, Body); var result = await pendingResult; diff --git a/Controllers/UserController.cs b/Controllers/UserController.cs new file mode 100644 index 0000000..02bab95 --- /dev/null +++ b/Controllers/UserController.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using System.Net.Http; +using EFB.Models.JSON; +using Microsoft.Extensions.Logging; + +namespace EFB.Controllers +{ + //[Route("[controller]")] + public class UserController : Controller + { + private readonly ILogger _logger; + + public UserController(ILogger logger) + { + _logger = logger; + } + + public IActionResult Index() + { + return View(); + } + + + public async Task Login(string email, string password){ + + if (Form.FormAuthenticator.ValidateEMail(email)) + { + //API Helper + API.APIInterface API = new API.APIInterface(); + + //Dictionary of Formdata to be encoded + Dictionary formData = new Dictionary(); + + formData.Add("grant_type", "client_credentials"); + formData.Add("client_id", email); + formData.Add("client_secret", password); + + HttpContent content = new FormUrlEncodedContent(formData); + + var request = API.Post("https://api.autorouter.aero/v1.0/oauth2/token", null, content); + + //Wait for the response to come through + var response = await request; + + if (response.error != null) + { + + TempData["Error"] = response.error_description; + return RedirectToAction("Index", "Home"); + + }else{ + //Create a user session and continue + return RedirectToAction("Index", "Home"); + } + + }else{ + TempData["Error"] = "Please enter a valid E-Mail"; + return RedirectToAction("Index", "Home"); + } + + } + + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public IActionResult Error() + { + return View("Error!"); + } + } +} \ No newline at end of file diff --git a/Models/JSON/Login.cs b/Models/JSON/Login.cs new file mode 100644 index 0000000..76db256 --- /dev/null +++ b/Models/JSON/Login.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Newtonsoft.Json; +using System.Threading.Tasks; + +namespace EFB.Models.JSON +{ + public class Login + { + [JsonProperty] + public string grant_type { get; set; } + [JsonProperty] + public string client_id { get; set; } + [JsonProperty] + public string client_secret { get; set; } + + } +} \ No newline at end of file diff --git a/Models/JSON/LoginResponse.cs b/Models/JSON/LoginResponse.cs new file mode 100644 index 0000000..765fac9 --- /dev/null +++ b/Models/JSON/LoginResponse.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace EFB.Models.JSON +{ + public class LoginResponse + { + [JsonProperty] + public string access_token { get; set; } + [JsonProperty] + public int expires_in { get; set; } + [JsonProperty] + public string token_type { get; set; } + [JsonProperty] + public string scope { get; set; } + + + [JsonProperty] + public string error { get; set; } = null; + [JsonProperty] + public string error_description { get; set; } = null; + + + } +} \ No newline at end of file diff --git a/Views/Home/Index.cshtml b/Views/Home/Index.cshtml index 6143668..930740a 100644 --- a/Views/Home/Index.cshtml +++ b/Views/Home/Index.cshtml @@ -11,7 +11,7 @@

-
+