Skip to content

Commit

Permalink
optionally check machine id when reading license (#13)
Browse files Browse the repository at this point in the history
* optionally check machine id when reading license

* Review for checking machine ID (#15)

* Refactor to remove duplicate code

* Fix tests

* Style

* More style

* More style

---------

Co-authored-by: Eric G. Kratz <[email protected]>
  • Loading branch information
valentinsulzer and kratman authored Oct 30, 2024
1 parent f66451f commit 5189c89
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
23 changes: 19 additions & 4 deletions ionwizard/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@
from pathlib import Path
import requests
from ionwizard.env_variables import KEYGEN_ACCOUNT_ID
import machineid


def read_config_libraries():
def read_config_file():
config_file = Path(platformdirs.user_config_dir("ionworks")) / "config.yml"
if config_file.exists():
with open(config_file, "r") as f:
config = yaml.safe_load(f)
libraries = config.get("ionworks", {}).get("libraries", {})
return libraries
return config.get("ionworks", {})
sys.tracebacklimit = 0
raise FileNotFoundError("\nError: No ionworks configuration file was found.\n")


def read_config_libraries():
return read_config_file().get("libraries", {})


def get_library_key(library_name):
"""
Get the API key from the library configuration.
Expand All @@ -28,7 +32,7 @@ def get_library_key(library_name):
return None


def license_check(library_name, custom_library_key=None):
def license_check(library_name, custom_library_key=None, check_machine_id=True):
"""
Check if the license for the library is valid.
Expand All @@ -40,6 +44,9 @@ def license_check(library_name, custom_library_key=None):
The library key to use for the license check. If not provided, the library key
will be retrieved from the library configuration.
"""
if check_machine_id:
machine_id_check()

if custom_library_key is not None:
library_key = custom_library_key
else:
Expand Down Expand Up @@ -75,3 +82,11 @@ def license_check(library_name, custom_library_key=None):
return {"success": False, "message": "Error: Invalid license key"}
else:
return {"success": False, "message": "Error: Failed to validate license key"}


def machine_id_check():
config = read_config_file()
machine_id = machineid.id()
if config.get("machine_id") != machine_id:
return {"success": False, "message": "Error: Machine ID mismatch"}
return {"success": True, "message": "Machine ID is valid"}
16 changes: 15 additions & 1 deletion tests/test_validate.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ionwizard.validate import get_library_key, license_check
from ionwizard.validate import get_library_key, license_check, machine_id_check
from tempfile import TemporaryDirectory
import machineid
import yaml
import os

Expand Down Expand Up @@ -29,6 +30,7 @@ def test_get_library_key(mocker):


def test_validate(mocker):
mocker.patch("ionwizard.validate.machine_id_check", return_value={})
mocker.patch("ionwizard.validate.get_library_key", return_value="VALID-KEY-123")
mocker.patch(
"requests.post",
Expand Down Expand Up @@ -70,3 +72,15 @@ def test_validate(mocker):
"success": False,
"message": "Error: Failed to validate license key",
}


def test_machine_id(mocker):
mocker.patch(
"ionwizard.validate.read_config_file",
return_value={"machine_id": machineid.id()},
)
assert machine_id_check()["success"]
mocker.patch(
"ionwizard.validate.read_config_file", return_value={"machine_id": "123"}
)
assert not machine_id_check()["success"]

0 comments on commit 5189c89

Please sign in to comment.