login: Fix logging for login/register actions.

Previously, Zulip's server logs would not show which user or client
was involved in login or user registration actions, which made
debugging more annoying than it needed to be.
This commit is contained in:
Tim Abbott 2017-08-24 16:11:30 -07:00
parent a570a57fd5
commit 8b0c0c33b5
5 changed files with 20 additions and 11 deletions

View File

@ -2,7 +2,7 @@ from __future__ import absolute_import
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.http import HttpResponseRedirect, HttpResponse from django.http import HttpResponseRedirect, HttpResponse
from django.contrib.auth import REDIRECT_FIELD_NAME from django.contrib.auth import REDIRECT_FIELD_NAME, login as django_login
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django.http import QueryDict, HttpResponseNotAllowed, HttpRequest from django.http import QueryDict, HttpResponseNotAllowed, HttpRequest
from django.http.multipartparser import MultiPartParser from django.http.multipartparser import MultiPartParser
@ -350,6 +350,15 @@ def logged_in_and_active(request):
return False return False
return check_subdomain(get_subdomain(request), request.user.realm.subdomain) return check_subdomain(get_subdomain(request), request.user.realm.subdomain)
def do_login(request, user_profile):
# type: (HttpRequest, UserProfile) -> None
"""Creates a session, logging in the user, using the Django method,
and also adds helpful data needed by our server logs.
"""
django_login(request, user_profile)
request._email = user_profile.email
process_client(request, user_profile, is_browser_view=True)
def add_logging_data(view_func): def add_logging_data(view_func):
# type: (ViewFuncT) -> ViewFuncT # type: (ViewFuncT) -> ViewFuncT
@wraps(view_func) @wraps(view_func)

View File

@ -449,7 +449,7 @@ class GitHubAuthBackendTest(ZulipTestCase):
# type: () -> None # type: () -> None
with mock.patch('social_core.backends.github.GithubOAuth2.do_auth', with mock.patch('social_core.backends.github.GithubOAuth2.do_auth',
side_effect=self.do_auth), \ side_effect=self.do_auth), \
mock.patch('zerver.views.auth.login'): mock.patch('zerver.views.auth.do_login'):
response = dict(email=self.email, name=self.name) response = dict(email=self.email, name=self.name)
result = self.backend.do_auth(response=response) result = self.backend.do_auth(response=response)
assert(result is not None) assert(result is not None)

View File

@ -277,7 +277,7 @@ class LoginTest(ZulipTestCase):
with queries_captured() as queries: with queries_captured() as queries:
self.register(self.nonreg_email('test'), "test") self.register(self.nonreg_email('test'), "test")
# Ensure the number of queries we make is not O(streams) # Ensure the number of queries we make is not O(streams)
self.assert_length(queries, 66) self.assert_length(queries, 67)
user_profile = self.nonreg_user('test') user_profile = self.nonreg_user('test')
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id) self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)
self.assertFalse(user_profile.enable_stream_desktop_notifications) self.assertFalse(user_profile.enable_stream_desktop_notifications)

View File

@ -3,12 +3,12 @@ from __future__ import absolute_import
from django.conf import settings from django.conf import settings
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.validators import validate_email from django.core.validators import validate_email
from django.contrib.auth import authenticate, login, get_backends from django.contrib.auth import authenticate, get_backends
from django.contrib.auth.views import login as django_login_page, \ from django.contrib.auth.views import login as django_login_page, \
logout_then_login as django_logout_then_login logout_then_login as django_logout_then_login
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from zerver.decorator import authenticated_json_post_view, require_post, \ from zerver.decorator import authenticated_json_post_view, require_post, \
process_client process_client, do_login
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect, \ from django.http import HttpRequest, HttpResponse, HttpResponseRedirect, \
HttpResponseNotFound HttpResponseNotFound
from django.middleware.csrf import get_token from django.middleware.csrf import get_token
@ -144,7 +144,7 @@ def login_or_register_remote_user(request, remote_username, user_profile, full_n
return response return response
login(request, user_profile) do_login(request, user_profile)
if settings.REALMS_HAVE_SUBDOMAINS and user_profile.realm.subdomain is not None: if settings.REALMS_HAVE_SUBDOMAINS and user_profile.realm.subdomain is not None:
return HttpResponseRedirect(user_profile.realm.uri) return HttpResponseRedirect(user_profile.realm.uri)
return HttpResponseRedirect("%s%s" % (settings.EXTERNAL_URI_SCHEME, return HttpResponseRedirect("%s%s" % (settings.EXTERNAL_URI_SCHEME,
@ -509,7 +509,7 @@ def dev_direct_login(request, **kwargs):
user_profile = authenticate(username=email, realm_subdomain=get_subdomain(request)) user_profile = authenticate(username=email, realm_subdomain=get_subdomain(request))
if user_profile is None: if user_profile is None:
raise Exception("User cannot login") raise Exception("User cannot login")
login(request, user_profile) do_login(request, user_profile)
if settings.REALMS_HAVE_SUBDOMAINS and user_profile.realm.subdomain is not None: if settings.REALMS_HAVE_SUBDOMAINS and user_profile.realm.subdomain is not None:
return HttpResponseRedirect(user_profile.realm.uri) return HttpResponseRedirect(user_profile.realm.uri)
return HttpResponseRedirect("%s%s" % (settings.EXTERNAL_URI_SCHEME, return HttpResponseRedirect("%s%s" % (settings.EXTERNAL_URI_SCHEME,
@ -546,7 +546,7 @@ def api_dev_fetch_api_key(request, username=REQ()):
if user_profile is None: if user_profile is None:
return json_error(_("This user is not registered."), return json_error(_("This user is not registered."),
data={"reason": "unregistered"}, status=403) data={"reason": "unregistered"}, status=403)
login(request, user_profile) do_login(request, user_profile)
return json_success({"api_key": user_profile.api_key, "email": user_profile.email}) return json_success({"api_key": user_profile.api_key, "email": user_profile.email})
@csrf_exempt @csrf_exempt

View File

@ -4,7 +4,7 @@ from typing import Any, List, Dict, Mapping, Optional, Text
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.conf import settings from django.conf import settings
from django.contrib.auth import authenticate, login, get_backends from django.contrib.auth import authenticate, get_backends
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, HttpResponseForbidden, HttpResponse, HttpRequest from django.http import HttpResponseRedirect, HttpResponseForbidden, HttpResponse, HttpRequest
from django.shortcuts import redirect, render from django.shortcuts import redirect, render
@ -26,7 +26,7 @@ from zerver.forms import RegistrationForm, HomepageForm, RealmCreationForm, \
from zerver.lib.actions import is_inactive, do_set_user_display_setting from zerver.lib.actions import is_inactive, do_set_user_display_setting
from django_auth_ldap.backend import LDAPBackend, _LDAPUser from django_auth_ldap.backend import LDAPBackend, _LDAPUser
from zerver.decorator import require_post, has_request_variables, \ from zerver.decorator import require_post, has_request_variables, \
JsonableError, get_user_profile_by_email, REQ JsonableError, get_user_profile_by_email, REQ, do_login
from zerver.lib.onboarding import send_initial_pms, setup_initial_streams, \ from zerver.lib.onboarding import send_initial_pms, setup_initial_streams, \
setup_initial_private_stream, send_initial_realm_messages setup_initial_private_stream, send_initial_realm_messages
from zerver.lib.response import json_success from zerver.lib.response import json_success
@ -247,7 +247,7 @@ def accounts_register(request):
# Mark the user as having been just created, so no login email is sent # Mark the user as having been just created, so no login email is sent
auth_result.just_registered = True auth_result.just_registered = True
login(request, auth_result) do_login(request, auth_result)
return HttpResponseRedirect(realm.uri + reverse('zerver.views.home.home')) return HttpResponseRedirect(realm.uri + reverse('zerver.views.home.home'))
return render( return render(