8000 Completed validation interfaces to allow custom validation extensions… by Kaon68 · Pull Request #42 · dnauck/Portable.Licensing · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Completed validation interfaces to allow custom validation extensions… #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Portable.Licensing/Validation/ILicenseValidator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright © 2012 - 2013 Nauck IT KG http://www.nauck-it.de
//
// Author:
Expand Down Expand Up @@ -49,5 +49,10 @@ public interface ILicenseValidator
/// will be returned to the application when the <see cref="ILicenseValidator"/> fails.
/// </summary>
IValidationFailure FailureResult { get; set; }

/// <summary>
/// Gets the <see cref="IValidationChain"/> to continue.
/// </summary>
IValidationChain ValidationChain { get; }
}
}
7 changes: 6 additions & 1 deletion src/Portable.Licensing/Validation/IStartValidationChain.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright © 2012 - 2013 Nauck IT KG http://www.nauck-it.de
//
// Author:
Expand Down Expand Up @@ -31,5 +31,10 @@ namespace Portable.Licensing.Validation
/// </summary>
public interface IStartValidationChain : IFluentInterface
{
/// <summary>
/// Starts the <see cref="ILicenseValidator" /> chain.
/// </summary>
/// <returns>License validator.</returns>
ILicenseValidator StartValidatorChain();
}
}
50 changes: 23 additions & 27 deletions src/Portable.Licensing/Validation/LicenseValidationExtensions.cs
10000
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright © 2012 - 2013 Nauck IT KG http://www.nauck-it.de
//
// Author:
Expand Down Expand Up @@ -51,17 +51,16 @@ public static IStartValidationChain Validate(this License license)
/// <returns>An instance of <see cref="IStartValidationChain"/>.</returns>
public static IValidationChain ExpirationDate(this IStartValidationChain validationChain)
{
var validationChainBuilder = (validationChain as ValidationChainBuilder);
var validator = validationChainBuilder.StartValidatorChain();
var validator = validationChain.StartValidatorChain();
validator.Validate = license => license.Expiration > DateTime.Now;

validator.FailureResult = new LicenseExpiredValidationFailure()
{
Message = "Licensing for this product has expired!",
HowToResolve = @"Your license is expired. Please contact your distributor/vendor to renew the license."
};
{
Message = "Licensing for this product has expired!",
HowToResolve = @"Your license is expired. Please contact your distributor/vendor to renew the license."
};

return validationChainBuilder;
return validator.ValidationChain;
}

/// <summary>
Expand All @@ -73,23 +72,22 @@ public static IValidationChain ExpirationDate(this IStartValidationChain validat
/// <returns>An instance of <see cref="IStartValidationChain"/>.</returns>
public static IValidationChain ProductBuildDate(this IStartValidationChain validationChain, Assembly[] assemblies)
{
var validationChainBuilder = (validationChain as ValidationChainBuilder);
var validator = validationChainBuilder.StartValidatorChain();
var validator = validationChain.StartValidatorChain();
validator.Validate = license => assemblies.All(
asm =>
asm.GetCustomAttributes(typeof (AssemblyBuildDateAttribute), false)
asm.GetCustomAttributes(typeof(AssemblyBuildDateAttribute), false)
.Cast<AssemblyBuildDateAttribute>()
.All(a => a.BuildDate < license.Expiration));

validator.FailureResult = new LicenseExpiredValidationFailure()
{
Message = "Licensing for this product has expired!",
HowToResolve = @"Your license is expired. Please contact your distributor/vendor to renew the license."
};
{
Message = "Licensing for this product has expired!",
HowToResolve = @"Your license is expired. Please contact your distributor/vendor to renew the license."
};

return validationChainBuilder;
return validator.ValidationChain;
}

/// <summary>
/// Allows you to specify a custom assertion that validates the <see cref="License"/>.
/// </summary>
Expand All @@ -99,13 +97,12 @@ public static IValidationChain ProductBuildDate(this IStartValidationChain valid
/// <returns>An instance of <see cref="IStartValidationChain"/>.</returns>
public static IValidationChain AssertThat(this IStartValidationChain validationChain, Predicate<License> predicate, IValidationFailure failure)
{
var validationChainBuilder = (validationChain as ValidationChainBuilder);
var validator = validationChainBuilder.StartValidatorChain();
var validator = validationChain.StartValidatorChain();

validator.Validate = predicate;
validator.FailureResult = failure;

return validationChainBuilder;
return validator.ValidationChain;
}

/// <summary>
Expand All @@ -116,17 +113,16 @@ public static IValidationChain AssertThat(this IStartValidationChain validationC
/// <returns>An instance of <see cref="IStartValidationChain"/>.</returns>
public static IValidationChain Signature(this IStartValidationChain validationChain, string publicKey)
{
var validationChainBuilder = (validationChain as ValidationChainBuilder);
var validator = validationChainBuilder.StartValidatorChain();
var validator = validationChain.StartValidatorChain();
validator.Validate = license => license.VerifySignature(publicKey);

validator.FailureResult = new InvalidSignatureValidationFailure()
{
Message = "License signature validation error!",
HowToResolve = @"The license signature and data does not match. This usually happens when a license file is corrupted or has been altered."
};
{
Message = "License signature validation error!",
HowToResolve = @"The license signature and data does not match. This usually happens when a license file is corrupted or has been altered."
};

return validationChainBuilder;
return validator.ValidationChain;
}
}
}
16 changes: 15 additions & 1 deletion src/Portable.Licensing/Validation/LicenseValidator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright © 2012 - 2013 Nauck IT KG http://www.nauck-it.de
//
// Author:
Expand Down Expand Up @@ -49,5 +49,19 @@ internal class LicenseValidator : ILicenseValidator
/// will be returned to the application when the <see cref="ILicenseValidator"/> fails.
/// </summary>
public IValidationFailure FailureResult { get; set; }

/// <summary>
/// Gets the validation chain.
/// </summary>
public IValidationChain ValidationChain { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="LicenseValidator" /> class.
/// </summary>
/// <param name="chain">The chain.</param>
public LicenseValidator(IValidationChain chain)
{
this.ValidationChain = chain;
}
}
}
10 changes: 5 additions & 5 deletions src/Portable.Licensing/Validation/ValidationChainBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright © 2012 - 2013 Nauck IT KG http://www.nauck-it.de
//
// Author:
Expand Down Expand Up @@ -42,7 +42,7 @@ public ValidationChainBuilder(License license)

public ILicenseValidator StartValidatorChain()
{
return currentValidatorChain = new LicenseValidator();
return currentValidatorChain = new LicenseValidator(this);
}

public void CompleteValidatorChain()
Expand Down Expand Up @@ -79,9 +79,9 @@ public IEnumerable<IValidationFailure> AssertValidLicense()
if (!validator.Validate(license))
yield return validator.FailureResult
?? new GeneralValidationFailure
{
Message = "License validation failed!"
};
{
Message = "License validation failed!"
};
}
}
}
Expand Down
0