Skip to content

Commit

Permalink
Use sub in auth service to authenticate the user. (#491)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Watts <[email protected]>
  • Loading branch information
rwatts3 and Ryan Watts authored May 23, 2024
1 parent 42b1703 commit ab142f2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 4 additions & 0 deletions services/enterprise/modules/user/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def get_user(self, query: dict) -> User:
user = MongoDB.find_one(USER_COL, query)
return User(id=str(user["_id"]), **user) if user else None

def get_user_by_sub(self, sub: str) -> User:
user = MongoDB.find_one(USER_COL, {"sub": sub})
return User(id=str(user["_id"]), **user) if user else None

def get_user_by_email(self, email: str) -> User:
user = MongoDB.find_one(USER_COL, {"email": email})
return User(id=str(user["_id"]), **user) if user else None
Expand Down
5 changes: 5 additions & 0 deletions services/enterprise/modules/user/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def get_users(self, org_id: str) -> list[UserResponse]:
def get_user(self, user_id: str, org_id: str) -> UserResponse:
return self.get_user_in_org(user_id, org_id)

def get_user_by_sub(self, sub: str) -> User:
"""Helper function to get user by Auth0sub."""
user = self.repo.get_user_by_sub(sub)
return user if user else None

def get_user_by_email(self, email: str) -> User:
"""Helper function to get user by email."""
user = self.repo.get_user_by_email(email)
Expand Down
6 changes: 3 additions & 3 deletions services/enterprise/utils/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ def _decode_payload(self):

class Authorize:
def user(self, payload: dict) -> User:
email = payload[auth_settings.auth0_issuer + "email"]
user = user_service.get_user_by_email(email)
sub = payload['sub']
user = user_service.get_user_by_sub(sub)
if not user:
raise UnauthorizedUserError(email=email)
raise UnauthorizedUserError(email=sub)
return user

def user_in_organization(self, user_id: str, org_id: str):
Expand Down

0 comments on commit ab142f2

Please sign in to comment.