Changeset - d052078e0a16
[Not reviewed]
default
0 1 0
Søren Løvborg - 10 years ago 2015-07-14 13:59:59
kwi@kwi.dk
BaseController: refactor API key authentication

Untangle API key authentication. Creating an AuthUser from an API key
can leave the AuthUser authenticated or not, depending on key validity
and Kallithea configuration; but either way, _determine_auth_user will
not change this fact, and we can return early.
1 file changed with 5 insertions and 3 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/base.py
Show inline comments
 
@@ -325,53 +325,55 @@ class BaseController(WSGIController):
 

	
 
        c.instance_id = config.get('instance_id')
 
        c.issues_url = config.get('bugtracker', url('issues_url'))
 
        # END CONFIG VARS
 

	
 
        c.repo_name = get_repo_slug(request)  # can be empty
 
        c.backends = BACKENDS.keys()
 
        c.unread_notifications = NotificationModel()\
 
                        .get_unread_cnt_for_user(c.authuser.user_id)
 

	
 
        self.cut_off_limit = safe_int(config.get('cut_off_limit'))
 

	
 
        c.my_pr_count = PullRequestModel().get_pullrequest_cnt_for_user(c.authuser.user_id)
 

	
 
        self.sa = meta.Session
 
        self.scm_model = ScmModel(self.sa)
 

	
 
    @staticmethod
 
    def _determine_auth_user(api_key, session_authuser):
 
        """
 
        Create an `AuthUser` object given the IP address of the request, the
 
        API key (if any), and the authuser from the session.
 
        """
 

	
 
        # Authenticate by API key
 
        if api_key:
 
            # when using API_KEY we are sure user exists.
 
            auth_user = AuthUser(api_key=api_key)
 
            authenticated = False
 
        else:
 
            return AuthUser(api_key=api_key)
 

	
 
        # Authenticate by session cookie
 
        if True:
 
            cookie_store = CookieStoreWrapper(session_authuser)
 
            user_id = cookie_store.get('user_id')
 
            try:
 
                auth_user = AuthUser(user_id=user_id)
 
            except UserCreationError as e:
 
                # container auth or other auth functions that create users on
 
                # the fly can throw UserCreationError to signal issues with
 
                # user creation. Explanation should be provided in the
 
                # exception object.
 
                from kallithea.lib import helpers as h
 
                h.flash(e, 'error')
 
                auth_user = AuthUser()
 

	
 
            authenticated = cookie_store.get('is_authenticated')
 

	
 
        if not auth_user.is_authenticated and auth_user.user_id is not None:
 
            # user is not authenticated and not empty
 
            auth_user.set_authenticated(authenticated)
 

	
 
        return auth_user
 

	
 
    def __call__(self, environ, start_response):
 
        """Invoke the Controller"""
 

	
0 comments (0 inline, 0 general)