Changeset - 8114623895cc
[Not reviewed]
default
0 1 0
Mads Kiilerich - 6 years ago 2020-02-05 13:22:53
mads@kiilerich.com
Grafted from: 86e54ab57c58
auth: make crowd logging simpler

There is no point in creating dicts and then logging them as json. Also, json
can't handle py3 bytes and it would fail on py3. (ext_json could perhaps handle
bytes, but it seems better to keep it simple and explicit.)

If the default repr isn't good enough, it would be better to use pprint. But
repr is good enough.
1 file changed with 6 insertions and 8 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/auth_modules/auth_crowd.py
Show inline comments
 
@@ -28,13 +28,13 @@ Original author and date, and relevant c
 

	
 
import base64
 
import logging
 
import urllib2
 

	
 
from kallithea.lib import auth_modules, ext_json
 
from kallithea.lib.compat import formatted_json, hybrid_property
 
from kallithea.lib.compat import hybrid_property
 
from kallithea.lib.utils2 import ascii_bytes, ascii_str, safe_bytes
 

	
 

	
 
log = logging.getLogger(__name__)
 

	
 

	
 
@@ -84,15 +84,13 @@ class CrowdServer(object):
 
                    "Accept": "application/json"}
 
        if self.user and self.passwd:
 
            authstring = ascii_str(base64.b64encode(safe_bytes("%s:%s" % (self.user, self.passwd))))
 
            _headers["Authorization"] = "Basic %s" % authstring
 
        if headers:
 
            _headers.update(headers)
 
        log.debug("Sent crowd: \n%s",
 
                  formatted_json({"url": url, "body": body,
 
                                           "headers": _headers}))
 
        log.debug("Sent to crowd at %s:\nHeaders: %s\nBody:\n%s", url, _headers, body)
 
        req = urllib2.Request(url, body, _headers)
 
        if method:
 
            req.get_method = lambda: method
 

	
 
        global msg
 
        msg = ""
 
@@ -207,27 +205,27 @@ class KallitheaAuthPlugin(auth_modules.K
 
        This is later validated for correctness
 
        """
 
        if not username or not password:
 
            log.debug('Empty username or password skipping...')
 
            return None
 

	
 
        log.debug("Crowd settings: \n%s", formatted_json(settings))
 
        log.debug("Crowd settings: %s", settings)
 
        server = CrowdServer(**settings)
 
        server.set_credentials(settings["app_name"], settings["app_password"])
 
        crowd_user = server.user_auth(username, password)
 
        log.debug("Crowd returned: \n%s", formatted_json(crowd_user))
 
        log.debug("Crowd returned: %s", crowd_user)
 
        if not crowd_user["status"]:
 
            log.error('Crowd authentication as %s returned no status', username)
 
            return None
 

	
 
        if not crowd_user.get('active'):
 
            log.error('Crowd authentication as %s returned in-active user', username)
 
            return None
 

	
 
        res = server.user_groups(crowd_user["name"])
 
        log.debug("Crowd groups: \n%s", formatted_json(res))
 
        log.debug("Crowd groups: %s", res)
 
        crowd_user["groups"] = [x["name"] for x in res["groups"]]
 

	
 
        # old attrs fetched from Kallithea database
 
        admin = getattr(userobj, 'admin', False)
 
        email = getattr(userobj, 'email', '')
 
        firstname = getattr(userobj, 'firstname', '')
 
@@ -244,12 +242,12 @@ class KallitheaAuthPlugin(auth_modules.K
 
        }
 

	
 
        # set an admin if we're in admin_groups of crowd
 
        for group in settings["admin_groups"].split(","):
 
            if group in user_data["groups"]:
 
                user_data["admin"] = True
 
        log.debug("Final crowd user object: \n%s", formatted_json(user_data))
 
        log.debug("Final crowd user object: %s", user_data)
 
        log.info('user %s authenticated correctly', user_data['username'])
 
        return user_data
 

	
 
    def get_managed_fields(self):
 
        return ['username', 'firstname', 'lastname', 'email', 'password']
0 comments (0 inline, 0 general)