Esempio n. 1
0
        public static int ValidateActivationStatus(string productId, ActivationPayload activationPayload)
        {
            var now = GetUtcTimestamp();

            if (activationPayload.LeaseExpiresAt != 0 && (activationPayload.LeaseExpiresAt < now || activationPayload.LeaseExpiresAt < activationPayload.IssuedAt))
            {
                LexDataStore.ResetValue(productId, LexConstants.KEY_ACTIVATION_JWT);
                return(LexStatusCodes.LA_FAIL);
            }

            bool skipGracePeriodCheck = (activationPayload.ServerSyncInterval == 0 || activationPayload.ServerSyncGracePeriodExpiresAt == 0);

            if (!skipGracePeriodCheck && activationPayload.ServerSyncGracePeriodExpiresAt < now)
            {
                return(LexStatusCodes.LA_GRACE_PERIOD_OVER);
            }
            else if (activationPayload.ExpiresAt != 0 && activationPayload.ExpiresAt < now)
            {
                return(LexStatusCodes.LA_EXPIRED);
            }

            else if (activationPayload.ExpiresAt != 0 && activationPayload.ExpiresAt < activationPayload.IssuedAt)
            {
                return(LexStatusCodes.LA_EXPIRED);
            }

            else if (activationPayload.Suspended)
            {
                return(LexStatusCodes.LA_SUSPENDED);
            }
            else
            {
                return(LexStatusCodes.LA_OK);
            }
        }
 public static int DeactivationErrorHandler(string productId, HttpResponseMessage httpResponse)
 {
     if (httpResponse.StatusCode == HttpStatusCode.InternalServerError || httpResponse.StatusCode == HttpStatusCode.ServiceUnavailable)
     {
         return(LexStatusCodes.LA_E_SERVER);
     }
     if (httpResponse.StatusCode == (HttpStatusCode)LexConstants.HttpTooManyRequests)
     {
         return(LexStatusCodes.LA_E_RATE_LIMIT);
     }
     if (httpResponse.StatusCode == HttpStatusCode.NotFound)
     {
         LexDataStore.ResetValue(productId, LexConstants.KEY_ACTIVATION_JWT);
         return(LexStatusCodes.LA_E_ACTIVATION_NOT_FOUND);
     }
     if (httpResponse.StatusCode == HttpStatusCode.Conflict)
     {
         var errorResponse = JsonConvert.DeserializeObject <HttpErrorResponse>(httpResponse.Content.ReadAsStringAsync().Result);
         if (errorResponse.Code == LexConstants.ActivationErrorCodes.DEACTIVATION_LIMIT_REACHED)
         {
             return(LexStatusCodes.LA_E_DEACTIVATION_LIMIT);
         }
     }
     return(LexStatusCodes.LA_E_CLIENT);
 }
Esempio n. 3
0
        public static bool ValidateSystemTime(string productId)
        {
            var now = GetUtcTimestamp();
            var lastRecordedTimeStr = LexDataStore.GetValue(productId, LexConstants.KEY_LAST_RECORDED_TIME);

            if (ValidateTime((long)Int32.Parse(lastRecordedTimeStr), LexConstants.ALLOWED_CLOCK_OFFSET))
            {
                LexDataStore.SaveValue(productId, LexConstants.KEY_LAST_RECORDED_TIME, now.ToString());
                return(true);
            }
            return(false);
        }
Esempio n. 4
0
        public static int ValidateActivation(string jwt, string publicKey, string licenseKey, string productId, ActivationPayload activationPayload)
        {
            string payload = LexJwtService.VerifyToken(jwt, publicKey);

            if (String.IsNullOrEmpty(payload))
            {
                return(LexStatusCodes.LA_FAIL);
            }
            var tempActivationPayload = JsonConvert.DeserializeObject <ActivationPayload>(payload);

            activationPayload.CopyProperties(tempActivationPayload);
            activationPayload.IsValid = true;
            int status;

            if (licenseKey != activationPayload.Key)
            {
                status = LexStatusCodes.LA_FAIL;
            }
            else if (productId != activationPayload.ProductId)
            {
                status = LexStatusCodes.LA_FAIL;
            }
            else if (activationPayload.Fingerprint != LexSystemInfo.GetFingerPrint())
            {
                status = LexStatusCodes.LA_E_MACHINE_FINGERPRINT;
            }
            else if (!ValidateTime(activationPayload.IssuedAt, activationPayload.AllowedClockOffset))
            {
                status = LexStatusCodes.LA_E_TIME;
            }
            else
            {
                status = LexValidator.ValidateActivationStatus(productId, activationPayload);
            }
            if (status == LexStatusCodes.LA_OK || status == LexStatusCodes.LA_EXPIRED || status == LexStatusCodes.LA_SUSPENDED || status == LexStatusCodes.LA_GRACE_PERIOD_OVER)
            {
                var now = GetUtcTimestamp();
                LexDataStore.SaveValue(productId, LexConstants.KEY_LAST_RECORDED_TIME, now.ToString());
                LexDataStore.SaveValue(productId, LexConstants.KEY_ACTIVATION_JWT, jwt);
            }
            else
            {
                LexDataStore.SaveValue(productId, LexConstants.KEY_LAST_RECORDED_TIME, activationPayload.IssuedAt.ToString());
            }
            return(status);
        }
        public static int DeactivateFromServer(string productId, ActivationPayload activationPayload)
        {
            var httpService = new LexHttpService();
            HttpResponseMessage httpResponse;

            try
            {
                httpResponse = httpService.DeleteActivation(activationPayload.Id);
            }
            catch (Exception exception)
            {
                System.Console.WriteLine(exception.Message);
                return(LexStatusCodes.LA_E_INET);
            }
            if (!httpResponse.IsSuccessStatusCode)
            {
                return(DeactivationErrorHandler(productId, httpResponse));
            }
            activationPayload.IsValid = false;
            LexDataStore.Reset(productId);
            return(LexStatusCodes.LA_OK);
        }
 public static int ActivationErrorHandler(string productId, HttpResponseMessage httpResponse)
 {
     if (httpResponse.StatusCode == HttpStatusCode.InternalServerError || httpResponse.StatusCode == HttpStatusCode.ServiceUnavailable)
     {
         return(LexStatusCodes.LA_E_SERVER);
     }
     if (httpResponse.StatusCode == (HttpStatusCode)LexConstants.HttpTooManyRequests)
     {
         return(LexStatusCodes.LA_E_RATE_LIMIT);
     }
     if (httpResponse.StatusCode == HttpStatusCode.NotFound)
     {
         LexDataStore.ResetValue(productId, LexConstants.KEY_ACTIVATION_JWT);
         return(LexStatusCodes.LA_E_ACTIVATION_NOT_FOUND);
     }
     if (httpResponse.StatusCode == HttpStatusCode.BadRequest)
     {
         var errorResponse = JsonConvert.DeserializeObject <HttpErrorResponse>(httpResponse.Content.ReadAsStringAsync().Result);
         if (errorResponse.Code == LexConstants.ActivationErrorCodes.ACTIVATION_LIMIT_REACHED)
         {
             return(LexStatusCodes.LA_E_ACTIVATION_LIMIT);
         }
         // server sync fp validation failed
         if (errorResponse.Code == LexConstants.ActivationErrorCodes.INVALID_ACTIVATION_FINGERPRINT)
         {
             LexDataStore.ResetValue(productId, LexConstants.KEY_ACTIVATION_JWT);
             return(LexStatusCodes.LA_E_MACHINE_FINGERPRINT);
         }
         if (errorResponse.Code == LexConstants.ActivationErrorCodes.VM_ACTIVATION_NOT_ALLOWED)
         {
             LexDataStore.ResetValue(productId, LexConstants.KEY_ACTIVATION_JWT);
             return(LexStatusCodes.LA_E_VM);
         }
         if (errorResponse.Code == LexConstants.ActivationErrorCodes.INVALID_PRODUCT_ID)
         {
             LexDataStore.ResetValue(productId, LexConstants.KEY_ACTIVATION_JWT);
             return(LexStatusCodes.LA_E_PRODUCT_ID);
         }
         if (errorResponse.Code == LexConstants.ActivationErrorCodes.INVALID_LICENSE_KEY)
         {
             LexDataStore.ResetValue(productId, LexConstants.KEY_ACTIVATION_JWT);
             return(LexStatusCodes.LA_E_LICENSE_KEY);
         }
         if (errorResponse.Code == LexConstants.ActivationErrorCodes.AUTHENTICATION_FAILED)
         {
             LexDataStore.ResetValue(productId, LexConstants.KEY_ACTIVATION_JWT);
             return(LexStatusCodes.LA_E_AUTHENTICATION_FAILED);
         }
         if (errorResponse.Code == LexConstants.ActivationErrorCodes.COUNTRY_NOT_ALLOWED)
         {
             LexDataStore.ResetValue(productId, LexConstants.KEY_ACTIVATION_JWT);
             return(LexStatusCodes.LA_E_COUNTRY);
         }
         if (errorResponse.Code == LexConstants.ActivationErrorCodes.IP_ADDRESS_NOT_ALLOWED)
         {
             LexDataStore.ResetValue(productId, LexConstants.KEY_ACTIVATION_JWT);
             return(LexStatusCodes.LA_E_IP);
         }
         if (errorResponse.Code == LexConstants.ActivationErrorCodes.REVOKED_LICENSE)
         {
             LexDataStore.ResetValue(productId, LexConstants.KEY_ACTIVATION_JWT);
             return(LexStatusCodes.LA_E_REVOKED);
         }
         if (errorResponse.Code == LexConstants.ActivationErrorCodes.INVALID_LICENSE_TYPE)
         {
             LexDataStore.ResetValue(productId, LexConstants.KEY_ACTIVATION_JWT);
             return(LexStatusCodes.LA_E_LICENSE_TYPE);
         }
         if (errorResponse.Code == LexConstants.ActivationErrorCodes.METER_ATTRIBUTE_USES_LIMIT_REACHED)
         {
             return(LexStatusCodes.LA_E_METER_ATTRIBUTE_USES_LIMIT_REACHED);
         }
         return(LexStatusCodes.LA_E_CLIENT);
     }
     return(LexStatusCodes.LA_E_INET);
 }