Skip to content

Commit

Permalink
Ensure username fetching logic is not token dependent (#2923)
Browse files Browse the repository at this point in the history
* Ensure username fetching logic is not token dependent
* Better handling of username from client, less errors when guessing username from an unvalidated token
  • Loading branch information
dgaloop committed Aug 3, 2024
1 parent 3794f91 commit 54d2575
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
35 changes: 21 additions & 14 deletions deeplake/client/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import deeplake
import requests # type: ignore
from typing import Any, Optional, Dict
from typing import Any, Optional, Dict, Tuple
from deeplake.util.exceptions import (
AgreementNotAcceptedError,
AuthorizationException,
Expand Down Expand Up @@ -59,18 +59,25 @@ def __init__(self, token: Optional[str] = None):

# remove public token, otherwise env var will be ignored
# we can remove this after a while
orgs = self.get_user_organizations()
if orgs == ["public"]:
self._username, self._organizations = self._get_username_and_organizations()
if self._username == "public":
self.token = token or self.get_token()
else:
username = self.get_user_profile()["name"]
if get_reporting_config().get("username") != username:
save_reporting_config(True, username=username)
set_username(username)
if get_reporting_config().get("username") != self._username:
save_reporting_config(True, username=self._username)
set_username(self._username)

def get_token(self):
return self.auth_context.get_token()

@property
def username(self) -> str:
return self._username

@property
def organizations(self) -> list[str]:
return self._organizations

def request(
self,
method: str,
Expand Down Expand Up @@ -335,25 +342,25 @@ def rename_dataset_entry(self, username, old_name, new_name):
"PUT", suffix, endpoint=self.endpoint(), json={"basename": new_name}
)

def get_user_organizations(self):
"""Get list of user organizations from the backend. If user is not authenticated, returns ['public'].
def _get_username_and_organizations(self) -> Tuple[str, list[str]]:
"""Get the username plus a list of user organizations from the backend. If user is not authenticated, returns ('public', ['public']).
Returns:
list: user/organization names
(str, list[str]): user + organization namess
"""

if self.auth_context.is_public_user():
return ["public"]
return "public", ["public"]

response = self.request(
"GET", GET_USER_PROFILE, endpoint=self.endpoint()
).json()
return response["organizations"]
return response["_id"], response["organizations"]

def get_workspace_datasets(
self, workspace: str, suffix_public: str, suffix_user: str
):
organizations = self.get_user_organizations()
if workspace in organizations:
if workspace in self.organizations:
response = self.request(
"GET",
suffix_user,
Expand Down
2 changes: 1 addition & 1 deletion deeplake/core/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def username(self) -> str:

try:
return jwt.decode(self.token, options={"verify_signature": False})["id"]
except DecodeError:
except:
return "public"

@property
Expand Down
4 changes: 4 additions & 0 deletions deeplake/core/dataset/deeplake_cloud_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def client(self):
self.__dict__["_client"] = DeepLakeBackendClient(token=self._token)
return self._client

@property
def username(self):
return self.client.username

@property
def is_actually_cloud(self) -> bool:
"""Datasets that are connected to Deep Lake cloud can still technically be stored anywhere.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ def exec_option(self) -> str:

@property
def username(self) -> str:
if self.dataset:
return self.dataset.username

username = "public"
if self.token is not None:
try:
Expand Down

0 comments on commit 54d2575

Please sign in to comment.