173 lines
5.7 KiB
C#
173 lines
5.7 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.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 IEnumerable<SoftwareFunctions> softwareFunctions = Array.Empty<SoftwareFunctions>();
|
|
|
|
public static String UserName;
|
|
|
|
private static Boolean HasUser => !string.IsNullOrWhiteSpace(bearer);
|
|
|
|
public static void Initialize()
|
|
{
|
|
var version = SoftwareVersion.Current;
|
|
var user = new LDAPUser();
|
|
|
|
Login(new ADLoginModel
|
|
{
|
|
Assembly = version.AssemblyName.Name,
|
|
Email = user.Email,
|
|
EmployeeId = user.EmployeeId,
|
|
FirstName = user.FirstName,
|
|
LastName = user.LastName,
|
|
Phone = user.Phone,
|
|
Username = user.Username,
|
|
MajorV = version.MajorV,
|
|
MinorV = version.MinorV,
|
|
BuildV = version.BuildV,
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sends login request and obtains an bearer authorization token.
|
|
/// </summary>
|
|
/// <param name="model"><see cref="ADLoginModel"/> - containing all software and user info required for authentication.</param>
|
|
public static void Login(ADLoginModel model)
|
|
{
|
|
var authenticationResponse = LocalWebRequest.PostRequestAndGetResponseAsync(
|
|
url: $"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Login/AD",
|
|
json: model);
|
|
bearer = authenticationResponse
|
|
?.Content
|
|
?.Trim()
|
|
?.Trim('"');
|
|
softwareFunctions = GetFunctions();
|
|
|
|
if (HasUser)
|
|
{
|
|
UserName = $"{model.FirstName} {model.LastName}";
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(UserName))
|
|
{
|
|
UserName = "Options";
|
|
}
|
|
}
|
|
|
|
/// <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 (HasUser)
|
|
{
|
|
try
|
|
{
|
|
UserName = LocalWebRequest
|
|
.GetRequest(
|
|
timeoutMs: 1200,
|
|
url: $"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Personalization/Login",
|
|
header: new Dictionary<String, String> { { "Authorization", $"Bearer {bearer}" } })
|
|
.Trim('"');
|
|
}
|
|
catch (Exception )
|
|
{
|
|
// ignored
|
|
}
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(UserName))
|
|
{
|
|
UserName = "Options";
|
|
}
|
|
|
|
return HasUser;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|