Skip to content

Commit 222707f

Browse files
authored
feat: BI-6808 add always_deny auth method (#1470)
1 parent f85d831 commit 222707f

File tree

11 files changed

+131
-35
lines changed

11 files changed

+131
-35
lines changed

lib/dl_app_api_base/dl_app_api_base/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
HttpServerSettings,
99
)
1010
from .auth import (
11-
NoAuthChecker,
12-
NoAuthResult,
11+
AlwaysAllowAuthChecker,
12+
AlwaysAllowAuthResult,
13+
AlwaysDenyAuthChecker,
1314
OAuthChecker,
1415
OAuthCheckerSettings,
1516
OAuthResult,
@@ -85,8 +86,9 @@
8586
"RequestAuthCheckerProtocol",
8687
"OAuthChecker",
8788
"OAuthResult",
88-
"NoAuthChecker",
89-
"NoAuthResult",
89+
"AlwaysAllowAuthChecker",
90+
"AlwaysAllowAuthResult",
91+
"AlwaysDenyAuthChecker",
9092
"RouteMatcher",
9193
"OAuthCheckerSettings",
9294
]

lib/dl_app_api_base/dl_app_api_base/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ async def _get_request_auth_checkers(
117117
self,
118118
) -> list[auth.RequestAuthCheckerProtocol]:
119119
return [
120-
auth.NoAuthChecker(
120+
auth.AlwaysAllowAuthChecker(
121121
route_matchers=[
122122
auth.RouteMatcher(
123123
path_regex=re.compile(r"^/api/v1/health/.*$"),

lib/dl_app_api_base/dl_app_api_base/auth/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from .checkers import (
2+
AlwaysAllowAuthChecker,
3+
AlwaysAllowAuthResult,
4+
AlwaysDenyAuthChecker,
25
BaseRequestAuthChecker,
36
BaseRequestAuthResult,
4-
NoAuthChecker,
5-
NoAuthResult,
67
OAuthChecker,
78
OAuthCheckerSettings,
89
OAuthResult,
@@ -25,8 +26,9 @@
2526
"BaseRequestAuthChecker",
2627
"RequestAuthCheckerProtocol",
2728
"BaseRequestAuthResult",
28-
"NoAuthChecker",
29-
"NoAuthResult",
29+
"AlwaysAllowAuthChecker",
30+
"AlwaysAllowAuthResult",
31+
"AlwaysDenyAuthChecker",
3032
"OAuthChecker",
3133
"OAuthCheckerSettings",
3234
"OAuthResult",
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
from .always_allow import (
2+
AlwaysAllowAuthChecker,
3+
AlwaysAllowAuthResult,
4+
)
5+
from .always_deny import AlwaysDenyAuthChecker
16
from .base import (
27
BaseRequestAuthChecker,
38
BaseRequestAuthResult,
49
RequestAuthCheckerProtocol,
510
)
6-
from .no_auth import (
7-
NoAuthChecker,
8-
NoAuthResult,
9-
)
1011
from .oauth import (
1112
OAuthChecker,
1213
OAuthCheckerSettings,
@@ -18,9 +19,10 @@
1819
"BaseRequestAuthChecker",
1920
"RequestAuthCheckerProtocol",
2021
"BaseRequestAuthResult",
21-
"NoAuthChecker",
22-
"NoAuthResult",
22+
"AlwaysAllowAuthChecker",
23+
"AlwaysAllowAuthResult",
2324
"OAuthChecker",
2425
"OAuthCheckerSettings",
2526
"OAuthResult",
27+
"AlwaysDenyAuthChecker",
2628
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import aiohttp.web
2+
import attr
3+
4+
import dl_app_api_base.auth.checkers.base as auth_checkers_base
5+
6+
7+
class AlwaysAllowAuthResult(auth_checkers_base.BaseRequestAuthResult):
8+
...
9+
10+
11+
@attr.define(frozen=True, kw_only=True)
12+
class AlwaysAllowAuthChecker(auth_checkers_base.BaseRequestAuthChecker):
13+
async def check(self, request: aiohttp.web.Request) -> AlwaysAllowAuthResult:
14+
return AlwaysAllowAuthResult()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import NoReturn
2+
3+
import aiohttp.web
4+
import attr
5+
6+
import dl_app_api_base.auth.checkers.base as auth_checkers_base
7+
import dl_app_api_base.auth.exc as auth_exc
8+
9+
10+
@attr.define(frozen=True, kw_only=True)
11+
class AlwaysDenyAuthChecker(auth_checkers_base.BaseRequestAuthChecker):
12+
async def check(self, request: aiohttp.web.Request) -> NoReturn:
13+
raise auth_exc.AuthFailureError("Always deny auth")

lib/dl_app_api_base/dl_app_api_base/auth/checkers/no_auth.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/dl_app_api_base/dl_app_api_base_tests/unit/auth/middleware/test_no_auth.py renamed to lib/dl_app_api_base/dl_app_api_base_tests/unit/auth/middleware/test_always_allow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ async def test_default(
99
app_client: aiohttp.ClientSession,
1010
) -> None:
1111
response = await app_client.get(
12-
"/api/v1/no_auth/ping",
12+
"/api/v1/always_allow/ping",
1313
)
1414
assert response.status == http.HTTPStatus.OK
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import http
2+
3+
import aiohttp
4+
import pytest
5+
6+
7+
@pytest.mark.asyncio
8+
async def test_default(
9+
app_client: aiohttp.ClientSession,
10+
) -> None:
11+
response = await app_client.get(
12+
"/api/v1/always_deny/ping",
13+
)
14+
assert response.status == http.HTTPStatus.UNAUTHORIZED

lib/dl_app_api_base/dl_app_api_base_tests/unit/conftest.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ async def _get_request_auth_checkers(
175175
base_checkers = await super()._get_request_auth_checkers()
176176

177177
return [
178-
dl_app_api_base.NoAuthChecker(
178+
dl_app_api_base.AlwaysAllowAuthChecker(
179179
route_matchers=[
180180
dl_app_api_base.RouteMatcher(
181181
path_regex=re.compile(r"^/api/v1/counter"),
@@ -192,10 +192,18 @@ async def _get_request_auth_checkers(
192192
)
193193
],
194194
),
195-
dl_app_api_base.NoAuthChecker(
195+
dl_app_api_base.AlwaysAllowAuthChecker(
196196
route_matchers=[
197197
dl_app_api_base.RouteMatcher(
198-
path_regex=re.compile(r"^/api/v1/no_auth/.*"),
198+
path_regex=re.compile(r"^/api/v1/always_allow/.*"),
199+
methods=frozenset(["GET"]),
200+
),
201+
],
202+
),
203+
dl_app_api_base.AlwaysDenyAuthChecker(
204+
route_matchers=[
205+
dl_app_api_base.RouteMatcher(
206+
path_regex=re.compile(r"^/api/v1/always_deny/.*"),
199207
methods=frozenset(["GET"]),
200208
),
201209
],
@@ -237,7 +245,12 @@ async def _get_aiohttp_app_routes(
237245
),
238246
dl_app_api_base.Route(
239247
method="GET",
240-
path="/api/v1/no_auth/ping",
248+
path="/api/v1/always_allow/ping",
249+
handler=PingHandler(),
250+
),
251+
dl_app_api_base.Route(
252+
method="GET",
253+
path="/api/v1/always_deny/ping",
241254
handler=PingHandler(),
242255
),
243256
]

0 commit comments

Comments
 (0)