import logging from flask import _request_ctx_stack from data import model logger = logging.getLogger(__name__) def get_authenticated_user(): user = getattr(_request_ctx_stack.top, 'authenticated_user', None) if not user: user_uuid = getattr(_request_ctx_stack.top, 'authenticated_user_uuid', None) if not user_uuid: logger.debug('No authenticated user or deferred user uuid.') return None logger.debug('Loading deferred authenticated user.') loaded = model.get_user_by_uuid(user_uuid) set_authenticated_user(loaded) user = loaded if user: logger.debug('Returning authenticated user: %s', user.username) return user def set_authenticated_user(user_or_robot): ctx = _request_ctx_stack.top ctx.authenticated_user = user_or_robot def get_grant_user_context(): return getattr(_request_ctx_stack.top, 'grant_user_context', None) def set_grant_user_context(username_or_robotname): ctx = _request_ctx_stack.top ctx.grant_user_context = username_or_robotname def set_authenticated_user_deferred(user_or_robot_db_uuid): logger.debug('Deferring loading of authenticated user object with uuid: %s', user_or_robot_db_uuid) ctx = _request_ctx_stack.top ctx.authenticated_user_uuid = user_or_robot_db_uuid def get_validated_oauth_token(): return getattr(_request_ctx_stack.top, 'validated_oauth_token', None) def set_validated_oauth_token(token): ctx = _request_ctx_stack.top ctx.validated_oauth_token = token def get_validated_token(): return getattr(_request_ctx_stack.top, 'validated_token', None) def set_validated_token(token): ctx = _request_ctx_stack.top ctx.validated_token = token