Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/blueapi/service/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ def _file_path(self) -> str:

def save_cache(self, cache: Cache) -> None:
self.delete_cache()
self._create_parent_folder_if_necessary()
with open(self._file_path, "xb") as token_file:
token_file.write(base64.b64encode(cache.model_dump_json().encode("utf-8")))
os.chmod(self._file_path, 0o600)

def load_cache(self) -> Cache:
self._create_parent_folder_if_necessary()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to create the directory on load? If it doesn't exist, I imagine this will behave the same as if the cache file doesn't exist which is (hopefully) already handled.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look in the stack trace on the ticket there were 2 errors thrown, the first when trying to open the file in this method, and the second when calling can_access_cache as part of the error handling for the first error.

If it is correct that this method should error and fall back on the error handling if the folder doesn't exist, then this can be removed, otherwise it does need to stay

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think load_cache will still throw the exception when the file doesn't exist so creating the directory doesn't really achieve much. The second exception where it can't start the device flow does need the directory to be present.

It doesn't really matter though so feel free to leave it if it would otherwise hold up the bug being fixed.

with open(self._file_path, "rb") as cache_file:
return TypeAdapter(Cache).validate_json(
base64.b64decode(cache_file.read()).decode("utf-8")
Expand All @@ -71,6 +73,7 @@ def _default_token_cache_path() -> Path:
def can_access_cache(self) -> bool:
assert self._token_path
try:
self._create_parent_folder_if_necessary()
self._token_path.write_text("")
except IsADirectoryError:
print("Invalid path: a directory path was provided instead of a file path")
Expand All @@ -80,6 +83,9 @@ def can_access_cache(self) -> bool:
return False
return True

def _create_parent_folder_if_necessary(self):
self._token_path.parent.mkdir(parents=True, exist_ok=True)


class SessionManager:
def __init__(self, server_config: OIDCConfig, cache_manager: CacheManager) -> None:
Expand Down
Loading