mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-18 05:29:38 -04:00
Record login including remembered user login events to the api based of flask login
signals.
This commit is contained in:
@@ -38,6 +38,7 @@ from app.notify_client.statistics_api_client import StatisticsApiClient
|
||||
from app.notify_client.status_api_client import StatusApiClient
|
||||
from app.notify_client.template_statistics_api_client import TemplateStatisticsApiClient
|
||||
from app.notify_client.user_api_client import UserApiClient
|
||||
from app.notify_client.events_api_client import EventsApiClient
|
||||
|
||||
login_manager = LoginManager()
|
||||
csrf = CsrfProtect()
|
||||
@@ -51,6 +52,7 @@ status_api_client = StatusApiClient()
|
||||
invite_api_client = InviteApiClient()
|
||||
statistics_api_client = StatisticsApiClient()
|
||||
template_statistics_client = TemplateStatisticsApiClient()
|
||||
events_api_client = EventsApiClient()
|
||||
asset_fingerprinter = AssetFingerprinter()
|
||||
|
||||
# The current service attached to the request stack.
|
||||
@@ -75,6 +77,7 @@ def create_app():
|
||||
invite_api_client.init_app(application)
|
||||
statistics_api_client.init_app(application)
|
||||
template_statistics_client.init_app(application)
|
||||
events_api_client.init_app(application)
|
||||
|
||||
login_manager.init_app(application)
|
||||
login_manager.login_view = 'main.sign_in'
|
||||
@@ -109,6 +112,8 @@ def create_app():
|
||||
application.context_processor(_attach_current_service)
|
||||
register_errorhandlers(application)
|
||||
|
||||
setup_event_handlers()
|
||||
|
||||
return application
|
||||
|
||||
|
||||
@@ -272,3 +277,11 @@ def register_errorhandlers(application):
|
||||
if current_app.config.get('DEBUG', None):
|
||||
raise error
|
||||
return _error_response(500)
|
||||
|
||||
|
||||
def setup_event_handlers():
|
||||
from flask.ext.login import user_logged_in, user_login_confirmed
|
||||
from app.event_handlers import on_user_logged_in, on_user_login_confirmed
|
||||
|
||||
user_logged_in.connect(on_user_logged_in)
|
||||
user_login_confirmed.connect(on_user_login_confirmed)
|
||||
|
||||
52
app/event_handlers.py
Normal file
52
app/event_handlers.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from app import events_api_client
|
||||
from flask_login import current_user
|
||||
|
||||
|
||||
def on_user_logged_in(sender, user):
|
||||
_send_event(sender, event_type='sucessful_login', user=user)
|
||||
|
||||
|
||||
# should change the event type? This is a remember me login.
|
||||
def on_user_login_confirmed(sender):
|
||||
_send_event(sender, event_type='sucessful_login_remember_me', user=current_user)
|
||||
|
||||
|
||||
def _send_event(sender, **kwargs):
|
||||
from flask import request
|
||||
try:
|
||||
event_data = _construct_event_data(request)
|
||||
if kwargs.get('user'):
|
||||
event_data['user_id'] = kwargs.get('user').id
|
||||
if not kwargs.get('event_type'):
|
||||
return
|
||||
events_api_client.create_event(kwargs['event_type'], event_data)
|
||||
except Exception as e:
|
||||
sender.logger.error('Error creating event')
|
||||
sender.logger.error(e)
|
||||
|
||||
|
||||
def _construct_event_data(request):
|
||||
return {'ip_address': _get_remote_addr(request),
|
||||
'browser_fingerprint': _get_browser_fingerprint(request)}
|
||||
|
||||
|
||||
# This might not be totally correct depending on proxy setup
|
||||
def _get_remote_addr(request):
|
||||
if request.headers.getlist("X-Forwarded-For"):
|
||||
return request.headers.getlist("X-Forwarded-For")[0]
|
||||
else:
|
||||
return request.remote_addr
|
||||
|
||||
|
||||
def _get_browser_fingerprint(request):
|
||||
browser = request.user_agent.browser
|
||||
version = request.user_agent.version
|
||||
platform = request.user_agent.platform
|
||||
user_agent_string = request.user_agent.string
|
||||
# at some point this may be hashed?
|
||||
finger_print = {'browser': browser,
|
||||
'platform': platform,
|
||||
'version': version,
|
||||
'user_agent_string': user_agent_string}
|
||||
|
||||
return finger_print
|
||||
@@ -1,12 +1,16 @@
|
||||
from flask import (render_template, redirect, url_for)
|
||||
from flask import session
|
||||
from flask_login import (login_required, logout_user)
|
||||
from flask import (
|
||||
redirect,
|
||||
url_for,
|
||||
session
|
||||
)
|
||||
|
||||
from flask.ext.login import logout_user
|
||||
|
||||
from app.main import main
|
||||
|
||||
|
||||
@main.route('/sign-out', methods=(['GET']))
|
||||
def sign_out():
|
||||
session.clear()
|
||||
logout_user()
|
||||
session.clear()
|
||||
return redirect(url_for('main.sign_in'))
|
||||
|
||||
21
app/notify_client/events_api_client.py
Normal file
21
app/notify_client/events_api_client.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from notifications_python_client.base import BaseAPIClient
|
||||
|
||||
|
||||
class EventsApiClient(BaseAPIClient):
|
||||
def __init__(self, base_url=None, client_id=None, secret=None):
|
||||
super(self.__class__, self).__init__(base_url=base_url or 'base_url',
|
||||
client_id=client_id or 'client_id',
|
||||
secret=secret or 'secret')
|
||||
|
||||
def init_app(self, app):
|
||||
self.base_url = app.config['API_HOST_NAME']
|
||||
self.client_id = app.config['ADMIN_CLIENT_USER_NAME']
|
||||
self.secret = app.config['ADMIN_CLIENT_SECRET']
|
||||
|
||||
def create_event(self, event_type, event_data):
|
||||
data = {
|
||||
'event_type': event_type,
|
||||
'data': event_data
|
||||
}
|
||||
resp = self.post(url='/events', data=data)
|
||||
return resp['data']
|
||||
Reference in New Issue
Block a user