Esempio n. 1
0
    def activate(token, rsa_pub_key, product_id, key, machine_code, fields_to_return = 0,\
                 metadata = False, floating_time_interval = 0,\
                 max_overdraft = 0, friendly_name = None):
        """
        Calls the Activate method in Web API 3 and returns a tuple containing
        (LicenseKey, Message). If an error occurs, LicenseKey will be None. If
        everything went well, no message will be returned.
        
        More docs: https://app.cryptolens.io/docs/api/v3/Activate
        """

        response = Response("", "", 0, "")

        try:
            response = Response.from_string(HelperMethods.send_request("key/activate", {"token":token,\
                                                  "ProductId":product_id,\
                                                  "key":key,\
                                                  "MachineCode":machine_code,\
                                                  "FieldsToReturn":fields_to_return,\
                                                  "metadata":metadata,\
                                                  "FloatingTimeInterval": floating_time_interval,\
                                                  "MaxOverdraft": max_overdraft,\
                                                  "FriendlyName" : friendly_name,\
                                                  "ModelVersion": 3 ,\
                                                  "Sign":"True",\
                                                  "SignMethod":1}))
        except HTTPError as e:
            response = Response.from_string(e.read())
        except URLError as e:
            return (None,
                    "Could not contact the server. Error message: " + str(e))
        except Exception:
            return (None, "Could not contact the server.")

        pubkey = RSAPublicKey.from_string(rsa_pub_key)

        if response.result == 1:
            return (None, response.message)
        else:
            try:
                if HelperMethods.verify_signature(response, pubkey):
                    return (LicenseKey.from_response(response),
                            response.message)
                else:
                    return (None, "The signature check failed.")
            except Exception:
                return (None, "The signature check failed.")
Esempio n. 2
0
    def load_from_string(rsa_pub_key,
                         string,
                         signature_expiration_interval=-1):
        """
        Loads a license from a string generated by save_as_string.
        Note: if an error occurs, None will be returned. An error can occur
        if the license string has been tampered with or if the public key is
        incorrectly formatted.
        
        :param signature_expiration_interval: If the license key was signed,
        this method will check so that no more than "signatureExpirationInterval" 
        days have passed since the last activation.
        """

        response = Response("", "", "", "")

        try:
            response = Response.from_string(string)
        except Exception as ex:
            return None

        if response.result == "1":
            return None
        else:
            try:
                pubKey = RSAPublicKey.from_string(rsa_pub_key)
                if HelperMethods.verify_signature(response, pubKey):

                    licenseKey = LicenseKey.from_response(response)

                    if signature_expiration_interval > 0 and \
                    (licenseKey.sign_date + datetime.timedelta(days=1*signature_expiration_interval) < datetime.datetime.utcnow()):
                        return None

                    return licenseKey
                else:
                    return None
            except Exception:
                return None
Esempio n. 3
0
 def get_key(token, rsa_pub_key, product_id, key, fields_to_return = 0,\
              metadata = False, floating_time_interval = 0):
     
     """
     Calls the GetKey method in Web API 3 and returns a tuple containing
     (LicenseKey, Message). If an error occurs, LicenseKey will be None. If
     everything went well, no message will be returned.
     
     More docs: https://app.cryptolens.io/docs/api/v3/GetKey
     """
     
     response = Response("","",0,"")
     
     try:
         response = Response.from_string(HelperMethods.send_request("key/getkey", {"token":token,\
                                               "ProductId":product_id,\
                                               "key":key,\
                                               "FieldsToReturn":fields_to_return,\
                                               "metadata":metadata,\
                                               "FloatingTimeInterval": floating_time_interval,\
                                               "Sign":"True",\
                                               "SignMethod":1}))
     except Exception:
         return (None, "Could not contact the server.")
     
     pubkey = RSAPublicKey.from_string(rsa_pub_key)
 
     if response.result == 1:
         return (None, response.message)
     else:
         try:
             if HelperMethods.verify_signature(response, pubkey):
                 return (LicenseKey.from_response(response), response.message)
             else:
                 return (None, "The signature check failed.")
         except Exception:
             return (None, "The signature check failed.")