From 49507512d428a1ae512284001a8e92ba79bf5f50 Mon Sep 17 00:00:00 2001 From: Eamon Bauman Date: Thu, 23 Feb 2023 19:40:53 -0500 Subject: [PATCH] adding time checking (#1) * added notAfter and notBefore checks * ignoring golicense binary --- .gitignore | 3 ++- pkg/client/client.go | 11 +++++++++++ pkg/types/errors.go | 22 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ec9faf1..f7b0386 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ ./golicense *.pem -.idea/ \ No newline at end of file +.idea/ +golicense \ No newline at end of file diff --git a/pkg/client/client.go b/pkg/client/client.go index a2797bf..d9d14e6 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -5,6 +5,7 @@ import ( "fmt" license2 "github.com/ebauman/golicense/pkg/license" "github.com/ebauman/golicense/pkg/types" + "time" ) func Blocking(licenseKey string, usages map[string]int, publicKeys []*rsa.PublicKey) types.LicensingResponse { @@ -25,6 +26,16 @@ func Blocking(licenseKey string, usages map[string]int, publicKeys []*rsa.Public return types.NewLicensingResponse(false, err) } + // it's a license. is it expired? + if license.NotAfter.Before(time.Now()) { + return types.NewLicensingResponse(false, types.NewLicenseExpiredError()) + } + + // are we using it before we should? + if license.NotBefore.After(time.Now()) { + return types.NewLicensingResponse(false, types.NewLicenseNotYetValidError()) + } + // valid license, check usages for k, v := range usages { // check to see if there is a grant for what we're trying to use diff --git a/pkg/types/errors.go b/pkg/types/errors.go index 8e53ed8..6c89175 100644 --- a/pkg/types/errors.go +++ b/pkg/types/errors.go @@ -99,3 +99,25 @@ func (InvalidPublicKeysError) Error() string { func NewInvalidPublicKeysError() InvalidPublicKeysError { return InvalidPublicKeysError{} } + +type LicenseExpiredError struct { +} + +func (LicenseExpiredError) Error() string { + return fmt.Sprintf("license expired") +} + +func NewLicenseExpiredError() LicenseExpiredError { + return LicenseExpiredError{} +} + +type LicenseNotYetValidError struct { +} + +func (LicenseNotYetValidError) Error() string { + return fmt.Sprintf("license not yet valid") +} + +func NewLicenseNotYetValidError() LicenseNotYetValidError { + return LicenseNotYetValidError{} +}