예제 #1
0
class ClientInfo(Registry):
    """
    A UserInfo holds client context information.
    """
    _class_id = 'ClientInfo'

    def __reginit__(self, username, **tokens):
        """
        Create a new ClientInfo object, and set the username.

        @param username: the client's username
        @type username: str
        @param **kw: optional context information
        @type **kw: dict
        """
        self.tokens = ACLTags(tokens)
        self.username = username
        self.load()

    def load(self):
        pass

    def save(self):
        pass

    def get_token(self, token, default=None):
        return self.tokens.get(token, default)

    def set_tokens(self, **tokens):
        """
        Set the authentication or context token of the user.

        @param **tokens: the tokens to be set.
        @type **tokens: dict
        @return: None
        """
        for token, value in tokens.items():
            # XXX: how to encrypt the tokens ? encrypt them all ?
            self.tokens[token] = str(value)

    def get_tags(self):
        tags = ACLTags(self.tokens)
        tags['username'] = self.username
        return tags

    def auth_token_order(self):
        return ('password', )

    def authenticate(self, **tokens):
        """
        Authenticate the client with C{**tokens}.

        @param **tokens: authentication tokens (password, key, ...)
        @type **tokens: dict
        @return: True if the client was authenticated, False otherwise.
        @rtype: bool
        """
        for token in self.auth_token_order():
            if token in tokens and tokens[token] is not None:
                if self.get_token(token) == tokens[token]:
                    return True
        return False

    def add_pubkey(self, pubkey, nbkey):
        ring = self.get_token('pubkey', self.get_token('pkey', ''))
        if pubkey in ring:
            return False

        ring = [k.strip() for k in ring.split('\n') if len(k.strip())]

        try:
            nbkey = int(nbkey)
            if len(ring) >= nbkey:
                return False
        except ValueError:
            # auto_add_key is not an integer, so an infinitie
            # number of keys is allowed
            pass

        ip_addr = self.get_token('ip_addr', 'unknown')
        ring = '\n'.join(ring +
                         ['%s %s@%s' % (pubkey, self.username, ip_addr)])

        self.set_tokens(pubkey=ring)
        self.save()
        return True
예제 #2
0
class ClientInfo(Registry):
    """
    A UserInfo holds client context information.
    """
    _class_id = 'ClientInfo'


    def __reginit__(self, username, **tokens):
        """
        Create a new ClientInfo object, and set the username.

        @param username: the client's username
        @type username: str
        @param **kw: optional context information
        @type **kw: dict
        """
        self.tokens = ACLTags(tokens)
        self.username = username
        self.load()


    def load(self):
        pass


    def save(self):
        pass


    def get_token(self, token, default=None):
        return self.tokens.get(token, default)


    def set_tokens(self, **tokens):
        """
        Set the authentication or context token of the user.

        @param **tokens: the tokens to be set.
        @type **tokens: dict
        @return: None
        """
        for token, value in tokens.items():
            # XXX: how to encrypt the tokens ? encrypt them all ?
            self.tokens[token] = str(value)


    def get_tags(self):
        tags = ACLTags(self.tokens)
        tags['username'] = self.username
        return tags


    def auth_token_order(self):
        return ('password',)


    def authenticate(self, **tokens):
        """
        Authenticate the client with C{**tokens}.

        @param **tokens: authentication tokens (password, key, ...)
        @type **tokens: dict
        @return: True if the client was authenticated, False otherwise.
        @rtype: bool
        """
        for token in self.auth_token_order():
            if token in tokens and tokens[token] is not None:
                if self.get_token(token) == tokens[token]:
                    return True
        return False

    def add_pubkey(self, pubkey, nbkey):
        ring = self.get_token('pubkey', self.get_token('pkey', ''))
        if pubkey in ring:
            return False

        ring = [ k.strip() for k in ring.split('\n') if len(k.strip()) ]

        try:
            nbkey = int(nbkey)
            if len(ring) >= nbkey:
                return False
        except ValueError:
            # auto_add_key is not an integer, so an infinitie
            # number of keys is allowed
            pass

        ip_addr = self.get_token('ip_addr', 'unknown')
        ring = '\n'.join(ring + [ '%s %s@%s' % (pubkey, self.username, ip_addr) ])

        self.set_tokens(pubkey=ring)
        self.save()
        return True