From 729764aacff555816963217ca89a69ff90ef75b3 Mon Sep 17 00:00:00 2001 From: lchandellier Date: Fri, 26 Jun 2020 12:14:36 +0200 Subject: [PATCH] Ajout des fonctions communes --- BDAccess/APIAccess.cs | 40 ----------------- Commun/APIAccess.cs | 71 +++++++++++++++++++++++++++++++ {BDAccess => Commun}/BDaccess.cs | 4 +- Commun/Function.cs | 32 ++++++++++++++ Commun/KeycloakAuthentificator.cs | 29 +++++++++++++ 5 files changed, 134 insertions(+), 42 deletions(-) delete mode 100644 BDAccess/APIAccess.cs create mode 100644 Commun/APIAccess.cs rename {BDAccess => Commun}/BDaccess.cs (93%) create mode 100644 Commun/Function.cs create mode 100644 Commun/KeycloakAuthentificator.cs diff --git a/BDAccess/APIAccess.cs b/BDAccess/APIAccess.cs deleted file mode 100644 index ffd6ceb..0000000 --- a/BDAccess/APIAccess.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Runtime.CompilerServices; -using System.Text.Json; -using System.Threading.Tasks; - -namespace EPAServeur.BDAccess -{ - public class APIAccess - { - private static APIAccess instance = null; - private string serveur = "http://localhost:3000"; - - private APIAccess() - { - - } - - public static APIAccess getInstance() - { - return instance; - } - - public static List requestAPI(string query) - { - if (instance == null) - { - instance = new APIAccess(); - } - - List result = new List(); - WebClient client = new WebClient(); - result = JsonSerializer.Deserialize>(client.DownloadString(instance.serveur+query)); - - return result; - } - } -} diff --git a/Commun/APIAccess.cs b/Commun/APIAccess.cs new file mode 100644 index 0000000..c5d479b --- /dev/null +++ b/Commun/APIAccess.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Runtime.CompilerServices; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace EPAServeur.BDAccess +{ + public class APIAccess + { + private static APIAccess instance = null; + private string serveur = "http://localhost:3000"; + + private APIAccess() + { + + } + + public static APIAccess getInstance() + { + return instance; + } + + public static List requestGetAPI(string query) + { + if (instance == null) + { + instance = new APIAccess(); + } + + List result = new List(); + WebClient client = new WebClient(); + result = JsonSerializer.Deserialize>(client.DownloadString(instance.serveur+query)); + + return result; + } + public static List requestPostAPI(string query, List> postParam = null) + { + if (instance == null) + { + instance = new APIAccess(); + } + + List result = new List(); + WebClient client = new WebClient(); + client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; + if (postParam != null) + { + foreach (KeyValuePair arg in postParam) + { + client.QueryString.Add(arg.Key, arg.Value); + } + } + + try + { + var data = client.UploadValues(query, "POST", client.QueryString); + result = JsonSerializer.Deserialize>(UnicodeEncoding.UTF8.GetString(data)); + } + catch + { + result = null; + } + return result; + } + + } +} diff --git a/BDAccess/BDaccess.cs b/Commun/BDaccess.cs similarity index 93% rename from BDAccess/BDaccess.cs rename to Commun/BDaccess.cs index 9cc2349..eaee2a2 100644 --- a/BDAccess/BDaccess.cs +++ b/Commun/BDaccess.cs @@ -36,10 +36,10 @@ namespace EPAServeur.BDAccess { instance = new BDAccess(); } + instance.connection.Close(); + instance.connection.Open(); MySqlCommand command = new MySqlCommand(query, instance.connection); - command.Connection.Open(); - return command.ExecuteReader(); } diff --git a/Commun/Function.cs b/Commun/Function.cs new file mode 100644 index 0000000..1da2710 --- /dev/null +++ b/Commun/Function.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Commun +{ + public class Function + { + private static Function instance = null; + + private Function() + { + + } + + public static Function getInstance() + { + return instance; + } + + public static string TransformNetDateTimeToSqlDate(DateTime date) + { + return date.ToString("yyyy'-'MM'-'dd' 'HH':'mm':'ss"); + } + + public static DateTime TransformSqlDateToNetDateTime(string date) + { + return new DateTime(); + } + } +} diff --git a/Commun/KeycloakAuthentificator.cs b/Commun/KeycloakAuthentificator.cs new file mode 100644 index 0000000..127d85c --- /dev/null +++ b/Commun/KeycloakAuthentificator.cs @@ -0,0 +1,29 @@ +using EPAServeur.BDAccess; +using Microsoft.AspNetCore.Http; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; + +namespace EPAServeur.Commun +{ + public class KeycloakAuthentificator + { + private static readonly string host = "http://localhost:8080/auth/realms/master/protocol/openid-connect/userinfo"; + public static bool IsvalidToken(string token) + { + List> param = new List>(); + param.Add(new KeyValuePair("access_token", token)); + + if (APIAccess.requestPostAPI>(host, param) == null) + { + return false; + } + else + { + return true; + } + } + } +}