2016-03-22 10:05:43 -05:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2018-01-05 04:42:49 -06:00
|
|
|
# Copyright (C) 2013 - 2018, The pgAdmin Development Team
|
2016-03-22 10:05:43 -05:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
"""
|
|
|
|
Implements the server-side session management.
|
|
|
|
|
2016-07-08 06:25:53 -05:00
|
|
|
Credit/Reference: http://flask.pocoo.org/snippets/109/
|
2016-03-22 10:05:43 -05:00
|
|
|
|
|
|
|
Modified to support both Python 2.6+ & Python 3.x
|
|
|
|
"""
|
|
|
|
|
2016-07-08 06:25:53 -05:00
|
|
|
import base64
|
|
|
|
import datetime
|
|
|
|
import hmac
|
|
|
|
import hashlib
|
2016-06-21 08:12:14 -05:00
|
|
|
import os
|
2016-07-08 06:25:53 -05:00
|
|
|
import random
|
|
|
|
import string
|
Do not dump the session data on the disk on every request.
Session object is updated, everytime a request is being served, and
that was forcing the session object dumped on the dist on every request.
On windows, it was causing issues on slower system on startup. Because -
windows file system locks the file, when it is opened by any
application. And, frequent requests on the pgAdmin main UI rendering
was causing issues, because of that.
In order to resolve the issue, we will not write the session data on
disk for every request, but - only after certain delay (in seconds),
from it was last written. It can be configured using the attribute
'PGADMIN_SESSION_DISK_WRITE_DELAY' in the configuration file,
default vaule for the delay is 10.
(i.e. 10 seconds)
2017-07-25 05:06:46 -05:00
|
|
|
import time
|
2018-10-09 05:34:13 -05:00
|
|
|
import config
|
2016-03-22 10:05:43 -05:00
|
|
|
from uuid import uuid4
|
2018-08-22 01:25:39 -05:00
|
|
|
from threading import Lock
|
2018-02-01 07:29:18 -06:00
|
|
|
from flask import current_app, request, flash, redirect
|
|
|
|
from flask_login import login_url
|
|
|
|
from pgadmin.utils.ajax import make_json_response
|
2016-06-21 08:12:14 -05:00
|
|
|
|
2016-03-22 10:05:43 -05:00
|
|
|
try:
|
2016-07-08 06:25:53 -05:00
|
|
|
from cPickle import dump, load
|
2018-01-31 07:58:55 -06:00
|
|
|
except ImportError:
|
2016-07-08 06:25:53 -05:00
|
|
|
from pickle import dump, load
|
|
|
|
|
|
|
|
try:
|
|
|
|
from collections import OrderedDict
|
2018-01-31 07:58:55 -06:00
|
|
|
except ImportError:
|
2016-07-08 06:25:53 -05:00
|
|
|
from ordereddict import OrderedDict
|
2016-03-22 10:05:43 -05:00
|
|
|
|
|
|
|
from flask.sessions import SessionInterface, SessionMixin
|
2016-07-08 06:25:53 -05:00
|
|
|
from werkzeug.datastructures import CallbackDict
|
|
|
|
|
2016-03-22 10:05:43 -05:00
|
|
|
|
2016-07-08 06:25:53 -05:00
|
|
|
def _calc_hmac(body, secret):
|
2016-07-11 05:59:03 -05:00
|
|
|
return base64.b64encode(
|
|
|
|
hmac.new(
|
|
|
|
secret.encode(), body.encode(), hashlib.sha1
|
|
|
|
).digest()
|
|
|
|
).decode()
|
2016-03-22 10:05:43 -05:00
|
|
|
|
2016-07-08 06:25:53 -05:00
|
|
|
|
2018-08-22 01:25:39 -05:00
|
|
|
sess_lock = Lock()
|
2018-10-09 05:34:13 -05:00
|
|
|
LAST_CHECK_SESSION_FILES = None
|
2018-08-22 01:25:39 -05:00
|
|
|
|
|
|
|
|
2016-07-08 06:25:53 -05:00
|
|
|
class ManagedSession(CallbackDict, SessionMixin):
|
2018-01-31 07:58:55 -06:00
|
|
|
def __init__(self, initial=None, sid=None, new=False, randval=None,
|
|
|
|
hmac_digest=None):
|
2016-07-08 06:25:53 -05:00
|
|
|
def on_update(self):
|
|
|
|
self.modified = True
|
|
|
|
|
|
|
|
CallbackDict.__init__(self, initial, on_update)
|
2016-03-22 10:05:43 -05:00
|
|
|
self.sid = sid
|
2016-07-08 06:25:53 -05:00
|
|
|
self.new = new
|
2016-03-22 10:05:43 -05:00
|
|
|
self.modified = False
|
2016-07-08 06:25:53 -05:00
|
|
|
self.randval = randval
|
Do not dump the session data on the disk on every request.
Session object is updated, everytime a request is being served, and
that was forcing the session object dumped on the dist on every request.
On windows, it was causing issues on slower system on startup. Because -
windows file system locks the file, when it is opened by any
application. And, frequent requests on the pgAdmin main UI rendering
was causing issues, because of that.
In order to resolve the issue, we will not write the session data on
disk for every request, but - only after certain delay (in seconds),
from it was last written. It can be configured using the attribute
'PGADMIN_SESSION_DISK_WRITE_DELAY' in the configuration file,
default vaule for the delay is 10.
(i.e. 10 seconds)
2017-07-25 05:06:46 -05:00
|
|
|
self.last_write = None
|
2017-09-18 08:39:43 -05:00
|
|
|
self.force_write = False
|
2016-07-08 06:25:53 -05:00
|
|
|
self.hmac_digest = hmac_digest
|
2018-10-09 05:34:13 -05:00
|
|
|
self.permanent = True
|
2016-07-08 06:25:53 -05:00
|
|
|
|
|
|
|
def sign(self, secret):
|
|
|
|
if not self.hmac_digest:
|
2016-07-11 05:59:03 -05:00
|
|
|
if hasattr(string, 'lowercase'):
|
|
|
|
population = string.lowercase
|
|
|
|
# If script is running under python3
|
|
|
|
elif hasattr(string, 'ascii_lowercase'):
|
|
|
|
population = string.ascii_lowercase
|
|
|
|
population += string.digits
|
|
|
|
|
|
|
|
self.randval = ''.join(random.sample(population, 20))
|
2018-01-31 07:58:55 -06:00
|
|
|
self.hmac_digest = _calc_hmac(
|
|
|
|
'%s:%s' % (self.sid, self.randval), secret)
|
2016-07-08 06:25:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
class SessionManager(object):
|
|
|
|
def new_session(self):
|
|
|
|
'Create a new session'
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def exists(self, sid):
|
|
|
|
'Does the given session-id exist?'
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def remove(self, sid):
|
|
|
|
'Remove the session'
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get(self, sid, digest):
|
|
|
|
'Retrieve a managed session by session-id, checking the HMAC digest'
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def put(self, session):
|
|
|
|
'Store a managed session'
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class CachingSessionManager(SessionManager):
|
2018-07-05 05:12:03 -05:00
|
|
|
def __init__(self, parent, num_to_store, skip_paths=[]):
|
2016-07-08 06:25:53 -05:00
|
|
|
self.parent = parent
|
|
|
|
self.num_to_store = num_to_store
|
|
|
|
self._cache = OrderedDict()
|
2018-07-05 05:12:03 -05:00
|
|
|
self.skip_paths = skip_paths
|
2016-07-08 06:25:53 -05:00
|
|
|
|
|
|
|
def _normalize(self):
|
|
|
|
if len(self._cache) > self.num_to_store:
|
|
|
|
# Flush 20% of the cache
|
2018-08-22 01:25:39 -05:00
|
|
|
with sess_lock:
|
|
|
|
while len(self._cache) > (self.num_to_store * 0.8):
|
|
|
|
self._cache.popitem(False)
|
2016-07-08 06:25:53 -05:00
|
|
|
|
|
|
|
def new_session(self):
|
|
|
|
session = self.parent.new_session()
|
2018-07-05 05:12:03 -05:00
|
|
|
|
|
|
|
# Do not store the session if skip paths
|
|
|
|
for sp in self.skip_paths:
|
|
|
|
if request.path.startswith(sp):
|
|
|
|
return session
|
|
|
|
|
2018-08-22 01:25:39 -05:00
|
|
|
with sess_lock:
|
|
|
|
self._cache[session.sid] = session
|
2016-07-08 06:25:53 -05:00
|
|
|
self._normalize()
|
|
|
|
|
|
|
|
return session
|
|
|
|
|
|
|
|
def remove(self, sid):
|
2018-08-22 01:25:39 -05:00
|
|
|
with sess_lock:
|
|
|
|
self.parent.remove(sid)
|
|
|
|
if sid in self._cache:
|
|
|
|
del self._cache[sid]
|
2016-07-08 06:25:53 -05:00
|
|
|
|
|
|
|
def exists(self, sid):
|
2018-08-22 01:25:39 -05:00
|
|
|
with sess_lock:
|
|
|
|
if sid in self._cache:
|
|
|
|
return True
|
|
|
|
return self.parent.exists(sid)
|
2016-07-08 06:25:53 -05:00
|
|
|
|
|
|
|
def get(self, sid, digest):
|
|
|
|
session = None
|
2018-08-22 01:25:39 -05:00
|
|
|
with sess_lock:
|
|
|
|
if sid in self._cache:
|
|
|
|
session = self._cache[sid]
|
|
|
|
if session.hmac_digest != digest:
|
|
|
|
session = None
|
2016-07-08 06:25:53 -05:00
|
|
|
|
2018-08-22 01:25:39 -05:00
|
|
|
# reset order in Dict
|
|
|
|
del self._cache[sid]
|
2016-07-08 06:25:53 -05:00
|
|
|
|
2018-08-22 01:25:39 -05:00
|
|
|
if not session:
|
|
|
|
session = self.parent.get(sid, digest)
|
2016-07-08 06:25:53 -05:00
|
|
|
|
2018-08-22 01:25:39 -05:00
|
|
|
# Do not store the session if skip paths
|
|
|
|
for sp in self.skip_paths:
|
|
|
|
if request.path.startswith(sp):
|
|
|
|
return session
|
2018-07-05 05:12:03 -05:00
|
|
|
|
2018-08-22 01:25:39 -05:00
|
|
|
self._cache[sid] = session
|
2016-07-08 06:25:53 -05:00
|
|
|
self._normalize()
|
|
|
|
|
|
|
|
return session
|
|
|
|
|
|
|
|
def put(self, session):
|
2018-08-22 01:25:39 -05:00
|
|
|
with sess_lock:
|
|
|
|
self.parent.put(session)
|
2018-07-05 05:12:03 -05:00
|
|
|
|
2018-08-22 01:25:39 -05:00
|
|
|
# Do not store the session if skip paths
|
|
|
|
for sp in self.skip_paths:
|
|
|
|
if request.path.startswith(sp):
|
|
|
|
return
|
2018-07-05 05:12:03 -05:00
|
|
|
|
2018-08-22 01:25:39 -05:00
|
|
|
if session.sid in self._cache:
|
|
|
|
try:
|
|
|
|
del self._cache[session.sid]
|
|
|
|
except Exception:
|
|
|
|
pass
|
2018-07-05 05:12:03 -05:00
|
|
|
|
2018-08-22 01:25:39 -05:00
|
|
|
self._cache[session.sid] = session
|
2016-07-08 06:25:53 -05:00
|
|
|
self._normalize()
|
|
|
|
|
|
|
|
|
|
|
|
class FileBackedSessionManager(SessionManager):
|
|
|
|
|
2018-07-05 05:12:03 -05:00
|
|
|
def __init__(self, path, secret, disk_write_delay, skip_paths=[]):
|
2016-07-08 06:25:53 -05:00
|
|
|
self.path = path
|
|
|
|
self.secret = secret
|
Do not dump the session data on the disk on every request.
Session object is updated, everytime a request is being served, and
that was forcing the session object dumped on the dist on every request.
On windows, it was causing issues on slower system on startup. Because -
windows file system locks the file, when it is opened by any
application. And, frequent requests on the pgAdmin main UI rendering
was causing issues, because of that.
In order to resolve the issue, we will not write the session data on
disk for every request, but - only after certain delay (in seconds),
from it was last written. It can be configured using the attribute
'PGADMIN_SESSION_DISK_WRITE_DELAY' in the configuration file,
default vaule for the delay is 10.
(i.e. 10 seconds)
2017-07-25 05:06:46 -05:00
|
|
|
self.disk_write_delay = disk_write_delay
|
2016-03-22 10:05:43 -05:00
|
|
|
if not os.path.exists(self.path):
|
2016-07-08 06:25:53 -05:00
|
|
|
os.makedirs(self.path)
|
2018-07-05 05:12:03 -05:00
|
|
|
self.skip_paths = skip_paths
|
2016-07-08 06:25:53 -05:00
|
|
|
|
|
|
|
def exists(self, sid):
|
|
|
|
fname = os.path.join(self.path, sid)
|
|
|
|
return os.path.exists(fname)
|
|
|
|
|
|
|
|
def remove(self, sid):
|
|
|
|
fname = os.path.join(self.path, sid)
|
|
|
|
if os.path.exists(fname):
|
|
|
|
os.unlink(fname)
|
|
|
|
|
|
|
|
def new_session(self):
|
|
|
|
sid = str(uuid4())
|
|
|
|
fname = os.path.join(self.path, sid)
|
|
|
|
|
|
|
|
while os.path.exists(fname):
|
|
|
|
sid = str(uuid4())
|
|
|
|
fname = os.path.join(self.path, sid)
|
|
|
|
|
2018-07-05 05:12:03 -05:00
|
|
|
# Do not store the session if skip paths
|
|
|
|
for sp in self.skip_paths:
|
|
|
|
if request.path.startswith(sp):
|
|
|
|
return ManagedSession(sid=sid)
|
|
|
|
|
2016-07-08 06:25:53 -05:00
|
|
|
# touch the file
|
2016-07-11 05:59:03 -05:00
|
|
|
with open(fname, 'wb'):
|
2016-07-08 06:25:53 -05:00
|
|
|
pass
|
|
|
|
|
|
|
|
return ManagedSession(sid=sid)
|
|
|
|
|
|
|
|
def get(self, sid, digest):
|
|
|
|
'Retrieve a managed session by session-id, checking the HMAC digest'
|
|
|
|
|
|
|
|
fname = os.path.join(self.path, sid)
|
|
|
|
data = None
|
|
|
|
hmac_digest = None
|
|
|
|
randval = None
|
|
|
|
|
|
|
|
if os.path.exists(fname):
|
|
|
|
try:
|
2016-07-11 05:59:03 -05:00
|
|
|
with open(fname, 'rb') as f:
|
2016-07-08 06:25:53 -05:00
|
|
|
randval, hmac_digest, data = load(f)
|
2018-01-31 07:58:55 -06:00
|
|
|
except Exception:
|
2016-07-08 06:25:53 -05:00
|
|
|
pass
|
|
|
|
|
|
|
|
if not data:
|
|
|
|
return self.new_session()
|
|
|
|
|
|
|
|
# This assumes the file is correct, if you really want to
|
|
|
|
# make sure the session is good from the server side, you
|
|
|
|
# can re-calculate the hmac
|
2016-03-22 10:05:43 -05:00
|
|
|
|
2016-07-08 06:25:53 -05:00
|
|
|
if hmac_digest != digest:
|
|
|
|
return self.new_session()
|
|
|
|
|
|
|
|
return ManagedSession(
|
|
|
|
data, sid=sid, randval=randval, hmac_digest=hmac_digest
|
2016-06-21 08:21:06 -05:00
|
|
|
)
|
2016-03-22 10:05:43 -05:00
|
|
|
|
2016-07-08 06:25:53 -05:00
|
|
|
def put(self, session):
|
Do not dump the session data on the disk on every request.
Session object is updated, everytime a request is being served, and
that was forcing the session object dumped on the dist on every request.
On windows, it was causing issues on slower system on startup. Because -
windows file system locks the file, when it is opened by any
application. And, frequent requests on the pgAdmin main UI rendering
was causing issues, because of that.
In order to resolve the issue, we will not write the session data on
disk for every request, but - only after certain delay (in seconds),
from it was last written. It can be configured using the attribute
'PGADMIN_SESSION_DISK_WRITE_DELAY' in the configuration file,
default vaule for the delay is 10.
(i.e. 10 seconds)
2017-07-25 05:06:46 -05:00
|
|
|
"""Store a managed session"""
|
|
|
|
current_time = time.time()
|
2016-07-08 06:25:53 -05:00
|
|
|
if not session.hmac_digest:
|
|
|
|
session.sign(self.secret)
|
2017-09-18 08:39:43 -05:00
|
|
|
elif not session.force_write:
|
2018-01-31 07:58:55 -06:00
|
|
|
if session.last_write is not None and \
|
|
|
|
(current_time - float(session.last_write)) < \
|
|
|
|
self.disk_write_delay:
|
Do not dump the session data on the disk on every request.
Session object is updated, everytime a request is being served, and
that was forcing the session object dumped on the dist on every request.
On windows, it was causing issues on slower system on startup. Because -
windows file system locks the file, when it is opened by any
application. And, frequent requests on the pgAdmin main UI rendering
was causing issues, because of that.
In order to resolve the issue, we will not write the session data on
disk for every request, but - only after certain delay (in seconds),
from it was last written. It can be configured using the attribute
'PGADMIN_SESSION_DISK_WRITE_DELAY' in the configuration file,
default vaule for the delay is 10.
(i.e. 10 seconds)
2017-07-25 05:06:46 -05:00
|
|
|
return
|
2016-07-08 06:25:53 -05:00
|
|
|
|
Do not dump the session data on the disk on every request.
Session object is updated, everytime a request is being served, and
that was forcing the session object dumped on the dist on every request.
On windows, it was causing issues on slower system on startup. Because -
windows file system locks the file, when it is opened by any
application. And, frequent requests on the pgAdmin main UI rendering
was causing issues, because of that.
In order to resolve the issue, we will not write the session data on
disk for every request, but - only after certain delay (in seconds),
from it was last written. It can be configured using the attribute
'PGADMIN_SESSION_DISK_WRITE_DELAY' in the configuration file,
default vaule for the delay is 10.
(i.e. 10 seconds)
2017-07-25 05:06:46 -05:00
|
|
|
session.last_write = current_time
|
2017-09-18 08:39:43 -05:00
|
|
|
session.force_write = False
|
2018-07-05 05:12:03 -05:00
|
|
|
|
|
|
|
# Do not store the session if skip paths
|
|
|
|
for sp in self.skip_paths:
|
|
|
|
if request.path.startswith(sp):
|
|
|
|
return
|
|
|
|
|
2016-07-08 06:25:53 -05:00
|
|
|
fname = os.path.join(self.path, session.sid)
|
2016-07-11 05:59:03 -05:00
|
|
|
with open(fname, 'wb') as f:
|
2016-07-08 06:25:53 -05:00
|
|
|
dump(
|
|
|
|
(session.randval, session.hmac_digest, dict(session)),
|
|
|
|
f
|
|
|
|
)
|
2016-03-22 10:05:43 -05:00
|
|
|
|
|
|
|
|
2016-07-08 06:25:53 -05:00
|
|
|
class ManagedSessionInterface(SessionInterface):
|
2018-10-09 05:34:13 -05:00
|
|
|
def __init__(self, manager):
|
2016-07-08 06:25:53 -05:00
|
|
|
self.manager = manager
|
2016-03-22 10:05:43 -05:00
|
|
|
|
|
|
|
def open_session(self, app, request):
|
2016-07-08 06:25:53 -05:00
|
|
|
cookie_val = request.cookies.get(app.session_cookie_name)
|
|
|
|
|
2018-01-31 07:58:55 -06:00
|
|
|
if not cookie_val or '!' not in cookie_val:
|
2016-07-08 06:25:53 -05:00
|
|
|
return self.manager.new_session()
|
|
|
|
|
|
|
|
sid, digest = cookie_val.split('!', 1)
|
|
|
|
|
|
|
|
if self.manager.exists(sid):
|
|
|
|
return self.manager.get(sid, digest)
|
|
|
|
|
|
|
|
return self.manager.new_session()
|
2016-03-22 10:05:43 -05:00
|
|
|
|
|
|
|
def save_session(self, app, session, response):
|
|
|
|
domain = self.get_cookie_domain(app)
|
|
|
|
if not session:
|
2016-07-08 06:25:53 -05:00
|
|
|
self.manager.remove(session.sid)
|
2016-03-22 10:05:43 -05:00
|
|
|
if session.modified:
|
2016-07-08 06:25:53 -05:00
|
|
|
response.delete_cookie(app.session_cookie_name, domain=domain)
|
|
|
|
return
|
|
|
|
|
|
|
|
if not session.modified:
|
|
|
|
# No need to save an unaltered session
|
|
|
|
# TODO: put logic here to test if the cookie is older than N days,
|
|
|
|
# if so, update the expiration date
|
2016-03-22 10:05:43 -05:00
|
|
|
return
|
2016-07-08 06:25:53 -05:00
|
|
|
|
|
|
|
self.manager.put(session)
|
|
|
|
session.modified = False
|
|
|
|
|
2016-03-22 10:05:43 -05:00
|
|
|
cookie_exp = self.get_expiration_time(app, session)
|
2018-01-31 07:58:55 -06:00
|
|
|
response.set_cookie(
|
|
|
|
app.session_cookie_name,
|
|
|
|
'%s!%s' % (session.sid, session.hmac_digest),
|
2018-03-15 13:54:14 -05:00
|
|
|
expires=cookie_exp, httponly=True, domain=domain
|
2018-01-31 07:58:55 -06:00
|
|
|
)
|
2016-07-08 06:25:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
def create_session_interface(app, skip_paths=[]):
|
|
|
|
return ManagedSessionInterface(
|
|
|
|
CachingSessionManager(
|
|
|
|
FileBackedSessionManager(
|
|
|
|
app.config['SESSION_DB_PATH'],
|
Do not dump the session data on the disk on every request.
Session object is updated, everytime a request is being served, and
that was forcing the session object dumped on the dist on every request.
On windows, it was causing issues on slower system on startup. Because -
windows file system locks the file, when it is opened by any
application. And, frequent requests on the pgAdmin main UI rendering
was causing issues, because of that.
In order to resolve the issue, we will not write the session data on
disk for every request, but - only after certain delay (in seconds),
from it was last written. It can be configured using the attribute
'PGADMIN_SESSION_DISK_WRITE_DELAY' in the configuration file,
default vaule for the delay is 10.
(i.e. 10 seconds)
2017-07-25 05:06:46 -05:00
|
|
|
app.config['SECRET_KEY'],
|
2018-07-05 05:12:03 -05:00
|
|
|
app.config.get('PGADMIN_SESSION_DISK_WRITE_DELAY', 10),
|
|
|
|
skip_paths
|
2016-07-08 06:25:53 -05:00
|
|
|
),
|
2018-07-05 05:12:03 -05:00
|
|
|
1000,
|
|
|
|
skip_paths
|
2018-10-09 05:34:13 -05:00
|
|
|
))
|
2018-02-01 07:29:18 -06:00
|
|
|
|
|
|
|
|
|
|
|
def pga_unauthorised():
|
|
|
|
|
|
|
|
lm = current_app.login_manager
|
|
|
|
login_message = None
|
|
|
|
|
|
|
|
if lm.login_message:
|
|
|
|
if lm.localize_callback is not None:
|
|
|
|
login_message = lm.localize_callback(lm.login_message)
|
|
|
|
else:
|
|
|
|
login_message = lm.login_message
|
|
|
|
|
|
|
|
if not lm.login_view or request.is_xhr:
|
|
|
|
# Only 401 is not enough to distinguish pgAdmin login is required.
|
|
|
|
# There are other cases when we return 401. For eg. wrong password
|
|
|
|
# supplied while connecting to server.
|
|
|
|
# So send additional 'info' message.
|
|
|
|
return make_json_response(
|
|
|
|
status=401,
|
|
|
|
success=0,
|
|
|
|
errormsg=login_message,
|
|
|
|
info='PGADMIN_LOGIN_REQUIRED'
|
|
|
|
)
|
|
|
|
|
|
|
|
if login_message:
|
|
|
|
flash(login_message, category=lm.login_message_category)
|
|
|
|
|
|
|
|
return redirect(login_url(lm.login_view, request.url))
|
2018-10-09 05:34:13 -05:00
|
|
|
|
|
|
|
|
|
|
|
def cleanup_session_files():
|
|
|
|
"""
|
|
|
|
This function will iterate through session directory and check the last
|
|
|
|
modified time, if it older than (session expiration time + 1) days then
|
|
|
|
delete that file.
|
|
|
|
"""
|
|
|
|
global LAST_CHECK_SESSION_FILES
|
|
|
|
if LAST_CHECK_SESSION_FILES is None:
|
|
|
|
LAST_CHECK_SESSION_FILES = datetime.datetime.now()
|
|
|
|
else:
|
|
|
|
if datetime.datetime.now() >= LAST_CHECK_SESSION_FILES + \
|
|
|
|
datetime.timedelta(hours=config.CHECK_SESSION_FILES_INTERVAL):
|
|
|
|
|
|
|
|
for root, dirs, files in os.walk(
|
|
|
|
current_app.config['SESSION_DB_PATH']):
|
|
|
|
for file_name in files:
|
|
|
|
absolute_file_name = os.path.join(root, file_name)
|
|
|
|
st = os.stat(absolute_file_name)
|
|
|
|
|
|
|
|
# Get the last modified time of the session file
|
|
|
|
last_modified_time = \
|
|
|
|
datetime.datetime.fromtimestamp(st.st_mtime)
|
|
|
|
|
|
|
|
# Calculate session file expiry time.
|
|
|
|
file_expiration_time = \
|
|
|
|
last_modified_time + \
|
|
|
|
current_app.permanent_session_lifetime + \
|
|
|
|
datetime.timedelta(days=1)
|
|
|
|
|
|
|
|
if file_expiration_time <= datetime.datetime.now():
|
|
|
|
if os.path.exists(absolute_file_name):
|
|
|
|
os.unlink(absolute_file_name)
|
|
|
|
|
|
|
|
LAST_CHECK_SESSION_FILES = datetime.datetime.now()
|