patbef-ServiceOutside/ServiceShared/Models/Request/Subscribe.cs

152 lines
5.2 KiB
C#
Raw Permalink Normal View History

2024-01-29 16:27:34 +01:00
using ServiceShared.Models.Response.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static ServiceShared.Models.Database.Device;
using static ServiceShared.Models.Database.Results;
namespace ServiceShared.Models.Request
{
public class Subscribe
{
/// <summary>
/// SHA512 - Hash of Plz Geburtsdatum Sampleid
/// </summary>
public string pgs { get; set; }
/// <summary>
/// AES encrypted hash of Plz Geburtsdatum Sampleid, that can decrypted by inside service
/// </summary>
public string pgs_hash { get; set; }
/// <summary>
/// Unique id of mobile device
/// </summary>
public string udid { get; set; }
/// <summary>
/// DeviceToken for notification service
/// </summary>
public string device_token { get; set; }
/// <summary>
/// Type of mobile device
/// </summary>
public DeviceTypes device_type { get; set; }
/// <summary>
/// Public Key of mobile device
/// </summary>
public string client_public_key { get; set; }
/// <summary>
/// Client verification hash
/// </summary>
public string verificator_hash { get; set; }
/// <summary>
/// Client PIN that can reset password on the same device
/// </summary>
public string pin { get; set; }
/// <summary>
/// Validates a Subscribe object
/// </summary>
/// <returns>Returns exception if some of validation fails</returns>
public static ResponseException Validate(Subscribe subscribe)
{
if (subscribe == null)
{
return new InvalidRequestException();
}
else if (string.IsNullOrEmpty(subscribe.pgs))
{
return new MissingArgumentException("pgs");
}
else if (string.IsNullOrEmpty(subscribe.udid))
{
return new MissingArgumentException("udid");
}
else if (string.IsNullOrEmpty(subscribe.device_token))
{
return new MissingArgumentException("device_token");
}
else if (string.IsNullOrEmpty(subscribe.client_public_key))
{
return new MissingArgumentException("client_public_key");
}
else if (subscribe.device_type == DeviceTypes.NONE)
{
return new MissingArgumentException("device_type");
}
else if (subscribe.pgs.Length != 128)
{
return new InvalidArgumentException("pgs", "wrong length");
}
else if (subscribe.udid.Length < 10 || subscribe.udid.Length > 64)
{
return new InvalidArgumentException("udid", "wrong length");
}
else if (subscribe.device_token.Length < 10 || subscribe.device_token.Length > 255)
{
return new InvalidArgumentException("device_token", "wrong length");
}
else if (subscribe.client_public_key.Length < 32)
{
return new InvalidArgumentException("client_public_key", "wrong length");
}
else if (string.IsNullOrEmpty(subscribe.verificator_hash))
{
return new MissingArgumentException("verificator_hash");
}
else if (subscribe.verificator_hash.Length != 128)
{
return new InvalidArgumentException("verificator_hash", "wrong length");
}
else
{
return null;
}
}
/// <summary>
/// Converts CheckFileChecksum object to the subscribe object and validates it at least
/// </summary>
/// <param name="checkFileChecksum">CheckFileChecksum object that should be validated</param>
/// <returns>Returns exception if some of validation fails</returns>
public static ResponseException Validate(CheckFileChecksum checkFileChecksum)
{
return Validate(new Subscribe()
{
device_token = checkFileChecksum.device_token,
device_type = checkFileChecksum.device_type,
pgs = checkFileChecksum.pgs,
client_public_key = checkFileChecksum.client_public_key,
udid = checkFileChecksum.udid,
verificator_hash = checkFileChecksum.verificator_hash
});
}
/// <summary>
/// Converts current subscribe object to the Database.Results
/// </summary>
/// <returns>returns Database.Results</returns>
public Database.Results ToResults()
{
return new Database.Results()
{
PGS = this.pgs,
PGS_HASH = this.pgs_hash,
UDID = this.udid,
DeviceToken = this.device_token,
DeviceType = this.device_type,
ClientPublicKey = this.client_public_key,
VerificationHash = this.verificator_hash,
PIN = this.pin
};
}
}
}