Skip to content

Commit

Permalink
Utilize new service exceptions in AuthenticatedUser
Browse files Browse the repository at this point in the history
Also removed the currently unused access tokens for pdm and ssdl
  • Loading branch information
sigurdp committed Dec 9, 2023
1 parent c1acc57 commit 07d1d85
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 36 deletions.
2 changes: 0 additions & 2 deletions backend/src/backend/auth/auth_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@ def get_authenticated_user(
"graph_access_token": graph_token,
"sumo_access_token": sumo_token,
"smda_access_token": smda_token,
"pdm_access_token": None,
"ssdl_access_token": None,
},
)

Expand Down
40 changes: 6 additions & 34 deletions backend/src/services/utils/authenticated_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

from typing import Any, Optional, TypedDict

from src.services.service_exceptions import Service, AuthorizationError


class AccessTokens(TypedDict):
graph_access_token: Optional[str]
sumo_access_token: Optional[str]
smda_access_token: Optional[str]
pdm_access_token: Optional[str]
ssdl_access_token: Optional[str]


class AuthenticatedUser:
Expand All @@ -23,8 +23,6 @@ def __init__(
self._graph_access_token = access_tokens.get("graph_access_token")
self._sumo_access_token = access_tokens.get("sumo_access_token")
self._smda_access_token = access_tokens.get("smda_access_token")
self._pdm_access_token = access_tokens.get("pdm_access_token")
self._ssdl_access_token = access_tokens.get("ssdl_access_token")

def __hash__(self) -> int:
return hash(self._user_id)
Expand All @@ -39,20 +37,20 @@ def get_graph_access_token(self) -> str:
if isinstance(self._graph_access_token, str) and self._graph_access_token:
return self._graph_access_token

raise ValueError("User has no graph access token")
raise AuthorizationError("User has no graph access token", Service.GENERAL)

def has_graph_access_token(self) -> bool:
try:
self.get_graph_access_token()
return True
except ValueError:
except AuthorizationError:
return False

def get_sumo_access_token(self) -> str:
if isinstance(self._sumo_access_token, str) and len(self._sumo_access_token) > 0:
return self._sumo_access_token

raise ValueError("User has no sumo access token")
raise AuthorizationError("User has no sumo access token", Service.GENERAL)

def has_sumo_access_token(self) -> bool:
try:
Expand All @@ -65,37 +63,11 @@ def get_smda_access_token(self) -> str:
if isinstance(self._smda_access_token, str) and len(self._smda_access_token) > 0:
return self._smda_access_token

raise ValueError("User has no smda access token")
raise AuthorizationError("User has no smda access token", Service.GENERAL)

def has_smda_access_token(self) -> bool:
try:
self.get_smda_access_token()
return True
except:
return False

def get_pdm_access_token(self) -> str:
if isinstance(self._pdm_access_token, str) and len(self._pdm_access_token) > 0:
return self._pdm_access_token

raise ValueError("User has no pdm access token")

def has_pdm_access_token(self) -> bool:
try:
self.get_pdm_access_token()
return True
except:
return False

def get_ssdl_access_token(self) -> str:
if isinstance(self._ssdl_access_token, str) and len(self._ssdl_access_token) > 0:
return self._ssdl_access_token

raise ValueError("User has no ssdl access token")

def has_ssdl_access_token(self) -> bool:
try:
self.get_ssdl_access_token()
return True
except:
return False

0 comments on commit 07d1d85

Please sign in to comment.