Reset failed login attempts counter when login success (#15564)
This commit is contained in:
1 parent
f2a99e83cd
commit
f1286f8e6b
4 files changed
+91
-6
No files matched your search
@@ -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."""
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user