def convert_to_uri(value, ns_obj=None, strip_iri=False):
    ''' converts a prefixed rdf ns equivalent value to its uri form. 
        If not found returns the value as is '''
    
    if ns_obj is None:
        from rdfframework import get_framework
        ns_obj=get_framework().ns_obj
    for _prefix, _ns_uri in ns_obj.items():
        if str(value).startswith(_prefix + "_") or \
                str(value).startswith("<%s_" % _prefix):
            if strip_iri:
                return value.replace("%s_" % _prefix, _ns_uri).replace(\
                        "<","").replace(">","")
            else:
                return value.replace("%s_" % _prefix, _ns_uri)
        if str(value).startswith(_prefix + ":") or \
                str(value).startswith("<%s:" % _prefix):
            if strip_iri:
                return value.replace("%s:" % _prefix, _ns_uri).replace(\
                        "<","").replace(">","")
            else:
                return value.replace("%s:" % _prefix, _ns_uri)
    if str(value).lower() == "none":
        return ""
    else:
        return value
Ejemplo n.º 2
0
def convert_obj_to_rdf_namespace(obj, ns_obj=None):
    ''' This function takes rdf json definitions and converts all of the
        uri strings to a ns_value format '''
    if ns_obj is None:
        from rdfframework import get_framework
        ns_obj = get_framework().ns_obj

    if isinstance(obj, list):
        _return_list = []
        for item in obj:
            if isinstance(item, list):
                _return_list.append(convert_obj_to_rdf_namespace(item, ns_obj))
            elif isinstance(item, dict):
                _return_list.append(convert_obj_to_rdf_namespace(item, ns_obj))
            else:
                _return_list.append(convert_to_ns(item, ns_obj))
        return _return_list
    elif isinstance(obj, dict):
        _return_obj = {}
        for key, item in obj.items():
            nkey = convert_to_ns(key, ns_obj)
            if isinstance(item, list):
                _return_obj[nkey] = convert_obj_to_rdf_namespace(item, ns_obj)
            elif isinstance(item, dict):
                _return_obj[nkey] = convert_obj_to_rdf_namespace(item, ns_obj)
            else:
                _return_obj[nkey] = convert_to_ns(item, ns_obj)
        return _return_obj
    else:
        return convert_to_ns(obj, ns_obj)
def iris_to_strings(obj, ns_obj=None):
    if ns_obj is None:
        from rdfframework import get_framework
        ns_obj = get_framework().ns_obj    
    
    if isinstance(obj, list):
        _return_list = []
        for item in obj:
            if isinstance(item, list):
                _return_list.append(iris_to_strings(item, ns_obj))
            elif isinstance(item, dict):
                _return_list.append(iris_to_strings(item, ns_obj))
            else:
                 _return_list.append(convert_to_uri(item, ns_obj, True))
        return _return_list
    elif isinstance(obj, dict):
        _return_obj = {}
        for key, item in obj.items():
            nkey = convert_to_ns(key, ns_obj)
            if isinstance(item, list):
                _return_obj[nkey] = iris_to_strings(item, ns_obj)
            elif isinstance(item, dict):
                _return_obj[nkey] = iris_to_strings(item, ns_obj)
            else:
                _return_obj[nkey] = convert_to_uri(item, ns_obj, True)
        return _return_obj
    else:
        return convert_to_uri(obj, ns_obj, True)
def uri_prefix(value):
    ''' Takes a uri and returns the prefix for that uri '''
    if not DEBUG:
        debug = False
    else:
        debug = False
    if debug: print("START uri_prefix() uriconvertor.py -------------------\n")
    global NS_OBJ
    if NS_OBJ is None:
        from rdfframework import get_framework
        NS_OBJ=get_framework().ns_obj
    _uri = None
    if not str(value).startswith("http"):
        _uri = convert_to_uri(value, NS_OBJ)
    else:
        _uri = value
    _ns_uri = _uri.replace(re.sub(r"^(.*[#/])", "", str(_uri)),"")
    if debug: print("_uri: ", _uri)
    if debug: print("_ns_uri: ", _ns_uri)
    if _uri:
        for prefix, uri in NS_OBJ.items():
            if debug: print("uri: ", uri, " prefix: ", prefix)
            if _ns_uri == uri:
                value = prefix
                break
    if debug: print("END uri_prefix() uriconvertor.py -------------------\n")
    return value
Ejemplo n.º 5
0
def convert_to_uri(value, ns_obj=None, strip_iri=False):
    ''' converts a prefixed rdf ns equivalent value to its uri form. 
        If not found returns the value as is '''

    if ns_obj is None:
        from rdfframework import get_framework
        ns_obj = get_framework().ns_obj
    for _prefix, _ns_uri in ns_obj.items():
        if str(value).startswith(_prefix + "_") or \
                str(value).startswith("<%s_" % _prefix):
            if strip_iri:
                return value.replace("%s_" % _prefix, _ns_uri).replace(\
                        "<","").replace(">","")
            else:
                return value.replace("%s_" % _prefix, _ns_uri)
        if str(value).startswith(_prefix + ":") or \
                str(value).startswith("<%s:" % _prefix):
            if strip_iri:
                return value.replace("%s:" % _prefix, _ns_uri).replace(\
                        "<","").replace(">","")
            else:
                return value.replace("%s:" % _prefix, _ns_uri)
    if str(value).lower() == "none":
        return ""
    else:
        return value
Ejemplo n.º 6
0
def iris_to_strings(obj, ns_obj=None):
    if ns_obj is None:
        from rdfframework import get_framework
        ns_obj = get_framework().ns_obj

    if isinstance(obj, list):
        _return_list = []
        for item in obj:
            if isinstance(item, list):
                _return_list.append(iris_to_strings(item, ns_obj))
            elif isinstance(item, dict):
                _return_list.append(iris_to_strings(item, ns_obj))
            else:
                _return_list.append(convert_to_uri(item, ns_obj, True))
        return _return_list
    elif isinstance(obj, dict):
        _return_obj = {}
        for key, item in obj.items():
            nkey = convert_to_ns(key, ns_obj)
            if isinstance(item, list):
                _return_obj[nkey] = iris_to_strings(item, ns_obj)
            elif isinstance(item, dict):
                _return_obj[nkey] = iris_to_strings(item, ns_obj)
            else:
                _return_obj[nkey] = convert_to_uri(item, ns_obj, True)
        return _return_obj
    else:
        return convert_to_uri(obj, ns_obj, True)
Ejemplo n.º 7
0
def uri_prefix(value):
    ''' Takes a uri and returns the prefix for that uri '''
    if not DEBUG:
        debug = False
    else:
        debug = False
    if debug: print("START uri_prefix() uriconvertor.py -------------------\n")
    global NS_OBJ
    if NS_OBJ is None:
        from rdfframework import get_framework
        NS_OBJ = get_framework().ns_obj
    _uri = None
    if not str(value).startswith("http"):
        _uri = convert_to_uri(value, NS_OBJ)
    else:
        _uri = value
    _ns_uri = _uri.replace(re.sub(r"^(.*[#/])", "", str(_uri)), "")
    if debug: print("_uri: ", _uri)
    if debug: print("_ns_uri: ", _ns_uri)
    if _uri:
        for prefix, uri in NS_OBJ.items():
            if debug: print("uri: ", uri, " prefix: ", prefix)
            if _ns_uri == uri:
                value = prefix
                break
    if debug: print("END uri_prefix() uriconvertor.py -------------------\n")
    return value
def convert_obj_to_rdf_namespace(obj, ns_obj=None):
    ''' This function takes rdf json definitions and converts all of the
        uri strings to a ns_value format ''' 
    if ns_obj is None:
        from rdfframework import get_framework
        ns_obj = get_framework().ns_obj    
    
    if isinstance(obj, list):
        _return_list = []
        for item in obj:
            if isinstance(item, list):
                _return_list.append(convert_obj_to_rdf_namespace(item, ns_obj))
            elif isinstance(item, dict):
                _return_list.append(convert_obj_to_rdf_namespace(item, ns_obj))
            else:
                 _return_list.append(convert_to_ns(item, ns_obj))
        return _return_list
    elif isinstance(obj, dict):
        _return_obj = {}
        for key, item in obj.items():
            nkey = convert_to_ns(key, ns_obj)
            if isinstance(item, list):
                _return_obj[nkey] = convert_obj_to_rdf_namespace(item, ns_obj)
            elif isinstance(item, dict):
                _return_obj[nkey] = convert_obj_to_rdf_namespace(item, ns_obj)
            else:
                _return_obj[nkey] = convert_to_ns(item, ns_obj)
        return _return_obj
    else:
        return convert_to_ns(obj, ns_obj)
def uri(value): 
    global NS_OBJ
    if NS_OBJ is None:
        from rdfframework import get_framework
        NS_OBJ=get_framework().ns_obj
    if str(value).startswith("http"):
        return value
    else:
        return convert_to_uri(value)
Ejemplo n.º 10
0
def uri(value):
    global NS_OBJ
    if NS_OBJ is None:
        from rdfframework import get_framework
        NS_OBJ = get_framework().ns_obj
    if str(value).startswith("http"):
        return value
    else:
        return convert_to_uri(value)
Ejemplo n.º 11
0
def ttluri(value):
    ''' converts an iri to the app defined rdf namespaces in the framework 
        in a turtle accessable format. i.e. schema_name or 
        http:schema.org/name  --> schema:name '''
    global NS_OBJ
    if NS_OBJ is None:
        from rdfframework import get_framework
        NS_OBJ=get_framework().ns_obj
    if str(value).startswith("http"):
        return convert_to_ttl(value, NS_OBJ)
    else:
        return convert_to_ttl(convert_to_uri(value, NS_OBJ), NS_OBJ)
Ejemplo n.º 12
0
def ttluri(value):
    ''' converts an iri to the app defined rdf namespaces in the framework 
        in a turtle accessable format. i.e. schema_name or 
        http:schema.org/name  --> schema:name '''
    global NS_OBJ
    if NS_OBJ is None:
        from rdfframework import get_framework
        NS_OBJ = get_framework().ns_obj
    if str(value).startswith("http"):
        return convert_to_ttl(value, NS_OBJ)
    else:
        return convert_to_ttl(convert_to_uri(value, NS_OBJ), NS_OBJ)
 def _salt_class_search(class_uri):
     _class_properties = getattr(get_framework(), class_uri).kds_properties
     _salt_property = None
     _salt_url = "kdr_SaltProcessor"
     for _class_prop in _class_properties.values():
         _processors = clean_processors([make_list(\
                 _class_prop.get("kds_propertyProcessing",{}))])
         for _processor in _processors.values():
             if _processor.get("rdf_type") == _salt_url:
                 _salt_property = {"kds_classUri": class_uri, \
                             "kds_propUri": _class_prop.get("kds_propUri")}
                 salt_processor_dict = _processor    
     return _salt_property    
Ejemplo n.º 14
0
def nouri(value):
    global NS_OBJ
    if NS_OBJ is None:
        from rdfframework import get_framework
        NS_OBJ = get_framework().ns_obj
    _uri = None
    if not str(value).startswith("http"):
        _uri = convert_to_uri(value, NS_OBJ)
    else:
        _uri = value
    if _uri:
        return re.sub(r"^(.*[#/])", "", str(_uri))
    else:
        return value
Ejemplo n.º 15
0
def nouri(value):
    global NS_OBJ
    if NS_OBJ is None:
        from rdfframework import get_framework
        NS_OBJ=get_framework().ns_obj
    _uri = None
    if not str(value).startswith("http"):
        _uri = convert_to_uri(value, NS_OBJ)
    else:
        _uri = value
    if _uri:
        return re.sub(r"^(.*[#/])", "", str(_uri))
    else:
        return value
Ejemplo n.º 16
0
def convert_to_ns(value, ns_obj=None):
    ''' converts a value to the prefixed rdf ns equivalent. If not found
        returns the value as is '''
    if ns_obj is None:
        from rdfframework import get_framework
        ns_obj = get_framework().ns_obj
    for _prefix, _ns_uri in ns_obj.items():
        if str(value).startswith(_prefix + ":") or \
                str(value).startswith("<%s:" % _prefix):
            return value.replace(_prefix + ":", _prefix + "_").replace(\
                    "<","").replace(">","")
        if str(value).startswith(_ns_uri) or str(value).startswith("<"+_ns_uri):
            return value.replace(_ns_uri, _prefix + "_").replace(\
                    "<","").replace(">","")
    return value
Ejemplo n.º 17
0
def convert_to_ns(value, ns_obj=None):
    ''' converts a value to the prefixed rdf ns equivalent. If not found
        returns the value as is '''
    if ns_obj is None:
        from rdfframework import get_framework
        ns_obj = get_framework().ns_obj
    for _prefix, _ns_uri in ns_obj.items():
        if str(value).startswith(_prefix + ":") or \
                str(value).startswith("<%s:" % _prefix):
            return value.replace(_prefix + ":", _prefix + "_").replace(\
                    "<","").replace(">","")
        if str(value).startswith(_ns_uri) or str(value).startswith("<" +
                                                                   _ns_uri):
            return value.replace(_ns_uri, _prefix + "_").replace(\
                    "<","").replace(">","")
    return value
def salt_processor(processor, obj, prop, mode="save", **kwargs):
    '''Generates a random string for salting'''
    if mode == "load":
        return obj.get("dataValue")
    length = 32
    obj['prop']['calcValue'] = True
    # if called from the password processor the kwargs will have a
    # salt_property and we can automatically generate a new one
    if kwargs.get('salt_property'):
        obj['processedData'][kwargs['salt_property']] = \
                        b64encode(os.urandom(length)).decode('utf-8')
        return obj
    # if the salt already exists in the processed data return the obj
    # the processor may have been called by the password processor
    if is_not_null(obj['processedData'].get(obj['propUri'])):
        return obj

    # find the password property
    _class_uri = obj['prop'].get("classUri")
    _class_properties = getattr(get_framework(), _class_uri).kds_properties
    password_property = None
    for _class_prop in _class_properties.values():
        if _class_prop.get("kds_propertyProcessing",{}).get("rdf_type") \
                == "kds_PasswordProcessor":
            password_property = obj['preSaveData'].get(\
                                            _class_prop.get("kds_propUri"))
    
    # check if there is a new password in the preSaveData
    #                         or
    # if the salt property is required and the old salt is empty
    if password_property is not None:
        if is_not_null(password_property.get('new')) or \
                                    (obj['prop'].get('required') and \
                                            not is_not_null(obj['prop']['old'])):
            obj['processedData'][obj['propUri']] = \
                        b64encode(os.urandom(length)).decode('utf-8')
    elif not is_not_null(obj['prop']['old']):
        obj['processedData'][obj['propUri']] = \
                        b64encode(os.urandom(length)).decode('utf-8')
    obj['prop']['calcValue'] = True
    return obj
def password_processor(processor, obj, prop, mode="save"):
    """Function handles application password actions

    Returns:
        modified passed in obj
    """
    if DEBUG:
        debug = True
    else:
        debug = False
    if debug: print("START password_processor --------------------------\n")
    salt_url = "kdr_SaltProcessor"
    if mode == "save":
        # find the salt property
        
        _class_uri = obj['prop'].get("classUri")
        _class_properties = getattr(get_framework(), _class_uri).kds_properties
        salt_property = None
        # find the property Uri that stores the salt value
        for _class_prop in _class_properties.values():
            _processors = clean_processors([make_list(\
                    _class_prop.get("kds_propertyProcessing",{}))])
            for _processor in _processors.values():
                if _processor.get("rdf_type") == salt_url:
                    salt_property = _class_prop.get("kds_propUri")
                    salt_processor_dict = _processor
        # if in save mode create a hashed password
        if mode == "save":
            # if the there is not a new password in the data return the obj
            if is_not_null(obj['prop']['new']) or obj['prop']['new'] != 'None':
                # if a salt has not been created call the salt processor
                if not obj['processedData'].get(salt_property):
                    obj = salt_processor(salt_processor_dict,
                                         obj, 
                                         mode,
                                         salt_property=salt_property)
                # create the hash
                salt = obj['processedData'].get(salt_property)
                _hash_value = sha256_crypt.encrypt(obj['prop']['new']+salt)
                # assign the hashed password to the processedData
                obj['processedData'][obj['propUri']] = _hash_value
                obj['prop']['calcValue'] = True
                if debug: print("END password_processor  mode = save-------\n")
            return obj
    elif mode == "verify":
        # verify the supplied password matches the saved password
        if not len(obj.query_data) > 0:
            setattr(prop, "password_verified", False)
            return obj    
        _class_uri = prop.kds_classUri
        _class_properties = getattr(get_framework(), _class_uri).kds_properties
        salt_property = None
        # find the property Uri that stores the salt value
        for _class_prop in _class_properties.values():
            _processors = clean_processors([make_list(\
                    _class_prop.get("kds_propertyProcessing",{}))])
            for _processor in _processors.values():
                if _processor.get("rdf_type") == salt_url:
                    salt_property = _class_prop.get("kds_propUri")
                    salt_processor_dict = _processor
        # find the salt value in the query_data
        salt_value = None
        for subject, props in obj.query_data.items():
            if clean_iri(props.get("rdf_type")) == _class_uri:
                salt_value = props.get(salt_property)
                hashed_password = props.get(prop.kds_propUri)
                break
        if debug: print(salt_value, " - ", hashed_password, " - ", prop.data)
        setattr(prop, "password_verified", \
            sha256_crypt.verify(prop.data + salt_value, hashed_password)) 
        if debug: print("END password_processor  mode = verify -------\n")
        return obj
    if mode == "load":
        if debug: print("END password_processor  mode = load -------\n")
        return obj
    return obj