using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http.Headers; using System.Security.Cryptography; using System.Text; using Xylem.Common.CommonCore.Configuration; using Xylem.Common.Logic.SoftwareAccessHelper; namespace Xylem.Common.Utils.UserAccessCtrl { /// /// Holds information over current software and logged in user. /// public static class Software { const String Bearer = nameof(Bearer); private static String bearer; private static LDAPUser user; private static IEnumerable softwareFunctions = Array.Empty(); public static String UserName { get; private set; } public static Boolean IsRegistrationPending { get; set; } public static LDAPUser PendingUser => user; public static Boolean IsAutenticated => !string.IsNullOrWhiteSpace(bearer); public static AuthenticationHeaderValue AuthorizationHeader = new AuthenticationHeaderValue(Bearer, bearer); public static void Initialize() { bearer = default(String); user = new LDAPUser(); var version = SoftwareVersion.Current; var response = new LDAPLoginModel { Assembly = version.Name, BuildV = version.BuildV, MajorV = version.MajorV, MinorV = version.MinorV, EmployeeId = user.EmployeeId } .PostAsJson($"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Login/AD"); if (response.HttpStatusCode == HttpStatusCode.Accepted) { IsRegistrationPending = true; } else if (response.HttpStatusCode == HttpStatusCode.OK) { bearer = response .Value ?.Trim() ?.Trim('"'); softwareFunctions = GetFunctions(); if (!string.IsNullOrWhiteSpace(bearer)) { UserName = $"{user.FirstName} {user.LastName}"; } } if (string.IsNullOrWhiteSpace(UserName)) { UserName = "Options"; } } internal static Boolean ChangePassword(String oldPassword, String newPassword, String confirmPassword, out Dictionary errors) { errors = new Dictionary(); var response = new { OldPassword = oldPassword.ComputeBase64Hash(), NewPassword = newPassword.ComputeBase64Hash(), ConfirmPassword = confirmPassword.ComputeBase64Hash() } .PutAsJson>($"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Login", headers => { headers.Authorization = $"{Bearer} {bearer}"; }); var succeeded = response.HttpStatusCode == HttpStatusCode.OK; if (!succeeded) { errors = response.Errors; } return succeeded; } internal static Boolean Register(LDAPUser user, out Dictionary errors) { errors = new Dictionary(); var version = SoftwareVersion.Current; var response = new LDAPRegisterModel { Assembly = version.Name, BuildV = version.BuildV, ConfirmPassword = user.ConfirmPassword, DomainName = user.DomainName, Email = user.Email, EmployeeId = user.EmployeeId, FirstName = user.FirstName, LastName = user.LastName, MajorV = version.MajorV, MinorV = version.MinorV, Password = user.Password, Phone = user.Phone, Username = user.Username } .PutAsJson($"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Login/AD"); var succeeded = response.HttpStatusCode == HttpStatusCode.OK; if (succeeded) { IsRegistrationPending = false; bearer = response ?.Value ?.Trim() ?.Trim('"'); softwareFunctions = GetFunctions(); if (!string.IsNullOrWhiteSpace(bearer)) { UserName = $"{user.FirstName} {user.LastName}"; } } else { errors = response.Errors; } return succeeded; } /// /// Sends login request and obtains an bearer authorization token. /// public static Boolean Login(String username, String password, out Dictionary errors) { bearer = default(String); errors = new Dictionary(); var response = new UserLoginModel { Username = username, Password = password, Assembly = SoftwareVersion.Current.Name, MajorV = SoftwareVersion.Current.MajorV, MinorV = SoftwareVersion.Current.MinorV, BuildV = SoftwareVersion.Current.BuildV, } .PostAsJson($"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Login/Local"); if (response.HttpStatusCode == HttpStatusCode.OK) { bearer = response .Value ?.Trim() ?.Trim('"'); softwareFunctions = GetFunctions(); if (!string.IsNullOrWhiteSpace(bearer)) { UserName = $"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Login" .GetAsJson(headersHandler: headers => { headers.Authorization = $"{Bearer} {bearer}"; }) .Value; } } else { errors = response.Errors; } if (string.IsNullOrWhiteSpace(UserName)) { UserName = "Options"; } return !string.IsNullOrWhiteSpace(bearer); } public static IEnumerable GetFunctions() { if (string.IsNullOrWhiteSpace(bearer)) { return Array.Empty(); } try { var response = $"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Personalization/Software/Access" .GetAsJson>(headersHandler: headers => { headers.Authorization = $"{Bearer} {bearer}"; }); return response.Value; } catch (Exception e) { Console.WriteLine(e); return Array.Empty(); } } public static Boolean IsEnabled(SoftwareFunctions softwareFunction) => softwareFunction != SoftwareFunctions.NONE && softwareFunctions.Contains(softwareFunction); public static void Logout() { UserName = "Options"; bearer = default(String); softwareFunctions = Array.Empty(); //Initialize(); } internal static String ComputeBase64Hash(this String plainText) { if (string.IsNullOrWhiteSpace(plainText)) { return plainText; } var buffer = Encoding.UTF8.GetBytes(plainText); var hash = SHA256.Create().ComputeHash(buffer); return Convert.ToBase64String(hash); } } }