Reset failed login attempts counter when login success (#15564)

This commit is contained in:
Jason Hu authored and Paulus Schoutsen committed 2018-07-20 12:09:48 +02:00
1 parent f2a99e83cd
commit f1286f8e6b
4 files changed
+91 -6

No files matched your search

+26 -1
View File
@@ -72,7 +72,11 @@ async def ban_middleware(request, handler):
async def process_wrong_login(request):
"""Process a wrong login attempt."""
"""Process a wrong login attempt.
Increase failed login attempts counter for remote IP address.
Add ip ban entry if failed login attempts exceeds threshold.
"""
remote_addr = request[KEY_REAL_IP]
msg = ('Login attempt or request with invalid authentication '
@@ -107,6 +111,27 @@ async def process_wrong_login(request):
'Banning IP address', NOTIFICATION_ID_BAN)
async def process_success_login(request):
"""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 = request[KEY_REAL_IP]
# Check if ban middleware is loaded
if (KEY_BANNED_IPS not in request.app or
request.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:
_LOGGER.debug('Login success, reset failed login attempts counter'
' from %s', remote_addr)
request.app[KEY_FAILED_LOGIN_ATTEMPTS].pop(remote_addr)
class IpBan:
"""Represents banned IP address."""
+6 -2
View File
@@ -12,6 +12,7 @@ from aiohttp import web
from aiohttp.web_exceptions import HTTPUnauthorized, HTTPInternalServerError
import homeassistant.remote as rem
from homeassistant.components.http.ban import process_success_login
from homeassistant.core import is_callback
from homeassistant.const import CONTENT_TYPE_JSON
@@ -91,8 +92,11 @@ def request_handler_factory(view, handler):
authenticated = request.get(KEY_AUTHENTICATED, False)
if view.requires_auth and not authenticated:
raise HTTPUnauthorized()
if view.requires_auth:
if authenticated:
await process_success_login(request)
else:
raise HTTPUnauthorized()
_LOGGER.info('Serving %s to %s (auth: %s)',
request.path, request.get(KEY_REAL_IP), authenticated)
+3 -1
View File
@@ -26,7 +26,8 @@ from homeassistant.helpers.service import async_get_all_descriptions
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.http.auth import validate_password
from homeassistant.components.http.const import KEY_AUTHENTICATED
from homeassistant.components.http.ban import process_wrong_login
from homeassistant.components.http.ban import process_wrong_login, \
process_success_login
DOMAIN = 'websocket_api'
@@ -360,6 +361,7 @@ class ActiveConnection:
return wsock
self.debug("Auth OK")
await process_success_login(request)
await self.wsock.send_json(auth_ok_message())
# ---------- AUTH PHASE OVER ----------