Skip to content

Commit 470a5d5

Browse files
authored
Create trial key (#10)
* Start on CreateTrialKey * Update input parameters * Update docs * Update setup.py
1 parent 2e6720b commit 470a5d5

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
lines changed

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,52 @@ res = Key.activate(token="WyIyNjA1IiwiTjhZQUpIYXJPaVdBV0ozQUlKVC9tamdDbFZDRzhZRH
108108
max_overdraft=1)
109109

110110
if res[0] == None or not Helpers.IsOnRightMachine(res[0], is_floating_license=True, allow_overdraft=True):
111-
print("An error occured: {0}".format(res[1]))
111+
print("An error occurred: {0}".format(res[1]))
112112
else:
113113
print("Success")
114114

115115
license_key = res[0]
116116
print("Feature 1: " + str(license_key.f1))
117117
print("License expires: " + str(license_key.expires))
118118
```
119+
120+
### Create Trial Key (verified trial)
121+
122+
#### Idea
123+
124+
A [trial key](https://help.cryptolens.io/examples/verified-trials) allows your users to evaluate some or all parts of your software for a limited period of time. The goal of trial keys is to set it up in such a way that you don’t need to manually create them, while still keeping everything secure.
125+
126+
In Cryptolens, all trial keys are bound to the device that requested them, which helps to prevent users from using the trial after reinstalling their device.
127+
128+
You can define which features should count as trial by [editing feature definitions](https://help.cryptolens.io/web-interface/feature-definitions) on the product page.
129+
130+
#### Implementation
131+
132+
The code below shows how to create trial key. If the trial key is successful, `trial_key[0]` will contain the license key string. We then need to call `Key.Activate` (as shown in the earlier examples) with the obtained license key to verify the license.
133+
134+
```python
135+
from licensing.models import *
136+
from licensing.methods import Key, Helpers
137+
138+
trial_key = res = Key.create_trial_key("WyIzODQ0IiwiempTRWs4SnBKTTArYUh3WkwyZ0VwQkVyeTlUVkRWK2ZTOS8wcTBmaCJd", 3941, Helpers.GetMachineCode())
139+
140+
if trial_key[0] == None:
141+
print("An error occurred: {0}".format(res[1]))
142+
143+
144+
pubKey = "<RSAKeyValue><Modulus>sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTnu+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"
145+
146+
res = Key.activate(token="WyIyNTU1IiwiRjdZZTB4RmtuTVcrQlNqcSszbmFMMHB3aWFJTlBsWW1Mbm9raVFyRyJd",\
147+
rsa_pub_key=pubKey,\
148+
product_id=3941, key=trial_key[0], \
149+
machine_code=Helpers.GetMachineCode())
150+
151+
if res[0] == None or not Helpers.IsOnRightMachine(res[0]):
152+
print("An error occurred: {0}".format(res[1]))
153+
else:
154+
print("Success")
155+
156+
license_key = res[0]
157+
print("Feature 1: " + str(license_key.f1))
158+
print("License expires: " + str(license_key.expires))
159+
```

licensing/methods.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import sys
1111
from licensing.internal import HelperMethods
1212
from licensing.models import *
13+
import json
1314

1415
class Key:
1516

@@ -58,6 +59,35 @@ def activate(token, rsa_pub_key, product_id, key, machine_code, fields_to_return
5859
except Exception:
5960
return (None, "The signature check failed.")
6061

62+
def create_trial_key(token, product_id, machine_code):
63+
"""
64+
Calls the CreateTrialKey method in Web API 3 and returns a tuple containing
65+
(LicenseKeyString, Message). If an error occurs, LicenseKeyString will be None. If
66+
everything went well, no message will be returned.
67+
68+
More docs: https://app.cryptolens.io/docs/api/v3/CreateTrialKey
69+
"""
70+
71+
response = ""
72+
73+
try:
74+
response = HelperMethods.send_request("key/createtrialkey", {"token":token,\
75+
"ProductId":product_id,\
76+
"MachineCode":machine_code})
77+
except Exception:
78+
return (None, "Could not contact the server.")
79+
80+
jobj = json.loads(response)
81+
82+
if jobj == None or jobj["result"] == "1":
83+
if jobj != None:
84+
return (None, jobj["message"])
85+
else:
86+
return (None, "Could not contact the server.")
87+
88+
return (jobj["key"], "")
89+
90+
6191

6292
class Helpers:
6393

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
setup(
33
name = 'licensing', # How you named your package folder (MyLib)
44
packages = ['licensing'], # Chose the same as "name"
5-
version = '0.7', # Start with a small number and increase it with every change you make
5+
version = '0.8', # Start with a small number and increase it with every change you make
66
license='MIT', # Chose a license from here: https://help.github.com/articles/licensing-a-repository
77
description = 'Client library for Cryptolens licensing Web API.', # Give a short description about your library
88
author = 'Cryptolens AB', # Type in your name
99
author_email = '[email protected]', # Type in your E-Mail
1010
url = 'https://cryptolens.io', # Provide either the link to your github or to your website
11-
download_url = 'https://github.com/Cryptolens/cryptolens-python/archive/v_07.tar.gz', # I explain this later on
11+
download_url = 'https://github.com/Cryptolens/cryptolens-python/archive/v_08.tar.gz', # I explain this later on
1212
keywords = ['software licensing', 'licensing library', 'cryptolens'], # Keywords that define your package best
1313
install_requires=[ # I get to this in a second
1414
'pycryptodome'

test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@
4040
print("Feature 1: " + str(license_key.f1))
4141
print("License expires: " + str(license_key.expires))
4242

43-
print(Helpers.GetMachineCode())
43+
print(Helpers.GetMachineCode())
44+
45+
res = Key.create_trial_key("WyIzODQ0IiwiempTRWs4SnBKTTArYUh3WkwyZ0VwQkVyeTlUVkRWK2ZTOS8wcTBmaCJd", 3941, Helpers.GetMachineCode())

0 commit comments

Comments
 (0)