Skip to content

Commit

Permalink
Resolving more lints
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-zywicki committed Nov 10, 2021
1 parent 6638367 commit ddad308
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
4 changes: 2 additions & 2 deletions asynction/security/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class SecurityException(AsynctionException):

class UnregisteredSecurityScheme(SecurityException):
"""
Raised when a security scheme not listed in the securitySchemes section of the spec is used
in a ``security`` or ``x-security`` specification
Raised when a security scheme not listed in the securitySchemes section of the
spec is used in a ``security`` or ``x-security`` specification
"""
pass

Expand Down
71 changes: 51 additions & 20 deletions asynction/security/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ def validate_basic(
return token_info


def validate_authorization_header(request: Request, token_info_func: TokenInfoFunc) -> Union[Mapping, None]:
""" Check that the provided request contains a properly formatted Authorization header and invokes the
token_info_func on the token inside of the header.
def validate_authorization_header(
request: Request,
token_info_func: TokenInfoFunc
) -> Union[Mapping, None]:
""" Check that the provided request contains a properly formatted Authorization
header and invokes the token_info_func on the token inside of the header.
"""
authorization = request.headers.get("Authorization")
if not authorization:
Expand Down Expand Up @@ -95,7 +98,10 @@ def validate_api_key(
return token_info


def validate_scopes(required_scopes: Sequence[str], token_scopes: Sequence[str]) -> bool:
def validate_scopes(
required_scopes: Sequence[str],
token_scopes: Sequence[str]
) -> bool:
""" Validates that all require scopes are present in the token scopes
"""
missing_scopes = set(required_scopes) - set(token_scopes)
Expand All @@ -106,28 +112,38 @@ def validate_scopes(required_scopes: Sequence[str], token_scopes: Sequence[str])


def load_basic_info_func(scheme: SecurityScheme) -> BasicInfoFunc:
basic_info_func = load_handler(scheme.x_basic_info_func)
if not basic_info_func:
if scheme.x_basic_info_func is not None:
basic_info_func = load_handler(scheme.x_basic_info_func)
if not basic_info_func:
raise SecurityException("Missing basic info func")
return basic_info_func
else:
raise SecurityException("Missing basic info func")

return basic_info_func


def load_token_info_func(scheme: SecurityScheme) -> TokenInfoFunc:
token_info_func = load_handler(scheme.x_token_info_func)
if not token_info_func:
if scheme.x_token_info_func is not None:
token_info_func = load_handler(scheme.x_token_info_func)
if not token_info_func:
raise SecurityException("Missing token info function")
return token_info_func
else:
raise SecurityException("Missing token info function")
return token_info_func


def load_api_key_info_func(scheme: SecurityScheme) -> APIKeyInfoFunc:
token_info_func = load_handler(scheme.x_api_key_info_func)
if not token_info_func:
if scheme.x_api_key_info_func is not None:
token_info_func = load_handler(scheme.x_api_key_info_func)
if not token_info_func:
raise SecurityException("Missing API Key info function")
return token_info_func
else:
raise SecurityException("Missing API Key info function")
return token_info_func


def build_http_security_check(requirement: SecurityRequirement) -> Callable[[Request], Mapping]:
def build_http_security_check(
requirement: SecurityRequirement
) -> Callable[[Request], Mapping]:
required_scopes = requirement.scopes
if requirement.scheme.scheme == HTTPSecuritySchemeType.BASIC:
basic_info_func = load_basic_info_func(requirement.scheme)
Expand All @@ -149,7 +165,12 @@ def http_security_check(request: Request):
bearer_format = requirement.scheme.bearer_format

def http_bearer_security_check(request: Request):
token_info = validate_api_key(request, api_key_info_func, required_scopes, bearer_format)
token_info = validate_api_key(
request,
api_key_info_func,
required_scopes,
bearer_format
)
if token_info is None:
return None

Expand Down Expand Up @@ -180,11 +201,15 @@ def get_cookie_value(cookies, name):
return None


def build_http_api_key_security_check(requirement: SecurityRequirement) -> Callable[[Request], Mapping]:
def build_http_api_key_security_check(
requirement: SecurityRequirement
) -> Callable[[Request], Mapping]:
api_key_info_func = load_api_key_info_func(requirement.scheme)
required_scopes = requirement.scopes
api_key_in = requirement.scheme.in_
api_key_name = requirement.scheme.name
if api_key_name is None:
raise SecurityException("invalid api key name specified")
if api_key_in not in ["query", "header", "cookie"]:
raise SecurityException("invalid api key location specified")

Expand Down Expand Up @@ -230,7 +255,9 @@ def _immutable_pop(_dict, key):
return http_api_key_security_check


def build_oauth2_security_check(requirement: SecurityRequirement) -> Callable[[Request], Mapping]:
def build_oauth2_security_check(
requirement: SecurityRequirement
) -> Callable[[Request], Mapping]:
token_info_func = load_token_info_func(requirement.scheme)
required_scopes = requirement.scopes

Expand All @@ -256,7 +283,9 @@ def oauth2_security_check(request: Request):
}


def build_security_handler(security: Sequence[SecurityRequirement]) -> Callable[[Request], Mapping]:
def build_security_handler(
security: Sequence[SecurityRequirement]
) -> Callable[[Request], Mapping]:
# build a list of security validators based on the provided security schemes
security_checks = []

Expand Down Expand Up @@ -288,7 +317,9 @@ def security_handler(request: Request) -> Mapping:
return security_handler


def security_handler_factory(security: Sequence[SecurityRequirement]) -> Callable:
def security_handler_factory(
security: Sequence[SecurityRequirement]
) -> Callable:
"""
Build a security handler decorator based on security object and securitySchemes provided in the API file.
"""
Expand Down

0 comments on commit ddad308

Please sign in to comment.