예제 #1
0
파일: config.py 프로젝트: hopil/LinOTP
    def __setitem__(self, key, val, typ=None, des=None):
        '''
        implemtation of the assignement operator == internal function

        :param key: key of the dict
        :type  key: string
        :param val: any value, which is put in the dict
        :type  val: any type
        :param typ: used in the database to control if the data is encrypted
        :type  typ: None,string,password
        :param des: literal, which describes the data
        :type  des: string
        '''

        if typ == 'password':

            # # in case we have a password type, we have to put
            # #- in the config only the encrypted pass and
            # #- add the config enclinotp.* with the clear password

            res = self.parent.__setitem__(key, encryptPassword(val))
            res = self.parent.__setitem__('enc' + key, val)
            self.glo.setConfig({key :encryptPassword(val)})
            self.glo.setConfig({'enc' + key : val})

        else:
            # # update this config and sync with global dict and db
            nVal = _expandHere(val)
            res = self.parent.__setitem__(key, nVal)
            self.glo.setConfig({key:nVal})

        _storeConfigDB(key, val, typ, des)
        _storeConfigDB('linotp.Config', datetime.now())
        return res
예제 #2
0
    def __setitem__(self, key, val, typ=None, des=None):
        '''
        implemtation of the assignement operator == internal function

        :param key: key of the dict
        :type  key: string
        :param val: any value, which is put in the dict
        :type  val: any type
        :param typ: used in the database to control if the data is encrypted
        :type  typ: None,string,password
        :param des: literal, which describes the data
        :type  des: string
        '''

        if typ == 'password':

            # # in case we have a password type, we have to put
            # #- in the config only the encrypted pass and
            # #- add the config enclinotp.* with the clear password

            res = self.parent.__setitem__(key, encryptPassword(val))
            res = self.parent.__setitem__('enc' + key, val)
            self.glo.setConfig({key: encryptPassword(val)})
            self.glo.setConfig({'enc' + key: val})

        else:
            # # update this config and sync with global dict and db
            nVal = _expandHere(val)
            res = self.parent.__setitem__(key, nVal)
            self.glo.setConfig({key: nVal})

        _storeConfigDB(key, val, typ, des)
        _storeConfigDB('linotp.Config', datetime.now())
        return res
예제 #3
0
파일: config.py 프로젝트: super-rain/LinOTP
    def __setitem__(self, key, val, typ=None, des=None):
        '''
        implemtation of the assignement operator == internal function

        :param key: key of the dict
        :type  key: string
        :param val: any value, which is put in the dict
        :type  val: any type
        :param typ: used in the database to control if the data is encrypted
        :type  typ: None,string,password
        :param des: literal, which describes the data
        :type  des: string
        '''

        now = datetime.now()

        self._check_type(key, val)

        if typ == 'password':

            # in case we have a password type, we have to put
            # - in the config only the encrypted pass and
            # - add the config enclinotp.* with the clear password

            utf_val = val.encode('utf-8')

            # store in request local config dict

            res = self.parent.__setitem__(key, encryptPassword(utf_val))
            res = self.parent.__setitem__('enc' + key, val)

            # store in global config

            self.glo.setConfig({key: encryptPassword(utf_val)})
            self.glo.setConfig({'enc' + key: val})

        else:

            # update this config and sync with global dict and db

            nVal = _expandHere(val)
            res = self.parent.__setitem__(key, nVal)
            self.glo.setConfig({key: nVal})

        # ----------------------------------------------------------------- --

        # finally store the entry in the database and
        # syncronize as well the global timestamp

        self.glo.setConfig({'linotp.Config': unicode(now)})

        _storeConfigDB(key, val, typ, des)
        _storeConfigDB('linotp.Config', now)

        return res
예제 #4
0
파일: config.py 프로젝트: jimmytuc/LinOTP
def _storeConfigDB(key, val, typ=None, desc=None):
    """
    insert or update the entry with  key, value, type and
    description in the config DB

    """
    value = val

    log_value = val
    if typ == 'password':
        log_value = "XXXXXXX"
    log.debug('key %r : value %r', key, log_value)

    if (not key.startswith("linotp.")):
        key = "linotp." + key

    if typ and typ == 'password':
        value = encryptPassword(val)
        en = decryptPassword(value)
        if (en != val):
            raise Exception("Error during encoding password type!")

    if type(value) not in [str, unicode]:
        return _storeConfigEntryDB(key, value, typ=typ, desc=desc)

    # for strings or unicode, we support continued entries
    # check if we have to split the value
    number_of_chunks = (len(value) / MAX_VALUE_LEN)
    if number_of_chunks == 0:
        return _storeConfigEntryDB(key, value, typ=typ, desc=desc)

    # the continuous type is a split over multiple entries:
    # * every entry will have an enumerated key but the first one which has the
    #   original one.
    # * For all the type is 'C', but the last one which contains the original
    #   type.
    # * For description all entries contains the enumeration, but the last the
    #   original description

    for i in range(number_of_chunks + 1):
        # iterate through the chunks, the last one might be empty though
        cont_value = value[i * MAX_VALUE_LEN: (i + 1) * MAX_VALUE_LEN]

        cont_typ = "C"
        cont_desc = "%d:%d" % (i, number_of_chunks)
        cont_key = "%s__[%d:%d]" % (key, i, number_of_chunks)

        if i == 0:
            # first one will contain the correct key, but type is continuous
            cont_key = key
        elif i == number_of_chunks:
            # the last one will contain the type and the description
            cont_typ = typ
            cont_desc = desc

        res = _storeConfigEntryDB(cont_key, cont_value,
                                  typ=cont_typ,
                                  desc=cont_desc)

    return res
예제 #5
0
파일: config.py 프로젝트: hopil/LinOTP
def _storeConfigDB(key, val, typ=None, desc=None):
    value = val
    log.debug('storeConfigDB: key %r : value %r' % (key, value))

    if (not key.startswith("linotp.")):
        key = "linotp." + key

    confEntries = Session.query(Config).filter(Config.Key == unicode(key))
    theConf = None

    if typ is not None and typ == 'password':
        value = encryptPassword(val)
        en = decryptPassword(value)
        if (en != val):
            raise Exception("StoreConfig: Error during encoding password type!")

    # # update
    if confEntries.count() == 1:
        theConf = confEntries[0]
        theConf.Value = unicode(value)
        if (typ is not None):
            theConf.Type = unicode(typ)
        if (desc is not None):
            theConf.Description = unicode(desc)

    # # insert
    elif confEntries.count() == 0:
        theConf = Config(
                        Key=unicode(key),
                        Value=unicode(value),
                        Type=unicode(typ),
                        Description=unicode(desc)
                        )
    if theConf is not None:
        Session.add(theConf)

    return 101
예제 #6
0
파일: config.py 프로젝트: I85YL64/LinOTP
def _storeConfigDB(key, val, typ=None, desc=None):
    value = val
    log.debug('storeConfigDB: key %r : value %r' % (key, value))

    if (not key.startswith("linotp.")):
        key = "linotp." + key

    confEntries = Session.query(Config).filter(Config.Key == unicode(key))
    theConf = None

    if typ is not None and typ == 'password':
        value = encryptPassword(val)
        en = decryptPassword(value)
        if (en != val):
            raise Exception(
                "StoreConfig: Error during encoding password type!")

    # # update
    if confEntries.count() == 1:
        theConf = confEntries[0]
        theConf.Value = unicode(value)
        if (typ is not None):
            theConf.Type = unicode(typ)
        if (desc is not None):
            theConf.Description = unicode(desc)

    # # insert
    elif confEntries.count() == 0:
        theConf = Config(Key=unicode(key),
                         Value=unicode(value),
                         Type=unicode(typ),
                         Description=unicode(desc))
    if theConf is not None:
        Session.add(theConf)

    return 101
예제 #7
0
def _storeConfigDB(key, val, typ=None, desc=None):
    """
    insert or update the entry with  key, value, type and
    description in the config DB

    """
    value = val

    log_value = val
    if typ == 'password':
        log_value = "XXXXXXX"
    log.debug('key %r : value %r', key, log_value)

    if (not key.startswith("linotp.")):
        key = "linotp." + key

    if typ and typ == 'password':
        value = encryptPassword(val)
        en = decryptPassword(value)
        if (en != val):
            raise Exception("Error during encoding password type!")

    if type(value) not in [str, unicode]:
        return _storeConfigEntryDB(key, value, typ=typ, desc=desc)

    # for strings or unicode, we support continued entries
    # check if we have to split the value

    # the continuous type is a split over multiple entries:
    # * every entry will have an enumerated key but the first one which has the
    #   original one.
    # * For all the type is 'C', but the last one which contains the original
    #   type.
    # * For description all entries contains the enumeration, but the last the
    #   original description

    #
    # we check the split algorithm depending on the value data -
    # in case of only ascii, we can use the much faster simple algorithm
    # in case of unicode characters we have to take the much slower one
    #

    chunks = []
    if len(value) < len(value.encode('utf-8')):
        text_slice = utf8_slice
    else:
        text_slice = simple_slice

    # the number of chunks is oriented toward the max length defined
    # by utf8 in bytes + the clipping of 6 bytes each. But as this could
    # vary, we could not calculate the number of chunks and thus benefit
    # from the iterator

    for cont_value in text_slice(value, MAX_VALUE_LEN):
        chunks.append(cont_value)

    number_of_chunks = len(chunks)

    # special simple case: either empty value or single entry
    if number_of_chunks == 1:
        return _storeConfigEntryDB(key, value, typ=typ, desc=desc)

    for i, cont_value in enumerate(chunks):

        cont_typ = "C"
        cont_desc = "%d:%d" % (i, number_of_chunks)
        cont_key = "%s__[%d:%d]" % (key, i, number_of_chunks)

        if i == 0:
            # first one will contain the correct key, but type is continuous
            cont_key = key
        elif i == number_of_chunks:
            # the last one will contain the type and the description
            cont_typ = typ
            cont_desc = desc

        res = _storeConfigEntryDB(cont_key, cont_value,
                                  typ=cont_typ,
                                  desc=cont_desc)

    return res