Closed NotFenixio closed 7 months ago
18cbd276cc
)[!TIP] I can email you next time I complete a pull request if you set up your email here!
The sandbox appears to be unavailable or down.
I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.
utils.py
✓ https://github.com/Splash-Media-Co/server/commit/c9a24192b72bd0e6fd662c363c49268f23fe0003 Edit
Modify utils.py with contents:
• Add a new function named `is_client_authenticated` to the utils.py file.
• This function should accept two parameters: `client_id` and `authenticated_clients`.
• Inside the function, implement the logic to check if the given `client_id` is in the `authenticated_clients` list.
• The function should return a boolean value: `True` if the client is authenticated, `False` otherwise.
• Ensure that the function is asynchronous if it needs to be awaited when called from main.py.
--- +++ @@ -29,3 +29,6 @@ return True else: return False + +async def is_client_authenticated(client_id, authenticated_clients): + return client_id in authenticated_clients
main.py
✓ https://github.com/Splash-Media-Co/server/commit/bae3aca181a5288f517e5f5637bfada1ff939abb Edit
Modify main.py with contents:
• Import the `is_client_authenticated` function from utils.py at the top of the main.py file.
• Replace the existing `if client.id in authenticated_clients:` check with a call to `is_client_authenticated(client.id, authenticated_clients)`.
• Since the function returns a boolean, the rest of the code within the `if` block can remain unchanged.
--- +++ @@ -33,7 +33,7 @@ # Import logging helpers, OceanAudit, and rate-limiter from logs import Critical, Debug, Error, Info, Warning # noqa: F401 from oceanaudit import OceanAuditLogger -from utils import WebSocketRateLimiter +from utils import WebSocketRateLimiter, is_client_authenticated # Import DB handler from oceandb import OceanDB # noqa: F401 @@ -87,7 +87,7 @@ @server.on_disconnect async def on_disconnect(client): Info(f"Client {str(client.id)} disconnected") - if client.id in authenticated_clients: + if await is_client_authenticated(client.id, authenticated_clients): authenticated_clients.remove(client.id) @@ -129,7 +129,7 @@ case "post": match str(message["val"]["val"]["type"]): case "send": - if client.id not in authenticated_clients: + if not await is_client_authenticated(client.id, authenticated_clients): try: server.send_packet_unicast( client, @@ -215,7 +215,7 @@ with concurrent.futures.ProcessPoolExecutor() as executor: executor.submit(post, url + str(payload[0])) case "delete": - if client.id not in authenticated_clients: + if not await is_client_authenticated(client.id, authenticated_clients): try: server.send_packet_unicast( client, @@ -316,7 +316,7 @@ f"User tried to delete a post with UID {str(message["val"]["val"]["uid"])} that didn't exist", ) case "edit": - if client.id not in authenticated_clients: + if not await is_client_authenticated(client.id, authenticated_clients): try: server.send_packet_unicast( client,
main.py
! No changes made Edit
Modify main.py with contents:
• Ensure that the `is_client_authenticated` function is used wherever there is a need to check if a client is authenticated within the message handling logic.
• Replace any `if client.id in authenticated_clients:` checks with the `is_client_authenticated` function call.
• Adjust the logic accordingly to handle the boolean return value from the function.
main.py
✓ https://github.com/Splash-Media-Co/server/commit/b633dabd838f79e674c5762408624c8693d1f3d0 Edit
Modify main.py with contents:
• Use the `is_client_authenticated` function in the logic that handles post requests, specifically for the "delete" case.
• Replace the `if client.id in authenticated_clients:` check with the `is_client_authenticated` function call.
• Ensure that the logic following the authentication check remains the same, as the function will return a boolean indicating the authentication status.
--- +++ @@ -33,7 +33,7 @@ # Import logging helpers, OceanAudit, and rate-limiter from logs import Critical, Debug, Error, Info, Warning # noqa: F401 from oceanaudit import OceanAuditLogger -from utils import WebSocketRateLimiter +from utils import WebSocketRateLimiter, is_client_authenticated # Import DB handler from oceandb import OceanDB # noqa: F401 @@ -87,7 +87,7 @@ @server.on_disconnect async def on_disconnect(client): Info(f"Client {str(client.id)} disconnected") - if client.id in authenticated_clients: + if await is_client_authenticated(client.id, authenticated_clients): authenticated_clients.remove(client.id) @@ -129,7 +129,9 @@ case "post": match str(message["val"]["val"]["type"]): case "send": - if client.id not in authenticated_clients: + if not await is_client_authenticated( + client.id, authenticated_clients + ): try: server.send_packet_unicast( client, @@ -215,7 +217,7 @@ with concurrent.futures.ProcessPoolExecutor() as executor: executor.submit(post, url + str(payload[0])) case "delete": - if client.id not in authenticated_clients: + if client.id in authenticated_clients: try: server.send_packet_unicast( client, @@ -316,7 +318,9 @@ f"User tried to delete a post with UID {str(message["val"]["val"]["uid"])} that didn't exist", ) case "edit": - if client.id not in authenticated_clients: + if not await is_client_authenticated( + client.id, authenticated_clients + ): try: server.send_packet_unicast( client,
main.py
✓ https://github.com/Splash-Media-Co/server/commit/5b28c548b5c47f93d6c197933e0d046b0a3a84f7 Edit
Modify main.py with contents:
• Apply the `is_client_authenticated` function in the logic that handles edit requests.
• Replace the `if client.id in authenticated_clients:` check with the `is_client_authenticated` function call.
• Maintain the existing logic after the authentication check, as the function will return a boolean indicating if the client is authenticated.
--- +++ @@ -33,7 +33,7 @@ # Import logging helpers, OceanAudit, and rate-limiter from logs import Critical, Debug, Error, Info, Warning # noqa: F401 from oceanaudit import OceanAuditLogger -from utils import WebSocketRateLimiter +from utils import WebSocketRateLimiter, is_client_authenticated # Import DB handler from oceandb import OceanDB # noqa: F401 @@ -87,7 +87,7 @@ @server.on_disconnect async def on_disconnect(client): Info(f"Client {str(client.id)} disconnected") - if client.id in authenticated_clients: + if await is_client_authenticated(client.id, authenticated_clients): authenticated_clients.remove(client.id) @@ -129,7 +129,9 @@ case "post": match str(message["val"]["val"]["type"]): case "send": - if client.id not in authenticated_clients: + if not await is_client_authenticated( + client.id, authenticated_clients + ): try: server.send_packet_unicast( client, @@ -215,7 +217,7 @@ with concurrent.futures.ProcessPoolExecutor() as executor: executor.submit(post, url + str(payload[0])) case "delete": - if client.id not in authenticated_clients: + if client.id in authenticated_clients: try: server.send_packet_unicast( client, @@ -316,7 +318,7 @@ f"User tried to delete a post with UID {str(message["val"]["val"]["uid"])} that didn't exist", ) case "edit": - if client.id not in authenticated_clients: + if not await is_client_authenticated(client.id, authenticated_clients): try: server.send_packet_unicast( client,
I have finished reviewing the code for completeness. I did not find errors for sweep/_a2258
.
💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request.Something wrong? Let us know.
This is an automated message generated by Sweep AI.
Details
Enhancement: move the
client.id in authenticated_clients
boilerplaté from main.py to utils.py in a function that accepts theauthenticated_clients
list and the server objectChecklist
- [X] Modify `utils.py` ✓ https://github.com/Splash-Media-Co/server/commit/c9a24192b72bd0e6fd662c363c49268f23fe0003 [Edit](https://github.com/Splash-Media-Co/server/edit/sweep/_a2258/utils.py) - [X] Modify `main.py` ✓ https://github.com/Splash-Media-Co/server/commit/bae3aca181a5288f517e5f5637bfada1ff939abb [Edit](https://github.com/Splash-Media-Co/server/edit/sweep/_a2258/main.py#L89-L90) - [X] Modify `main.py` ! No changes made [Edit](https://github.com/Splash-Media-Co/server/edit/sweep/_a2258/main.py#L127-L131) - [X] Modify `main.py` ✓ https://github.com/Splash-Media-Co/server/commit/b633dabd838f79e674c5762408624c8693d1f3d0 [Edit](https://github.com/Splash-Media-Co/server/edit/sweep/_a2258/main.py#L215-L217) - [X] Modify `main.py` ✓ https://github.com/Splash-Media-Co/server/commit/5b28c548b5c47f93d6c197933e0d046b0a3a84f7 [Edit](https://github.com/Splash-Media-Co/server/edit/sweep/_a2258/main.py#L316-L318)