Files
laatzen/Common/Ui/GenesisToolBox/Infrastructure/Software.cs
T

257 lines
9.1 KiB
C#

namespace Xylem.Common.Ui.GenesisToolBox.Infrastructure
{
using CommonCore.Configuration;
using Logic.SoftwareAccessHelper;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// Holds information over current software and logged in user.
/// </summary>
internal static class Software
{
const string Bearer = nameof(Bearer);
private static String bearer;
private static LDAPUser user;
private static IEnumerable<SoftwareFunctions> softwareFunctions = Array.Empty<SoftwareFunctions>();
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()
{
user = new LDAPUser();
var version = SoftwareVersion.Current;
var uri = $"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Login/AD";
var authenticationResponse = LocalWebRequest.PostRequestAndGetResponseAsync(uri, json: new LDAPLoginModel
{
Assembly = version.Name,
BuildV = version.BuildV,
MajorV = version.MajorV,
MinorV = version.MinorV,
EmployeeId = user.EmployeeId
});
if (authenticationResponse.StatusCode == HttpStatusCode.OK)
{
bearer = authenticationResponse
?.Content
?.Trim()
?.Trim('"');
softwareFunctions = GetFunctions();
if (!string.IsNullOrWhiteSpace(bearer))
{
UserName = $"{user.FirstName} {user.LastName}";
}
}
else if (authenticationResponse.StatusCode == HttpStatusCode.Accepted)
{
IsRegistrationPending = true;
}
if (string.IsNullOrWhiteSpace(UserName))
{
UserName = "Options";
}
}
internal static Boolean ChangePassword(String oldPassword, String newPassword, String confirmPassword, out Dictionary<String, String> errors)
{
errors = new Dictionary<string, string>();
var changePasswordModel = new
{
OldPassword = oldPassword.ComputeBase64Hash(),
NewPassword = newPassword.ComputeBase64Hash(),
ConfirmPassword = confirmPassword.ComputeBase64Hash()
};
var changePasswordResponse = changePasswordModel.PutAsJson($"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Login", headers =>
{
headers.Authorization = $"{Bearer} {bearer}";
});
if (changePasswordResponse.StatusCode != HttpStatusCode.OK)
{
if (!string.IsNullOrWhiteSpace(changePasswordResponse.Content))
{
errors = JsonConvert.DeserializeObject<Dictionary<string, string>>(changePasswordResponse.Content);
}
}
return changePasswordResponse.StatusCode == HttpStatusCode.OK;
}
internal static Boolean Register(LDAPUser user, out Dictionary<String, String> errors)
{
errors = new Dictionary<string, string>();
var version = SoftwareVersion.Current;
var registerModel = 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
};
var registerResponse = registerModel.PutAsJson($"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Login/AD");
if (registerResponse.StatusCode == HttpStatusCode.OK)
{
IsRegistrationPending = false;
bearer = registerResponse
?.Content
?.Trim()
?.Trim('"');
softwareFunctions = GetFunctions();
if (!string.IsNullOrWhiteSpace(bearer))
{
UserName = $"{user.FirstName} {user.LastName}";
}
}
else if (!string.IsNullOrWhiteSpace(registerResponse.Content))
{
errors = JsonConvert.DeserializeObject<Dictionary<string, string>>(registerResponse.Content);
}
return registerResponse.StatusCode == HttpStatusCode.OK;
}
/// <summary>
/// Sends login request and obtains an bearer authorization token.
/// </summary>
public static Boolean Login(String username, String password, out Dictionary<String, String> errors)
{
errors = new Dictionary<string, string>();
var loginResponse = LocalWebRequest.PostRequestAndGetResponseAsync(
url: $"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Login/Local",
json: new UserLoginModel
{
Username = username,
Password = password,
Assembly = SoftwareVersion.Current.Name,
MajorV = SoftwareVersion.Current.MajorV,
MinorV = SoftwareVersion.Current.MinorV,
BuildV = SoftwareVersion.Current.BuildV,
});
if (loginResponse.StatusCode == HttpStatusCode.OK)
{
bearer = loginResponse
?.Content
?.Trim()
?.Trim('"');
softwareFunctions = GetFunctions();
if (!string.IsNullOrWhiteSpace(bearer))
{
UserName = LocalWebRequest
.GetRequest(
timeoutMs: 1200,
url: $"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Personalization/Login",
header: new Dictionary<String, String> { { "Authorization", $"{Bearer} {bearer}" } })
.Trim('"');
}
}
else
{
errors = JsonConvert.DeserializeObject<Dictionary<string, string>>(loginResponse.Content);
}
if (string.IsNullOrWhiteSpace(UserName))
{
UserName = "Options";
}
return !string.IsNullOrWhiteSpace(bearer);
}
public static IEnumerable<SoftwareFunctions> GetFunctions()
{
if (string.IsNullOrWhiteSpace(bearer))
{
return Array.Empty<SoftwareFunctions>();
}
try
{
var functionsResponse = LocalWebRequest.GetRequest(
timeoutMs: 1200,
url: $"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Personalization/Software/Access",
header: new Dictionary<String, String> { { "Authorization", $"{Bearer} {bearer}" } });
return JsonConvert
.DeserializeObject<IEnumerable<String>>(functionsResponse)
?.Select(x => Enum.TryParse($"{x}", out SoftwareFunctions f) ? f : default(SoftwareFunctions))
?.Distinct() ?? Array.Empty<SoftwareFunctions>();
}
catch (Exception e)
{
Console.WriteLine(e);
return Array.Empty<SoftwareFunctions>();
}
}
public static Boolean IsEnabled(SoftwareFunctions softwareFunction)
=> softwareFunction != SoftwareFunctions.NONE && softwareFunctions.Contains(softwareFunction);
public static void Logout()
{
UserName = "Options";
bearer = default(String);
softwareFunctions = Array.Empty<SoftwareFunctions>();
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);
}
}
}
/*
* 19207 Stoyan Zlatev +49 (5102) 74 3005 AD True 0 szlatev rd rd NULL 2017-07-18 00:00:00 6271 NULL 0 False Stoyan.Zlatev@xylem.com NULL NULL False False NULL NULL NULL NULL
*/