-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.py
86 lines (74 loc) · 2.79 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import machineid
import requests
import json
import sys
import os
def activate_license(license_key):
machine_fingerprint = machineid.hashed_id('example-app')
validation = requests.post(
"https://api.keygen.sh/v1/accounts/{}/licenses/actions/validate-key".format(os.environ['KEYGEN_ACCOUNT_ID']),
headers={
"Content-Type": "application/vnd.api+json",
"Accept": "application/vnd.api+json"
},
data=json.dumps({
"meta": {
"scope": { "fingerprint": machine_fingerprint },
"key": license_key
}
})
).json()
if "errors" in validation:
errs = validation["errors"]
return False, "license validation failed: {}".format(
','.join(map(lambda e: "{} - {}".format(e["title"], e["detail"]).lower(), errs))
)
# If the license is valid for the current machine, that means it has
# already been activated. We can return early.
if validation["meta"]["valid"]:
return True, "license has already been activated on this machine"
# Otherwise, we need to determine why the current license is not valid,
# because in our case it may be invalid because another machine has
# already been activated, or it may be invalid because it doesn't
# have any activated machines associated with it yet and in that case
# we'll need to activate one.
validation_code = validation["meta"]["code"]
activation_is_required = validation_code == 'FINGERPRINT_SCOPE_MISMATCH' or \
validation_code == 'NO_MACHINES' or \
validation_code == 'NO_MACHINE'
if not activation_is_required:
return False, "license {}".format(validation["meta"]["detail"])
# If we've gotten this far, then our license has not been activated yet,
# so we should go ahead and activate the current machine.
activation = requests.post(
"https://api.keygen.sh/v1/accounts/{}/machines".format(os.environ['KEYGEN_ACCOUNT_ID']),
headers={
"Authorization": "License {}".format(license_key),
"Content-Type": "application/vnd.api+json",
"Accept": "application/vnd.api+json"
},
data=json.dumps({
"data": {
"type": "machines",
"attributes": {
"fingerprint": machine_fingerprint
},
"relationships": {
"license": {
"data": { "type": "licenses", "id": validation["data"]["id"] }
}
}
}
})
).json()
# If we get back an error, our activation failed.
if "errors" in activation:
errs = activation["errors"]
return False, "license activation failed: {}".format(
','.join(map(lambda e: "{} - {}".format(e["title"], e["detail"]).lower(), errs))
)
return True, "license activated"
# Run from the command line:
# python main.py some_license_key
status, msg = activate_license(sys.argv[1])
print(status, msg)