mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-18 05:29:38 -04:00
Merge pull request #1146 from alphagov/session-id
check users' session id.
This commit is contained in:
@@ -50,6 +50,7 @@ from app.notify_client.user_api_client import UserApiClient
|
||||
from app.notify_client.events_api_client import EventsApiClient
|
||||
from app.notify_client.provider_client import ProviderClient
|
||||
from app.notify_client.organisations_client import OrganisationsClient
|
||||
from app.notify_client.models import AnonymousUser
|
||||
|
||||
login_manager = LoginManager()
|
||||
csrf = CsrfProtect()
|
||||
@@ -103,6 +104,7 @@ def create_app():
|
||||
login_manager.login_view = 'main.sign_in'
|
||||
login_manager.login_message_category = 'default'
|
||||
login_manager.session_protection = None
|
||||
login_manager.anonymous_user = AnonymousUser
|
||||
|
||||
from app.main import main as main_blueprint
|
||||
application.register_blueprint(main_blueprint)
|
||||
|
||||
@@ -76,7 +76,13 @@ def sign_in():
|
||||
).format(password_reset=url_for('.forgot_password'))
|
||||
))
|
||||
|
||||
return render_template('views/signin.html', form=form, again=bool(request.args.get('next')))
|
||||
other_device = current_user.logged_in_elsewhere()
|
||||
return render_template(
|
||||
'views/signin.html',
|
||||
form=form,
|
||||
again=bool(request.args.get('next')),
|
||||
other_device=other_device
|
||||
)
|
||||
|
||||
|
||||
@login_manager.unauthorized_handler
|
||||
|
||||
@@ -26,6 +26,9 @@ def two_factor():
|
||||
if form.validate_on_submit():
|
||||
try:
|
||||
user = user_api_client.get_user(user_id)
|
||||
# the user will have a new current_session_id set by the API - store it in the cookie so we can match it in
|
||||
# future requests
|
||||
session['current_session_id'] = user.current_session_id
|
||||
services = service_api_client.get_active_services({'user_id': str(user_id)}).get('data', [])
|
||||
# Check if coming from new password page
|
||||
if 'password' in session['user_details']:
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from flask_login import (UserMixin, login_fresh)
|
||||
from flask_login import UserMixin, AnonymousUserMixin, login_fresh
|
||||
from flask import session
|
||||
|
||||
|
||||
class User(UserMixin):
|
||||
@@ -13,20 +14,26 @@ class User(UserMixin):
|
||||
self._state = fields.get('state')
|
||||
self.max_failed_login_count = max_failed_login_count
|
||||
self.platform_admin = fields.get('platform_admin')
|
||||
self.current_session_id = fields.get('current_session_id')
|
||||
|
||||
def get_id(self):
|
||||
return self.id
|
||||
|
||||
def logged_in_elsewhere(self):
|
||||
# if the current user (ie: db object) has no session, they've never logged in before
|
||||
return self.current_session_id is not None and session.get('current_session_id') != self.current_session_id
|
||||
|
||||
@property
|
||||
def is_active(self):
|
||||
return self.state == 'active'
|
||||
|
||||
@property
|
||||
def is_authenticated(self):
|
||||
# To handle remember me token renewal
|
||||
if not login_fresh():
|
||||
return False
|
||||
return super(User, self).is_authenticated
|
||||
return (
|
||||
login_fresh() and
|
||||
not self.logged_in_elsewhere() and
|
||||
super(User, self).is_authenticated
|
||||
)
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
@@ -114,15 +121,18 @@ class User(UserMixin):
|
||||
return self.failed_login_count >= self.max_failed_login_count
|
||||
|
||||
def serialize(self):
|
||||
dct = {"id": self.id,
|
||||
"name": self.name,
|
||||
"email_address": self.email_address,
|
||||
"mobile_number": self.mobile_number,
|
||||
"password_changed_at": self.password_changed_at,
|
||||
"state": self.state,
|
||||
"failed_login_count": self.failed_login_count,
|
||||
"permissions": [x for x in self._permissions]}
|
||||
if getattr(self, '_password', None):
|
||||
dct = {
|
||||
"id": self.id,
|
||||
"name": self.name,
|
||||
"email_address": self.email_address,
|
||||
"mobile_number": self.mobile_number,
|
||||
"password_changed_at": self.password_changed_at,
|
||||
"state": self.state,
|
||||
"failed_login_count": self.failed_login_count,
|
||||
"permissions": [x for x in self._permissions],
|
||||
"current_session_id": self.current_session_id
|
||||
}
|
||||
if hasattr(self, '_password'):
|
||||
dct['password'] = self._password
|
||||
return dct
|
||||
|
||||
@@ -174,3 +184,9 @@ class InvitedUser(object):
|
||||
else:
|
||||
data['permissions'] = self.permissions
|
||||
return data
|
||||
|
||||
|
||||
class AnonymousUser(AnonymousUserMixin):
|
||||
# set the anonymous user so that if a new browser hits us we don't error http://stackoverflow.com/a/19275188
|
||||
def logged_in_elsewhere(self):
|
||||
return False
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import uuid
|
||||
|
||||
from flask import session
|
||||
from notifications_python_client.errors import HTTPError
|
||||
|
||||
from app.notify_client import NotifyAdminAPIClient
|
||||
|
||||
@@ -13,9 +13,15 @@
|
||||
|
||||
{% if again %}
|
||||
<h1 class="heading-large">You need to sign in again</h1>
|
||||
<p>
|
||||
We sign you out if you haven’t used Notify for a while.
|
||||
</p>
|
||||
{% if other_device %}
|
||||
<p>
|
||||
We signed you out because you logged in to Notify on another device.
|
||||
</p>
|
||||
{% else %}
|
||||
<p>
|
||||
We signed you out because you haven’t used Notify for a while.
|
||||
</p>
|
||||
{% endif %}
|
||||
{% else %}
|
||||
<h1 class="heading-large">Sign in</h1>
|
||||
<p>
|
||||
|
||||
Reference in New Issue
Block a user