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

Small cleanups to process_success_login #103282

Merged
merged 3 commits into from
Nov 7, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/auth/login_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ async def _async_flow_result_to_response(
f"Login blocked: {user_access_error}", HTTPStatus.FORBIDDEN
)

await process_success_login(request)
process_success_login(request)
result["result"] = self._store_result(client_id, result_obj)

return self.json(result)
Expand Down
19 changes: 10 additions & 9 deletions homeassistant/components/http/ban.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,27 +162,28 @@ async def process_wrong_login(request: Request) -> None:
)


async def process_success_login(request: Request) -> None:
@callback
def process_success_login(request: Request) -> None:
"""Process a success login attempt.

Reset failed login attempts counter for remote IP address.
No release IP address from banned list function, it can only be done by
manual modify ip bans config file.
"""
remote_addr = ip_address(request.remote) # type: ignore[arg-type]

app = request.app
# Check if ban middleware is loaded
if KEY_BAN_MANAGER not in request.app or request.app[KEY_LOGIN_THRESHOLD] < 1:
if KEY_BAN_MANAGER not in app or app[KEY_LOGIN_THRESHOLD] < 1:
return

if (
remote_addr in request.app[KEY_FAILED_LOGIN_ATTEMPTS]
and request.app[KEY_FAILED_LOGIN_ATTEMPTS][remote_addr] > 0
):
remote_addr = ip_address(request.remote) # type: ignore[arg-type]
login_attempt_history: defaultdict[IPv4Address | IPv6Address, int] = app[
KEY_FAILED_LOGIN_ATTEMPTS
]
if remote_addr in login_attempt_history and login_attempt_history[remote_addr] > 0:
_LOGGER.debug(
"Login success, reset failed login attempts counter from %s", remote_addr
)
request.app[KEY_FAILED_LOGIN_ATTEMPTS].pop(remote_addr)
login_attempt_history.pop(remote_addr)


class IpBan:
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/websocket_api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ async def _async_finish_auth(
) -> ActiveConnection:
"""Create an active connection."""
self._logger.debug("Auth OK")
await process_success_login(self._request)
process_success_login(self._request)
self._send_message(auth_ok_message())
return ActiveConnection(
self._logger, self._hass, self._send_message, user, refresh_token
Expand Down
16 changes: 15 additions & 1 deletion tests/components/http/test_ban.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
KEY_BAN_MANAGER,
KEY_FAILED_LOGIN_ATTEMPTS,
IpBanManager,
process_success_login,
setup_bans,
)
from homeassistant.components.http.view import request_handler_factory
Expand Down Expand Up @@ -332,9 +333,14 @@ async def auth_handler(request):
"""Return 200 status code."""
return None, 200

async def auth_true_handler(request):
"""Return 200 status code."""
process_success_login(request)
return None, 200

app.router.add_get(
"/auth_true",
request_handler_factory(hass, Mock(requires_auth=True), auth_handler),
request_handler_factory(hass, Mock(requires_auth=True), auth_true_handler),
)
app.router.add_get(
"/auth_false",
Expand Down Expand Up @@ -377,4 +383,12 @@ async def mock_auth(request, handler):
# We no longer support trusted networks.
resp = await client.get("/auth_true")
assert resp.status == HTTPStatus.OK
assert app[KEY_FAILED_LOGIN_ATTEMPTS][remote_ip] == 0

resp = await client.get("/auth_false")
assert resp.status == HTTPStatus.UNAUTHORIZED
assert app[KEY_FAILED_LOGIN_ATTEMPTS][remote_ip] == 1

resp = await client.get("/auth_false")
assert resp.status == HTTPStatus.UNAUTHORIZED
assert app[KEY_FAILED_LOGIN_ATTEMPTS][remote_ip] == 2