Skip to content
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
10 changes: 10 additions & 0 deletions homeassistant/components/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
import voluptuous as vol

from homeassistant import data_entry_flow
from homeassistant.components.http.ban import process_wrong_login, \
log_invalid_auth
from homeassistant.core import callback
from homeassistant.helpers.data_entry_flow import (
FlowManagerIndexView, FlowManagerResourceView)
Expand Down Expand Up @@ -183,6 +185,7 @@ async def get(self, request):
vol.Required('handler'): vol.Any(str, list),
vol.Required('redirect_uri'): str,
}))
@log_invalid_auth
async def post(self, request, data):
"""Create a new login flow."""
if not indieauth.verify_redirect_uri(data['client_id'],
Expand Down Expand Up @@ -212,6 +215,7 @@ async def get(self, request, flow_id):
@RequestDataValidator(vol.Schema({
'client_id': str
}, extra=vol.ALLOW_EXTRA))
@log_invalid_auth
async def post(self, request, flow_id, data):
"""Handle progressing a login flow request."""
client_id = data.pop('client_id')
Expand All @@ -227,6 +231,11 @@ async def post(self, request, flow_id, data):
return self.json_message('User input malformed', 400)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an invalid login? This might be if a user does not fill in a username or password. Or if an MFA module requires numbers and we put in a string.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required field and simple format check for example numbers check, length check etc should done in frontend, if it is still malformed, someone is trying to hack in.


if result['type'] != data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
# @log_invalid_auth does not work here since it returns HTTP 200
# need manually log failed login attempts
if result['errors'] is not None and \
result['errors'].get('base') == 'invalid_auth':
await process_wrong_login(request)
return self.json(self._prepare_result_json(result))

result.pop('data')
Expand All @@ -247,6 +256,7 @@ def __init__(self, retrieve_credentials):
"""Initialize the grant token view."""
self._retrieve_credentials = retrieve_credentials

@log_invalid_auth
async def post(self, request):
"""Grant a token."""
hass = request.app['hass']
Expand Down
12 changes: 11 additions & 1 deletion homeassistant/components/http/ban.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Ban logic for HTTP component."""

from collections import defaultdict
from datetime import datetime
from ipaddress import ip_address
Expand Down Expand Up @@ -71,6 +70,17 @@ async def ban_middleware(request, handler):
raise


def log_invalid_auth(func):
"""Decorator to handle invalid auth or failed login attempts."""
async def handle_req(view, request, *args, **kwargs):
"""Try to log failed login attempts if response status >= 400."""
resp = await func(view, request, *args, **kwargs)
if resp.status >= 400:
await process_wrong_login(request)
return resp
return handle_req


async def process_wrong_login(request):
"""Process a wrong login attempt.

Expand Down