using System;
using System.Xml;
using System.IO;
namespace AnticipatingMinds.PlatformServices.Licensing{
public enum LicenseType
{
Unknown,
RegistrationInformationLock,
TimeExpiration
}
[Flags]
public enum LicensingState
{
LicenseNotFound = 0x01,
InvalidLicenseSignature = 0x02,
LicensedProductIdMistmatch = 0x04,
LicensedProductVersionMistmatch = 0x08,
LicenseHasExpired = 0x20,
LicenseIsValid = 0x40
}
/// <summary>
/// Summary description for License.
/// </summary>
public sealed class License
{
internal License(XmlElement licenseElement,LicenseStore licenseStore)
{
if(licenseElement == null)
throw new ArgumentNullException("licenseElement");
if(licenseStore == null)
throw new ArgumentNullException("licenseStore");
this.licenseElement = licenseElement;
this.licenseStore = licenseStore;
if(licenseElement["LicenseType"].InnerText == "RegistrationInformationLock")
licenseType = LicenseType.RegistrationInformationLock;
if(licenseElement["LicenseType"].InnerText == "TimeExpiration")
licenseType = LicenseType.TimeExpiration;
}
public LicenseType LicenseType
{
get
{
return licenseType;
}
}
public string LicenseId
{
get
{
return GetStringValue("LicenseId");
}
}
public string ProductId
{
get
{
return GetStringValue("LicensedProductId");
}
}
public string ProductName
{
get
{
return GetStringValue("LicensedProductName");
}
}
public Version LicensedVersion
{
get
{
XmlElement licensedVersion = licenseElement["LicensedProductVersion"];
if(licensedVersion == null)
throw new LicenseFormatException("LicensedProductVersion element is missing");
return new Version(licensedVersion.InnerText);
}
}
public string GetStringValue(string valueElementName)
{
if(licenseElement.SelectSingleNode(valueElementName) == null)
return string.Empty;
return licenseElement.SelectSingleNode(valueElementName).InnerText;
}
public bool IsValid(string productId,Version productVersion)
{
return GetLicenseStatus(productId,productVersion) == LicensingState.LicenseIsValid;
}
public void ValidateSignature()
{
//Check license integrity
if(!licenseStore.IsLicenseSignatureValid(licenseElement))
throw new LicenseSignatureMismatchException(this);
}
public LicensingState GetLicenseStatus(string productId,Version productVersion)
{
//Check license integrity
if(!licenseStore.IsLicenseSignatureValid(licenseElement))
return LicensingState.InvalidLicenseSignature;
//Check common license properties
if(productId != ProductId)
return LicensingState.LicensedProductIdMistmatch;
if(!IsLicensedVersion(productVersion))
return LicensingState.LicensedProductVersionMistmatch;
//Check expiration Date if exists
if(licenseElement["ExpirationDate"] != null)
{
DateTime expirationDate = DateTime.Parse(licenseElement["ExpirationDate"].InnerText);
if(DateTime.Now > expirationDate)
return LicensingState.LicenseHasExpired;
}
return LicensingState.LicenseIsValid;
}
public void Validate(string productId,Version productVersion)
{
switch(GetLicenseStatus(productId,productVersion))
{
case LicensingState.InvalidLicenseSignature:
throw new LicenseSignatureMismatchException(this);
case LicensingState.LicensedProductIdMistmatch:
throw new LicenseProductIdMismatchException(this);
case LicensingState.LicensedProductVersionMistmatch:
throw new LicenseProductVersionMismatchException(this);
case LicensingState.LicenseHasExpired:
throw new LicenseExpiredException(this);
}
}
internal XmlElement GetLicenseElement()
{
return licenseElement;
}
private bool IsLicensedVersion(Version ProductVersion)
{
if(ProductVersion.Major == LicensedVersion.Major &&
ProductVersion.Minor == LicensedVersion.Minor &&
ProductVersion.Build <= LicensedVersion.Build &&
ProductVersion.Revision <= LicensedVersion.Revision)
return true;
else
return false;
}
private XmlElement licenseElement;
private LicenseStore licenseStore;
private LicenseType licenseType = LicenseType.Unknown;
}
}
|