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

125 lines
4.1 KiB
C#

using BeWo.Data;
using BeWo.Service.ServiceProxy;
using BS.SharedLauncher.DataContract;
using BS.SharedLauncher.Enums;
using BS.SharedLauncher.Information;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using BeWo.Service.ServiceUtils.Paths;
namespace BeWo.Service.ServiceUtils.Update
{
public static class UpdateRequestValidator
{
/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <returns>Exception string, otherwise null</returns>
public static string IsUpdatePlanRequestValid(UpdatePlanRequest request, string tenant)
{
if (!VerifySession(request.Session, tenant))
return "Session konnte nicht verifiziert werden.";
return null;
}
public static string IsUpdateFileRequestValid(UpdateFileRequest request, string tenant)
{
// Check for Login ?
// Check for File Permissions ?
if (!VerifyVersion(request, tenant))
return $"Version konnte nicht bestätigt werden. Angefragte Version:{request.Version}";
if (!VerifySource(request))
return $"Datei Herkunft konnte nicht bestätigt werden. File:{request.UpdateFile.RelativePath}, Source:{request.UpdateFile.Source}";
if (!ValidPath(request.UpdateFile, request.Version, tenant))
return $"Datei Pfad konnte nicht bestätigt werden. Path:{request.UpdateFile.RelativePath}";
return null;
}
private static bool VerifySession(SessionDC session, string tenant)
{
SessionInformation.Login.Username = session.Username;
SessionInformation.Login.Password = session.Password;
SessionInformation.Login.Tenant = tenant;
ServiceFacade.UpdateServerAddresses(session.CoreServerAddress);
var result = ServiceFacade.DoLauncherServiceSync(x => x.IsUserValid(session.TempUsername, session.Password, session.Pin, session.ClientID, true));
if (result == UserValidationResult.UserValid)
return true;
return false;
}
private static bool VerifyVersion(UpdateFileRequest request, string tenant)
{
var version_config = UpdateConfigReader.GetValidVersion(tenant);
return request.Version == version_config;
}
private static bool VerifySource(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;
}
public static bool ValidPath(UpdateFileDC fileDC, string version, string tenant)
{
var source_folder = ServerFilePaths.GetSourceFolderPath(fileDC.Source, version, tenant);
var requested_path = Path.Combine(source_folder, fileDC.RelativePath);
Regex regex = new Regex(@"([a-zA-Z0-9\s_\\.\-:])+(.dat)$");
Match match = regex.Match(requested_path);
if (match.Success)
{
if (File.Exists(requested_path) && Path.GetFullPath(requested_path).StartsWith(source_folder, StringComparison.OrdinalIgnoreCase))
{
return true;
}
else
{
// "File not found";
return false;
}
}
else
{
// "File name not valid"
return false;
}
}
}
}