Exemplo n.º 1
0
def remove_keys_directory(user):
    with open(user_index, 'r+') as f:
        ref = log_entry ("Removing %s's keys location at %s...." % (user, user_index))
        index_data = json.load(f)
        index_data.pop(user)
        f.seek(0)
        json.dump(index_data, f, sort_keys=True, indent=4)
        f.truncate()
        log_entry (ref, "Removed!")
Exemplo n.º 2
0
def set_keys_directory(user, directory):
    with open(user_index, 'r+') as f:
        ref = log_entry ("Updating %s's keys location at %s...." % (user, user_index))
        index_data = json.load(f)
        index_data[user] = directory
        f.seek(0)
        json.dump(index_data, f, sort_keys=True, indent=4)
        f.truncate()
        log_entry (ref, "Updated!")
Exemplo n.º 3
0
 def refresh_auth(self):
     data = parse_application_parameters(self.param)
     headers = {"Content-Type": "application/x-www-form-urlencoded", "User-Agent" : application_user_agent}
     req = request.Request(self.access_point, data=data, headers=headers)
     ref = log_entry ("Sending OAuth2 request to %s...." % self.context_short_str)
     raw_resp = raw_response(req)
     response = interpret_response(raw_resp)
     
     self.access_token  = response["access_token"]
     self.refresh_token = response["refresh_token"]
     log_entry (ref, "Authenticated!")
     
     self.param["refresh_token"] = self.refresh_token
     self.bearer = {"Authorization": "Bearer %s" % self.access_token}
     self.user_agent = {"User-Agent" : application_user_agent}
     self.default_headers = dict(self.bearer, **self.user_agent)
Exemplo n.º 4
0
 def __init__(self, user, password, testing = False):
     self.is_tester = testing
     self.server = beta_server if testing else main_server
     self.context_str = get_context_str("context", mode=self.__mode_str())
     self.context_short_str = get_context_str("context_short", mode=self.__mode_str())
     self.access_point = full_resource_url('access token', mode=self.__mode_str())
     self.param, location = retrieve_keys(user, password, context=self.context_short_str, return_location=True,)
     
     check_param = ["client_id", "client_secret", "password"]
     if not all(key in self.param for key in check_param) or len(self.param)!=len(check_param):
         raise ParameterTypeError ([key for key in self.param], check_param, location)
     
     self.param["grant_type"] = "password"
     self.param["username"]   = user
     
     ref = log_entry ("Connecting to %s...." % self.context_str)
     self.refresh_auth() #the name may seem inappropriate, but it's the same method
     log_entry (ref, "Connected!")
     
     self.param["grant_type"] = "refresh_token" #these two instructions make sure the fields are hereby updated correctly
     self.param.pop("password")
Exemplo n.º 5
0
def store_keys (user, keys, password="", context=default_context, if_no_directory = no_directory_default):
    directory = get_keys_directory(user, if_no_directory)
    
    if password:
        ref = log_entry ("Encrypting %s's keys...." % user)
        keys = transform(keys, password)
        log_entry (ref, "Encrypted!")
    else:
        log_entry ("WARNING: No password provided to encrypt %s's keys. This is unsafe, as keys will be stored in plain text." % user)
        
    filename = standard_filename(user, password, directory, context)
    with open(filename, 'w') as f:
        ref = log_entry("Storing %s's keys at %s...." % (user, filename))
        json.dump(keys, f, sort_keys=True, indent=4)
        log_entry(ref, "Stored!")
Exemplo n.º 6
0
def retrieve_keys (user, password="", context=default_context, return_location=False):
    directory = get_keys_directory(user)
    
    filename = standard_filename(user, password, directory, context)
    if os.path.isfile(filename):
        with open(filename, 'r') as f:
            ref = log_entry("Retrieving %s's keys from %s...." % (user, filename))
            keys = json.load(f)
            log_entry(ref, "Retrieved!")
    else:
        raise KeysFileNotFound(user, filename)
    
    if password:
        ref = log_entry ("Decrypting %s's keys...." % user)
        keys = transform(keys, password)
        log_entry (ref, "Decrypted!")
    
    if return_location:
        return (keys, filename)
    else:
        return keys
Exemplo n.º 7
0
 def wrapper():
     ref = log_entry("Loading {} client tools...".format(name))
     result = loader()
     log_entry(ref, "Loaded!!!")
     return result
Exemplo n.º 8
0
GET, PUT, POST, PATCH, DELETE = 'GET', 'PUT', 'POST', 'PATCH', 'DELETE'


    
def get_context_str(s, mode = "main"):
    return server_context_strings[mode][s]


main_server = SUPPORTED_SERVERS[get_context_str('context_short')]
beta_server = SUPPORTED_SERVERS[get_context_str('context_short', 'test')]




__ref1 = log_entry ("Setting up %s resource tree...." % get_context_str("host_website"))


# Initializing custom tool:
__ref2 = log_entry ("Initializing custom dictionary tool....")

class ResourceDict(MarkedDict):
    """
    ''HACK'' FOR RETURNING A PRIORI UNRECOGNIZED ENTRIES LITERALLY:
    """
    #There is a __hack_title, field, not initialized on purpose
    
    def setup_hack(self, id_counter=0):
        if self.is_hack(): # initializes the __hack_title field, if it does not exist.
            pass # End of recursion!! (explicit for readability)
        else:
Exemplo n.º 9
0
def get_keys_directory(user, on_fail = no_directory_default):
    with open(user_index, 'r+') as f:
        index_data = json.load(f)
        update =  False
        ref = log_entry("Searching %s's keys location from %s...." % (user, user_index))
        
        if user in index_data:
            dir = index_data[user]
        else:
            log_entry(ref, "Location not found.")
            dir = False
        
        if not (dir and os.path.isdir(dir)):
            if dir:
                log_entry (ref, "Location invalid.")
                index_data.pop(user)
                update = True
            
            ref = log_entry("Getting %s's keys location from backup method...." % user)
            dir = on_fail(user)
            
            try:
                if os.path.isdir(dir):
                    index_data[user] = dir
                    update = True
                else:
                    log_entry(ref, "Location not found or invalid.")
                    raise KeysDirectoryNotFound(user)
            finally:
                if update:
                    ref = log_entry ("Updating %s...." % user_index)
                    f.seek(0)
                    json.dump(index_data, f, sort_keys=True, indent=4)
                    f.truncate()
                    log_entry (ref, "Updated!")
    
    log_entry (ref, "Location found!")
    return dir
Exemplo n.º 10
0
def reset_index():
    with open (user_index, 'w') as f:
        json.dump({}, f)
        log_entry (user_index, "file reset to empty value.")
Exemplo n.º 11
0
        ref = log_entry ("Decrypting %s's keys...." % user)
        keys = transform(keys, password)
        log_entry (ref, "Decrypted!")
    
    if return_location:
        return (keys, filename)
    else:
        return keys


def standard_filename(user, password, directory, context):
    filename = context+(obfuscated if password else plaintext)+quote(user, safe='')+".json"
    return os.path.join(directory, filename)
    
    
    
    
###########################
##                       ##
##  INITIALIZATION CODE  ##
##                       ##
###########################

if not os.path.isfile(user_index):
    log_entry (user_index, "file does not exist.")
    __ref = log_entry ("Creating file %s...." % user_index)
    reset_index()
    log_entry(__ref, "File created. Ready!")
    del __ref
else:
    log_entry (user_index, "file exists. Ready!")