88import json
99import base64
1010import datetime
11+ import copy
12+
13+ from licensing .internal import Helpers
1114
1215class LicenseKey :
1316
@@ -40,7 +43,7 @@ def __init__(self, ProductId, ID, Key, Created, Expires, Period, F1, F2,\
4043 self .allowed_machines = AllowedMachines
4144 self .data_objects = DataObjects
4245 self .sign_date = SignDate
43- self .raw_respone = RawResponse
46+ self .raw_response = RawResponse
4447
4548 def from_response (response ):
4649
@@ -56,6 +59,44 @@ def from_response(response):
5659 obj ["Customer" ], obj ["ActivatedMachines" ], obj ["TrialActivation" ], \
5760 obj ["MaxNoOfMachines" ], obj ["AllowedMachines" ], obj ["DataObjects" ], \
5861 datetime .datetime .fromtimestamp (obj ["SignDate" ]), response )
62+
63+ def save_as_string (self ):
64+ """
65+ Save the license as a string that can later be read by load_from_string.
66+ """
67+ res = copy .copy (self .raw_response .__dict__ )
68+ res ["licenseKey" ] = res ["license_key" ]
69+ res .pop ("license_key" , None )
70+ return json .dumps (res )
71+
72+ def load_from_string (rsa_pub_key , string ):
73+ """
74+ Loads a license from a string generated by save_as_string.
75+ Note: if an error occurs, None will be returned. An error can occur
76+ if the license string has been tampered with or if the public key is
77+ incorrectly formatted.
78+ """
79+
80+ response = Response ("" ,"" ,"" ,"" )
81+
82+ try :
83+ response = Response .from_string (string )
84+ except Exception as ex :
85+ return None
86+
87+ if response .result == "1" :
88+ return None
89+ else :
90+ try :
91+ pubKey = RSAPublicKey .from_string (rsa_pub_key )
92+ if Helpers .verify_signature (response , pubKey ):
93+ return LicenseKey .from_response (response )
94+ else :
95+ return None
96+ except Exception :
97+ return None
98+
99+
59100
60101class Response :
61102
@@ -66,7 +107,7 @@ def __init__(self, license_key, signature, result, message):
66107 self .message = message
67108
68109 def from_string (responseString ):
69- obj = json .loads (responseString )
110+ obj = json .loads (responseString )
70111 return Response (obj ["licenseKey" ], obj ["signature" ], obj ["result" ],obj ["message" ])
71112
72113class RSAPublicKey :
0 commit comments