247 lines
8.0 KiB
C#
247 lines
8.0 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// Holds information over current software and logged in user.
|
|
/// </summary>
|
|
public 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()
|
|
{
|
|
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<String>($"{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<String, String> errors)
|
|
{
|
|
errors = new Dictionary<String, String>();
|
|
|
|
var response = new
|
|
{
|
|
OldPassword = oldPassword.ComputeBase64Hash(),
|
|
NewPassword = newPassword.ComputeBase64Hash(),
|
|
ConfirmPassword = confirmPassword.ComputeBase64Hash()
|
|
}
|
|
.PutAsJson<Dictionary<String, String>>($"{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<String, String> errors)
|
|
{
|
|
errors = new Dictionary<String, String>();
|
|
|
|
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<String>($"{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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sends login request and obtains an bearer authorization token.
|
|
/// </summary>
|
|
public static Boolean Login(String username, String password, out Dictionary<String, String> errors)
|
|
{
|
|
bearer = default(String);
|
|
errors = new Dictionary<String, String>();
|
|
|
|
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<String>($"{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<String>(headersHandler: headers =>
|
|
{
|
|
headers.Authorization = $"{Bearer} {bearer}";
|
|
})
|
|
.Value;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
errors = response.Errors;
|
|
}
|
|
|
|
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 response = $"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Personalization/Software/Access"
|
|
.GetAsJson<IEnumerable<SoftwareFunctions>>(headersHandler: headers =>
|
|
{
|
|
headers.Authorization = $"{Bearer} {bearer}";
|
|
});
|
|
|
|
return response.Value;
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
}
|