def get_user(token, abort_if_no_token=True): """ return the current authenticated User or None """ if hasattr(g, 'user'): return g.user else: if not token: #a token is mandatory for non public jormungandr if not current_app.config.get('PUBLIC', False): if abort_if_no_token: flask_restful.abort( 401, message= 'no token. You can get one at http://www.navitia.io or contact your support if you’re using the opensource version of Navitia https://github.com/CanalTP/navitia' ) else: return None else: # for public one we allow unknown user g.user = User(login="******") g.user.id = 0 else: g.user = cache_get_user(token) return g.user
def get_user(): """ return the current authentificated User or None """ if hasattr(g, 'user'): return g.user else: g.user = User.get_from_token(get_token(), datetime.datetime.now()) return g.user
def uncached_get_user(token): logging.getLogger(__name__).debug('Get User from token (uncached)') if not can_connect_to_database(): logging.getLogger(__name__).debug('Cannot connect to database, we set User to None') return None try: user = User.get_from_token(token, datetime.datetime.now()) except Exception as e: logging.getLogger(__name__).error('No access to table User (error: {})'.format(e)) g.can_connect_to_database = False return None return user
def get_user(abort_if_no_token=True): """ return the current authenticated User or None """ if hasattr(g, 'user'): return g.user else: token = get_token() if not token: #a token is mandatory for non public jormungandr if not current_app.config.get('PUBLIC', False): if abort_if_no_token: flask_restful.abort(401) else: return None else: # for public one we allow unknown user g.user = User(login="******") g.user.id = 0 else: g.user = User.get_from_token(token, datetime.datetime.now()) logging.debug('user %s', g.user) return g.user
def get_user(token, abort_if_no_token=True): """ return the current authenticated User or None """ if hasattr(g, 'user'): return g.user else: if not token: #a token is mandatory for non public jormungandr if not current_app.config.get('PUBLIC', False): if abort_if_no_token: flask_restful.abort(401, message='no token') else: return None else: # for public one we allow unknown user g.user = User(login="******") g.user.id = 0 else: g.user = cache_get_user(token) return g.user
def cache_get_user(token): """ We allow this method to be cached even if it depends on the current time because we assume the cache time is small and the error can be tolerated. """ return User.get_from_token(token, datetime.datetime.now())