Skip to content

Commit

Permalink
Fix mypy errors
Browse files Browse the repository at this point in the history
Fix mypy errors
  • Loading branch information
francbartoli committed Jan 28, 2024
1 parent 3ab1779 commit 24c512f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
6 changes: 4 additions & 2 deletions app/auth/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def __init__(
) -> None:
"""Set properties initialization for injectables."""
self.key = key
self.skip_endpoints = [re.compile(skip) for skip in skip_endpoints]
self.skip_endpoints = [
re.compile(skip) for skip in skip_endpoints
] # type:ignore

@abstractmethod
async def extract(self, request: Request) -> List:
Expand All @@ -30,7 +32,7 @@ class Oauth2Provider:

def __init__(
self,
authentication: [AuthInterface, List[AuthInterface]],
authentication: [AuthInterface, List[AuthInterface]], # type:ignore
injectables: Optional[List[Injectable]] = None,
accepted_methods: Optional[List[str]] = [ # noqa B006
"id_token",
Expand Down
23 changes: 10 additions & 13 deletions app/middleware/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import List
from typing import Optional

from app.auth.exceptions import Oauth2Exception
from app.auth.exceptions import Oauth2Error
from app.auth.oauth2 import Oauth2Provider
from app.config.app import configuration as cfg
from app.config.logging import create_logger
Expand All @@ -16,22 +16,16 @@
from starlette.types import Scope
from starlette.types import Send

try:
Pattern = re.Pattern
except AttributeError:
# Python3.6 does not contain re.Pattern
Pattern = None


logger = create_logger("app.middleware.oauth2")


def should_skip_endpoint(endpoint: str, skip_endpoints: List[Pattern]) -> bool:
def should_skip_endpoint(endpoint: str, skip_endpoints: List[re.Pattern]) -> bool:
"""Evaluate whether a given endpoint should be skipped.
Args:
endpoint (str): Endpoint path
skip_endpoints (List[Pattern]): Pattern to skip
skip_endpoints (List[re.Pattern]): Pattern to skip
Returns:
bool: Result of the evaluation
Expand Down Expand Up @@ -75,12 +69,15 @@ def __init__(
"""Initialize OAuth2 authentication middleware."""
self.config = config
self.app = app
if cfg.FASTGEOAPI_CONTEXT and cfg.FASTGEOAPI_CONTEXT not in skip_endpoints:
if cfg.FASTGEOAPI_CONTEXT not in skip_endpoints: # type:ignore
self.skip_endpoints = [
re.compile(f"{cfg.FASTGEOAPI_CONTEXT}{skip}") for skip in skip_endpoints
re.compile(f"{cfg.FASTGEOAPI_CONTEXT}{skip}")
for skip in skip_endpoints # type:ignore
]
else:
self.skip_endpoints = [re.compile(skip) for skip in skip_endpoints]
self.skip_endpoints = [
re.compile(skip) for skip in skip_endpoints
] # type:ignore
logger.debug(f"Compiled skippable endpoints: {self.skip_endpoints}")

async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
Expand Down Expand Up @@ -116,7 +113,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if isinstance(user_info_or_auth_redirect, dict):
successful = True
break
except Oauth2Exception as e:
except Oauth2Error as e:
logger.error(f"Authentication Exception raised on login \n{e}")

# Some authentication flows require a prior redirect to id provider
Expand Down
2 changes: 1 addition & 1 deletion app/middleware/pygeoapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async def send_with_security(self, message: Message) -> None: # noqa: C901
item in ["http", "apiKey", "oauth2", "openIdConnect"]
for item in security_scheme_types
):
security_schemes = {"securitySchemes": {}}
security_schemes = {"securitySchemes": {}} # type: dict[str, dict]
dumped_schemes = {}
for scheme in self.security_schemes:
dumped_schemes.update(
Expand Down

0 comments on commit 24c512f

Please sign in to comment.