Skip to content

Commit 021a23c

Browse files
Provide license date fallback
not_valid_before_utc and not_valid_after_utc are available as of cryptography version 42. Restore old behavior for older versions.
1 parent a285497 commit 021a23c

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
apiSpec = None # API specification
99
apiVersion = None # API specification version. Extracted from the OpenAPI document.
10-
backendVersion = "1.17.6" # Backend version number
10+
backendVersion = "1.17.7" # Backend version number
1111

1212

1313
def _loadOpenApiSpec():

tools/license.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from datetime import datetime, timezone, MAXYEAR
66

7+
import cryptography
78
import logging
89

910
from cryptography import x509
@@ -14,15 +15,25 @@
1415

1516
logger = logging.getLogger("license")
1617

18+
cryptographyVersion = [int(part) for part in cryptography.__version__.split(".")]
19+
TZ = None if cryptographyVersion[0] < 42 else timezone.utc
20+
21+
22+
def cert_not_before(cert):
23+
return cert.not_valid_before if cryptographyVersion[0] < 42 else cert.not_valid_before_utc
24+
25+
26+
def cert_not_after(cert):
27+
return cert.not_valid_after if cryptographyVersion[0] < 42 else cert.not_valid_after_utc
28+
1729

1830
class CertificateError(Exception):
1931
pass
2032

21-
2233
class GrommunioLicense(GenericObject):
2334
@staticmethod
2435
def validate(cert):
25-
if cert is not None and not cert.not_valid_before_utc <= datetime.now(tz=timezone.utc) <= cert.not_valid_after_utc:
36+
if cert is not None and not cert_not_before(cert) <= datetime.now(tz=TZ) <= cert_not_after(cert):
2637
raise CertificateError("Certificate expired")
2738

2839
@property
@@ -38,8 +49,8 @@ def _defaultLicense():
3849
file=None,
3950
users=5,
4051
product="Community",
41-
notBefore=datetime(1000, 1, 1),
42-
notAfter=datetime(MAXYEAR, 12, 31, 23, 59, 59))
52+
notBefore=datetime(1000, 1, 1, tzinfo=TZ),
53+
notAfter=datetime(MAXYEAR, 12, 31, 23, 59, 59, tzinfo=TZ))
4354

4455

4556
def _processCertificate(data):
@@ -50,8 +61,8 @@ def _processCertificate(data):
5061
lic = GrommunioLicense(cert=cert, file=data)
5162
lic.users = int(exts.get("1.3.6.1.4.1.56504.1.1"))
5263
lic.product = exts.get("1.3.6.1.4.1.56504.1.2").decode("utf-8") if "1.3.6.1.4.1.56504.1.2" in exts else None
53-
lic.notBefore = cert.not_valid_before_utc
54-
lic.notAfter = cert.not_valid_after_utc
64+
lic.notBefore = cert_not_before(cert)
65+
lic.notAfter = cert_not_after(cert)
5566
return True, lic
5667
except ValueError:
5768
return False, "Bad certificate"

0 commit comments

Comments
 (0)