2014-03-17 16:01:13 +00:00
|
|
|
import logging
|
|
|
|
|
2014-02-25 20:07:24 +00:00
|
|
|
from flask import _request_ctx_stack
|
2014-03-17 16:01:13 +00:00
|
|
|
from data import model
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-02-25 20:07:24 +00:00
|
|
|
|
|
|
|
def get_authenticated_user():
|
2014-03-17 16:01:13 +00:00
|
|
|
user = getattr(_request_ctx_stack.top, 'authenticated_user', None)
|
|
|
|
if not user:
|
2014-11-20 23:44:36 +00:00
|
|
|
user_uuid = getattr(_request_ctx_stack.top, 'authenticated_user_uuid', None)
|
|
|
|
if not user_uuid:
|
|
|
|
logger.debug('No authenticated user or deferred user uuid.')
|
2014-03-17 16:01:13 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
logger.debug('Loading deferred authenticated user.')
|
2014-11-20 23:44:36 +00:00
|
|
|
loaded = model.get_user_by_uuid(user_uuid)
|
2015-05-11 21:13:42 +00:00
|
|
|
if not loaded.enabled:
|
|
|
|
return None
|
|
|
|
|
2014-03-17 16:01:13 +00:00
|
|
|
set_authenticated_user(loaded)
|
|
|
|
user = loaded
|
|
|
|
|
2014-04-01 17:00:26 +00:00
|
|
|
if user:
|
|
|
|
logger.debug('Returning authenticated user: %s', user.username)
|
2014-03-17 16:01:13 +00:00
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
def set_authenticated_user(user_or_robot):
|
2015-05-11 21:13:42 +00:00
|
|
|
if not user_or_robot.enabled:
|
|
|
|
raise Exception('Attempt to authenticate a disabled user/robot: %s' % user_or_robot.username)
|
|
|
|
|
2014-03-17 16:01:13 +00:00
|
|
|
ctx = _request_ctx_stack.top
|
|
|
|
ctx.authenticated_user = user_or_robot
|
|
|
|
|
|
|
|
|
2015-02-23 20:07:02 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2014-11-20 23:44:36 +00:00
|
|
|
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)
|
2014-03-17 16:01:13 +00:00
|
|
|
ctx = _request_ctx_stack.top
|
2014-11-20 23:44:36 +00:00
|
|
|
ctx.authenticated_user_uuid = user_or_robot_db_uuid
|
2014-03-17 16:01:13 +00:00
|
|
|
|
2014-02-25 20:07:24 +00:00
|
|
|
|
2014-03-18 20:45:18 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2014-02-25 20:07:24 +00:00
|
|
|
def get_validated_token():
|
|
|
|
return getattr(_request_ctx_stack.top, 'validated_token', None)
|
2014-03-17 16:01:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def set_validated_token(token):
|
|
|
|
ctx = _request_ctx_stack.top
|
|
|
|
ctx.validated_token = token
|