Skip to content

Commit

Permalink
minor cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
Leobouloc committed Oct 24, 2023
1 parent 34c8413 commit a97415b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/ralph/api/auth/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pydantic import BaseModel, root_validator
from starlette.authentication import AuthenticationError

from ralph.api.auth.user import AuthenticatedUser, UserScopes
from ralph.api.auth.user import AuthenticatedUser
from ralph.conf import settings

# Unused password used to avoid timing attacks, by comparing passwords supplied
Expand Down Expand Up @@ -188,8 +188,7 @@ def get_basic_auth_user(
# Restrict access by scopes
if settings.LRS_RESTRICT_BY_SCOPES:
for requested_scope in security_scopes.scopes:
is_auth = user.scopes.is_authorized(requested_scope)
if not is_auth:
if not user.scopes.is_authorized(requested_scope):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=f'Access not authorized to scope: "{requested_scope}".',
Expand Down
3 changes: 1 addition & 2 deletions src/ralph/api/auth/oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ def get_oidc_user(
# Restrict access by scopes
if settings.LRS_RESTRICT_BY_SCOPES:
for requested_scope in security_scopes.scopes:
is_auth = user.scopes.is_authorized(requested_scope)
if not is_auth:
if not user.scopes.is_authorized(requested_scope):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=f'Access not authorized to scope: "{requested_scope}".',
Expand Down
6 changes: 3 additions & 3 deletions src/ralph/api/auth/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from functools import lru_cache
from typing import Dict, FrozenSet, Literal

from ralph.conf import settings

from pydantic import BaseModel

Scope = Literal[
Expand Down Expand Up @@ -58,11 +56,13 @@ def is_authorized(self, requested_scope: Scope):

@classmethod
def __get_validators__(cls): # noqa: D105
def validate(value: FrozenSet[Scope]) -> UserScopes:
def validate(value: FrozenSet[Scope]):
"""Transform value to an instance of UserScopes."""
return cls(value)

yield validate


class AuthenticatedUser(BaseModel):
"""Pydantic model for user authentication.
Expand Down

0 comments on commit a97415b

Please sign in to comment.