Skip to content

Commit 304f14d

Browse files
committed
[#69419] server: add 'authenticated' API decorator
1 parent eeadb7d commit 304f14d

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

server/src/api/v1/middleware.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@
6767
"""providing a management token with read-write scope""" \
6868
f""" ``{SCOPE_READ_WRITE}``."""
6969

70+
""" Text to append after the endpoint's docstring when a token
71+
with zero or more scopes is required
72+
"""
73+
DOCS_AUTHENTICATED_API_TEXT = """.. warning:: Accessing this endpoint requires """ \
74+
"""providing an authorization token"""
75+
7076
""" Unprotected API routes
7177
"""
7278
DOCS_PUBLIC_API_TEXT = """.. note:: This is a public API route; no """ \
@@ -749,6 +755,17 @@ def scope_check_callback(scopes):
749755
return __management_api(scope_check_callback, append_scopes=True)(f)
750756

751757

758+
def authenticated_api(f):
759+
"""Decorator to be used for API routes available to every
760+
authenticated user
761+
"""
762+
763+
f.__rdfm_api_privileges__ = "authenticated"
764+
__add_scope_docs(f, DOCS_AUTHENTICATED_API_TEXT)
765+
766+
return management_user_validation(f)
767+
768+
752769
def public_api(f):
753770
"""Decorator to be used on public API routes
754771

server/src/api/v1/permissions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
management_read_only_api,
1111
management_read_write_api,
1212
deserialize_schema,
13-
check_admin_rights
13+
check_admin_rights,
14+
authenticated_api
1415
)
1516
from typing import List, Optional
1617
import server
@@ -36,7 +37,7 @@ def model_to_schema(
3637

3738

3839
@permissions_blueprint.route("/api/v1/permissions")
39-
@management_user_validation
40+
@authenticated_api
4041
@wrap_api_exception("permissions fetching failed")
4142
def fetch_all(**kwargs):
4243
"""Fetch all permissions

server/tests/test-server-route-correctness.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_authorization_on_all_routes(endpoint, func):
4747
# Check for the presence of a decorator
4848
# The device/management API decorators add a special field for identification
4949
error_string = (f"route function {func.__name__} should be decorated using "
50-
"a management, device or public API decorator, but none was found")
50+
"a management, authenticated, device or public API decorator, but none was found")
5151
assert hasattr(func, "__rdfm_api_privileges__"), error_string
5252

5353

0 commit comments

Comments
 (0)