Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api docstring update #50

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions backend/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,20 @@ async def check_failed_login(ip_address: str) -> None:

async def set_failed_login(ip_address: str):
ns = "login-failed-ip"
failed_attempts = await nebula.redis.incr(ns, ip_address)
failed_attempts_str = await nebula.redis.incr(ns, ip_address)
failed_attempts = int(failed_attempts_str) if failed_attempts_str else 0

await nebula.redis.expire(
ns, ip_address, 600
) # this is just for the clean-up, it cannot be used to reset the counter

if failed_attempts > nebula.config.max_failed_login_attempts:
ban_time = nebula.config.failed_login_ban_time or 0
await nebula.redis.set(
"banned-ip-until",
ip_address,
str(int(time.time() + nebula.config.failed_login_ban_time)),

str(time.time() + ban_time),
)


Expand All @@ -84,7 +88,15 @@ async def clear_failed_login(ip_address: str):


class LoginRequest(APIRequest):
"""Login using a username and password"""
"""Login using a username and password

This request will return an access token that can be used in the
Authorization header for the subsequent requests.
If the login fails, request will return 401 Unauthorized.

If the login fails too many (configurable) times,
the IP address will be banned for a certain amount of time (configurable).
"""

name: str = "login"
response_model = LoginResponseModel
Expand All @@ -94,6 +106,7 @@ async def handle(
request: Request,
payload: LoginRequestModel,
) -> LoginResponseModel:

if request is not None:
await check_failed_login(get_real_ip(request))

Expand All @@ -113,7 +126,10 @@ async def handle(


class LogoutRequest(APIRequest):
"""Log out the current user"""
"""Log out the current user.

This request will invalidate the access token used in the Authorization header.
"""

name: str = "logout"
title: str = "Logout"
Expand Down Expand Up @@ -144,7 +160,7 @@ async def handle(
self,
request: PasswordRequestModel,
user: CurrentUser,
):
) -> Response:
if request.login:
if not user.is_admin:
raise nebula.UnauthorizedException(
Expand Down
2 changes: 1 addition & 1 deletion backend/api/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async def handle(
case _:
# do not delete bins directly
raise nebula.NotImplementedException(
f"Deleting {request.obejct_type} is not implemented"
f"Deleting {request.object_type} is not implemented"
)

# Delete simple objects
Expand Down
4 changes: 2 additions & 2 deletions backend/api/jobs/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ async def set_priority(id_job: int, priority: int, user: nebula.User) -> None:


class JobsRequest(APIRequest):
"""List and control jobs"""
"""Get list of jobs, abort or restart them"""

name: str = "jobs"
title: str = "Get list of jobs, abort or restart them"
title: str = "List and control jobs"
response_model = JobsResponseModel

async def handle(
Expand Down
4 changes: 1 addition & 3 deletions backend/api/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ServicesResponseModel(ResponseModel):


class Request(APIRequest):
"""Get a list of objects"""
"""List and control installed services."""

name: str = "services"
title: str = "Service control"
Expand All @@ -57,8 +57,6 @@ async def handle(
request: ServiceRequestModel,
user: CurrentUser,
) -> ServicesResponseModel:
"""List and control installed services."""

if request.stop:
nebula.log.info(f"Stopping service {request.stop}", user=user.name)
await nebula.db.execute(
Expand Down
15 changes: 11 additions & 4 deletions backend/api/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class SessionsRequest(RequestModel):


class Sessions(APIRequest):
"""List user sessions."""

name = "sessions"
title = "List sessions"
response_model = list[SessionModel]
Expand All @@ -21,7 +23,6 @@ async def handle(
request: SessionsRequest,
user: CurrentUser,
) -> list[SessionModel]:
"""Create or update an object."""

id_user = request.id_user

Expand All @@ -46,16 +47,22 @@ class InvalidateSessionRequest(RequestModel):


class InvalidateSession(APIRequest):
"""Invalidate a user session.

This endpoint is used to invalidate an user session. It can be used
to remotely log out a user. If the user is an admin, it can also be
used to log out other users.
"""

name = "invalidate_session"
title = "Invalidate session"
title = "Invalidate a session"
responses = [204, 201]

async def handle(
self,
payload: InvalidateSessionRequest,
user: CurrentUser,
) -> None:
"""Create or update an object."""
) -> Response:

session = await Session.check(payload.token)
if session is None:
Expand Down
10 changes: 6 additions & 4 deletions backend/api/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,17 @@ async def can_modify_object(obj, user: nebula.User):


class OperationsRequest(APIRequest):
"""Create or update multiple objects in one requests."""

name: str = "ops"
title: str = "Create / update multiple objects at once"
title: str = "Save multiple objects"
response_model = OperationsResponseModel

async def handle(
self,
request: OperationsRequestModel,
user: CurrentUser,
) -> OperationsResponseModel:
"""Create or update multiple objects in one requests."""

pool = await nebula.db.pool()
result = []
Expand Down Expand Up @@ -264,16 +265,17 @@ async def handle(


class SetRequest(APIRequest):
"""Create or update an object."""

name = "set"
title = "Create or update an object"
title = "Save an object"
response_model = OperationResponseModel

async def handle(
self,
request: OperationModel,
user: CurrentUser,
) -> OperationResponseModel:
"""Create or update an object."""

operation = OperationsRequest()
result = await operation.handle(
Expand Down
4 changes: 3 additions & 1 deletion backend/api/solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SolveRequestModel(RequestModel):


class Request(APIRequest):
"""Browse the assets database."""
"""Solve a rundown placeholder"""

name: str = "solve"
responses: list[int] = [200]
Expand All @@ -63,6 +63,8 @@ async def handle(
request: SolveRequestModel,
user: CurrentUser,
) -> Response:
# TODO: check permissions
assert user is not None

solver = get_solver(request.solver)

Expand Down
2 changes: 1 addition & 1 deletion backend/nebula/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "6.0.4"
__version__ = "6.0.4"
2 changes: 1 addition & 1 deletion backend/server/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def install_endpoints(app: fastapi.FastAPI):
app.router.add_api_route(
route,
endpoint.handle, # type: ignore
name=endpoint.name,
name=endpoint.title or endpoint.name,
operation_id=slugify(endpoint.name, separator="_"),
methods=endpoint.methods,
description=docstring,
Expand Down