'Perform ZAP authentication'

def handle_zap_message(self, msg):

    if (len(msg) < 6):
        self.log.error('Invalid    ZAP    message,    not    enough    frames:    %r', msg)
        if (len(msg) < 2):
            self.log.error('Not    enough    information    to    reply')
        else:
            self._send_zap_reply(msg[1], '400', 'Not    enough    frames')
        return
    (version, request_id, domain, address, identity, mechanism) = msg[:6]
    credentials = msg[6:]
    domain = u(domain, self.encoding, 'replace')
    address = u(address, self.encoding, 'replace')
    if (version != VERSION):
        self.log.error('Invalid    ZAP    version:    %r', msg)
        self._send_zap_reply(request_id, '400', 'Invalid    version')
        return
    self.log.debug('version:    %r,    request_id:    %r,    domain:    %r,    address:    %r,    identity:    %r,    mechanism:    %r', version, request_id, domain, address, identity, mechanism)
    allowed = False
    denied = False
    reason = 'NO    ACCESS'
    if self.whitelist:
        if (address in self.whitelist):
            allowed = True
            self.log.debug('PASSED    (whitelist)    address=%s', address)
        else:
            denied = True
            reason = 'Address    not    in    whitelist'
            self.log.debug('DENIED    (not    in    whitelist)    address=%s', address)
    elif self.blacklist:
        if (address in self.blacklist):
            denied = True
            reason = 'Address    is    blacklisted'
            self.log.debug('DENIED    (blacklist)    address=%s', address)
        else:
            allowed = True
            self.log.debug('PASSED    (not    in    blacklist)    address=%s', address)
    username = u('anonymous')
    if (not denied):
        if ((mechanism == 'NULL') and (not allowed)):
            self.log.debug('ALLOWED    (NULL)')
            allowed = True
        elif (mechanism == 'PLAIN'):
            if (len(credentials) != 2):
                self.log.error('Invalid    PLAIN    credentials:    %r', credentials)
                self._send_zap_reply(request_id, '400', 'Invalid    credentials')
                return
            (username, password) = [u(c, self.encoding, 'replace') for c in credentials]
            (allowed, reason) = self._authenticate_plain(domain, username, password)
        elif (mechanism == 'CURVE'):
            if (len(credentials) != 1):
                self.log.error('Invalid    CURVE    credentials:    %r', credentials)
                self._send_zap_reply(request_id, '400', 'Invalid    credentials')
                return
            key = credentials[0]
            (allowed, reason) = self._authenticate_curve(domain, key)
            if allowed:
                username = self.curve_user_id(key)
        elif (mechanism == 'GSSAPI'):
            if (len(credentials) != 1):
                self.log.error('Invalid    GSSAPI    credentials:    %r', credentials)
                self._send_zap_reply(request_id, '400', 'Invalid    credentials')
                return
            principal = username = credentials[0]
            (allowed, reason) = self._authenticate_gssapi(domain, principal)
    if allowed:
        self._send_zap_reply(request_id, '200', 'OK', username)
    else:
        self._send_zap_reply(request_id, '400', reason)
