200 lines
7.0 KiB
C#
200 lines
7.0 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.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
/// <summary>
|
|
/// Holds information over current software and logged in user.
|
|
/// </summary>
|
|
internal static class Software
|
|
{
|
|
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; } //=> user.Found && string.IsNullOrWhiteSpace(bearer);
|
|
|
|
public static LDAPUser PendingUser => user;
|
|
|
|
public static void Initialize()
|
|
{
|
|
var version = SoftwareVersion.Current;
|
|
user = new LDAPUser();
|
|
|
|
var authenticationResponse = LocalWebRequest.PostRequestAndGetResponseAsync(
|
|
url: $"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Login/AD",
|
|
json: user);
|
|
|
|
if (authenticationResponse.StatusCode == HttpStatusCode.Accepted)
|
|
{
|
|
IsRegistrationPending = true;
|
|
}
|
|
else
|
|
{
|
|
bearer = authenticationResponse
|
|
?.Content
|
|
?.Trim()
|
|
?.Trim('"');
|
|
softwareFunctions = GetFunctions();
|
|
|
|
if (!string.IsNullOrWhiteSpace(bearer))
|
|
{
|
|
UserName = $"{user.FirstName} {user.LastName}";
|
|
}
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(UserName))
|
|
{
|
|
UserName = "Options";
|
|
}
|
|
}
|
|
|
|
internal static Boolean ChangePassword(String text1, String text2, String text3, out Dictionary<String, String> errors)
|
|
{
|
|
errors = new Dictionary<string, string>();
|
|
|
|
var changePasswordResponse = LocalWebRequest.PostRequestAndGetResponseAsync(
|
|
url: $"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Login/AD",
|
|
json: user);
|
|
|
|
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 registerLDAPResponse = LocalWebRequest.PostRequestAndGetResponseAsync(
|
|
url: $"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Login/AD",
|
|
json: user);
|
|
|
|
if (registerLDAPResponse.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
bearer = registerLDAPResponse
|
|
?.Content
|
|
?.Trim()
|
|
?.Trim('"');
|
|
softwareFunctions = GetFunctions();
|
|
}
|
|
else if (!string.IsNullOrWhiteSpace(registerLDAPResponse.Content))
|
|
{
|
|
errors = JsonConvert.DeserializeObject<Dictionary<string, string>>(registerLDAPResponse.Content);
|
|
}
|
|
|
|
return registerLDAPResponse.StatusCode == HttpStatusCode.OK;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sends login request and obtains an bearer authorization token.
|
|
/// </summary>
|
|
public static Boolean Login(String username, String password)
|
|
{
|
|
var authenticationResponse = 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,
|
|
});
|
|
bearer = authenticationResponse
|
|
?.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('"');
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|