Files
BeWoPlaner/Service/ServiceUtils/Update/UpdateRequestValidator.cs

202 lines
6.9 KiB
C#

using BeWo.Data;
using BeWo.Service.ServiceProxy;
using BS.SharedLauncher.DataContract;
using BS.SharedLauncher.Enums;
using BS.SharedLauncher.Exceptions;
using BS.SharedLauncher.Information;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace BeWo.Service.ServiceUtils.Update
{
public static class UpdateRequestValidator
{
private static string GETSERVER_URL = "https://support.bewoplaner.de/api/getserver.php?k={0}&t=0";
public static string Tenant => MultitenancyOperationContextExt.Current.Tenant;
public static void ValidatePlanRequest(UpdatePlanRequest request)
{
ValidateLogin(request);
if (!TryValidatePlanRequestProperties(request))
throw new HideClientUpdateException("Kann Request nicht bestätigen");
}
public static void ValidateFileRequest(UpdateFileRequest request)
{
if (!TryValidateFileRequestProperties(request))
throw new HideClientUpdateException("Kann Request nicht bestätigen");
if (!TryValidateVersion(request, Tenant))
throw new HideClientUpdateException("Kann Request nicht bestätigen");
if (!TryValidateSource(request))
throw new HideClientUpdateException("Kann Request nicht bestätigen");
ValidateSession(request, Tenant);
}
private static bool TryValidatePlanRequestProperties(UpdatePlanRequest request)
{
if (request.PlanerRootFolderExists)
{
// ...
}
else
{
// Root Folder ex. nicht
if (request.DownloadFolderExists)
return false;
if (!request.DownloadFolderIsEmpty)
return false;
if (request.ApplyFolderExists)
return false;
if (!request.ApplyFolderIsEmpty)
return false;
}
return true;
}
private static void ValidateLogin(UpdatePlanRequest request) => ValidateLogin(request.Login);
private static void ValidateLogin(LoginDC login) => ValidateLogin(login.Username, login.Password, Tenant, login.Pin, login.ClientID);
private static void ValidateLogin(string username, string password, string tenant, string pin, string clientid)
{
string server_address = string.Empty;
try
{
SessionInformation.Login.Username = username;
SessionInformation.Login.Password = password;
SessionInformation.Login.Tenant = tenant;
server_address = GetServerFromTenant();
#if DEBUG
server_address = "http://localhost:3777/Host";
#endif
ServiceFacade.UpdateServerAddresses(server_address);
var result = ServiceFacade.DoLauncherServiceSync(x => x.IsUserValid(username, password, pin, clientid, true));
if (result != UserValidationResult.UserValid)
throw new HideClientUpdateException("Kann Anmeldung nicht bestätigen! Result: " + result.ToString());
}
catch (Exception e)
{
throw new HideClientUpdateException("Kann Anmeldung nicht bestätigen! ", e);
}
}
private static bool TryValidateFileRequestProperties(UpdateFileRequest request)
{
if (string.IsNullOrWhiteSpace(request.UpdateSessionId))
return false;
if (string.IsNullOrWhiteSpace(request.Version))
return false;
if (request.UpdateFile is null)
return false;
if (string.IsNullOrWhiteSpace(request.UpdateFile.RelativePath))
return false;
return true;
}
private static bool TryValidateVersion(UpdateFileRequest request, string tenant)
{
var version_config = UpdateConfigReader.GetValidVersion(tenant);
return request.Version == version_config;
}
private static bool TryValidateSource(UpdateFileRequest request)
{
switch (request.UpdateFile.Source)
{
case UpdateFileSource.Invalid:
return false;
case UpdateFileSource.VersionDefault:
break;
case UpdateFileSource.VersionTenant:
break;
case UpdateFileSource.VersionUser:
return false;
case UpdateFileSource.OverrideTenant:
break;
case UpdateFileSource.OverrideUser:
return false;
default:
return false;
}
return true;
}
private static void ValidateSession(UpdateFileRequest request, string tenant)
{
var error = TryValidateSession(request, tenant);
if (!string.IsNullOrWhiteSpace(error))
throw new HideClientUpdateException(error);
}
private static string TryValidateSession(UpdateFileRequest request, string tenant)
{
var session = UpdateServerSessionManager.CurrentSession(request.UpdateSessionId);
if (session is null)
return $"Session \"{request.UpdateSessionId}\" nicht verfügbar";
if (session.Tenant != tenant)
return $"Tenant konnte nicht bestätigt werden. {session.Tenant} != {tenant}";
if (!session.FileLimiterDict.ContainsKey(
request.UpdateFile.RelativePath))
return $"Angefragte Datei nicht verfügbar. Anfrage an \"{request.UpdateFile.RelativePath}\"";
if (session.IsTimeouted)
return $"Session \"{request.UpdateSessionId}\" nicht mehr verfügbar - Timeout";
return null;
}
private static string GetServerFromTenant(string tenant = null)
{
if (tenant is null)
tenant = SessionInformation.Login.Tenant;
if (tenant == "demo")
return null;
try
{
String url = String.Format(GETSERVER_URL, tenant);
using (WebClient client = new WebClient())
{
//MessageBox.Show(hostAddress);
byte[] response = client.UploadValues(url, "POST", new NameValueCollection());
String server = System.Text.Encoding.ASCII.GetString(response);
return $"https://{server}/service";
}
}
catch (Exception ex)
{
throw new HideClientUpdateException("Login konnte nicht verifiziert werden.", ex);
}
}
}
}