This commit is contained in:
		@@ -1,4 +1,4 @@
 | 
				
			|||||||
import os,sys
 | 
					import os, sys
 | 
				
			||||||
from base64 import b64encode
 | 
					from base64 import b64encode
 | 
				
			||||||
from Crypto.Hash import SHA256
 | 
					from Crypto.Hash import SHA256
 | 
				
			||||||
from Crypto.Protocol.KDF import bcrypt, bcrypt_check
 | 
					from Crypto.Protocol.KDF import bcrypt, bcrypt_check
 | 
				
			||||||
@@ -9,10 +9,11 @@ import logging
 | 
				
			|||||||
auth_logger = logging.getLogger('AUTH APPLICATION ')
 | 
					auth_logger = logging.getLogger('AUTH APPLICATION ')
 | 
				
			||||||
auth_logger.root.setLevel(logging.INFO)
 | 
					auth_logger.root.setLevel(logging.INFO)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Authetication:
 | 
					class Authetication:
 | 
				
			||||||
    ABSOLUTE_PATH = os.path.abspath(os.path.dirname(sys.argv[0]))
 | 
					    ABSOLUTE_PATH = os.path.abspath(os.path.dirname(sys.argv[0]))
 | 
				
			||||||
    HOME_DIRECTORY_LOCATION = ABSOLUTE_PATH + "\\home"
 | 
					    HOME_DIRECTORY_LOCATION = ABSOLUTE_PATH + "\\home"
 | 
				
			||||||
    CONFIG_DIRECTORY_LOCATION = ABSOLUTE_PATH+ "\\config"
 | 
					    CONFIG_DIRECTORY_LOCATION = ABSOLUTE_PATH + "\\config"
 | 
				
			||||||
    USER_INDEX = 0
 | 
					    USER_INDEX = 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self):
 | 
					    def __init__(self):
 | 
				
			||||||
@@ -22,14 +23,14 @@ class Authetication:
 | 
				
			|||||||
        if not os.path.isdir(self.CONFIG_DIRECTORY_LOCATION):
 | 
					        if not os.path.isdir(self.CONFIG_DIRECTORY_LOCATION):
 | 
				
			||||||
            os.mkdir(self.CONFIG_DIRECTORY_LOCATION)
 | 
					            os.mkdir(self.CONFIG_DIRECTORY_LOCATION)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if not os.path.isfile(self.CONFIG_DIRECTORY_LOCATION + "\\config.txt") or os.stat(self.CONFIG_DIRECTORY_LOCATION + "\\config.txt").st_size == 0:
 | 
					        if not os.path.isfile(self.CONFIG_DIRECTORY_LOCATION + "\\config.txt") or os.stat(
 | 
				
			||||||
 | 
					                self.CONFIG_DIRECTORY_LOCATION + "\\config.txt").st_size == 0:
 | 
				
			||||||
            data = {}
 | 
					            data = {}
 | 
				
			||||||
            data['index'] = 0
 | 
					            data['index'] = 0
 | 
				
			||||||
            data['user'] = []
 | 
					            data['user'] = []
 | 
				
			||||||
            with open(self.CONFIG_DIRECTORY_LOCATION + "\\config.txt", 'w+') as outfile:
 | 
					            with open(self.CONFIG_DIRECTORY_LOCATION + "\\config.txt", 'w+') as outfile:
 | 
				
			||||||
                json.dump(data, outfile)
 | 
					                json.dump(data, outfile)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
    def login(self, username, password):
 | 
					    def login(self, username, password):
 | 
				
			||||||
        with open(Authetication.CONFIG_DIRECTORY_LOCATION + '\\config.txt') as json_file:
 | 
					        with open(Authetication.CONFIG_DIRECTORY_LOCATION + '\\config.txt') as json_file:
 | 
				
			||||||
            data = json.load(json_file)
 | 
					            data = json.load(json_file)
 | 
				
			||||||
@@ -43,10 +44,9 @@ class Authetication:
 | 
				
			|||||||
                except ValueError:
 | 
					                except ValueError:
 | 
				
			||||||
                    auth_logger.debug("User NOT logged in: " + username)
 | 
					                    auth_logger.debug("User NOT logged in: " + username)
 | 
				
			||||||
                    return ''
 | 
					                    return ''
 | 
				
			||||||
        auth_logger.debug("User logged in: "+username)
 | 
					        auth_logger.debug("User logged in: " + username)
 | 
				
			||||||
        return user['homeDir']
 | 
					        return user['homeDir']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
    def checkUserExists(self, username):
 | 
					    def checkUserExists(self, username):
 | 
				
			||||||
        with open(Authetication.CONFIG_DIRECTORY_LOCATION + '\\config.txt') as json_file:
 | 
					        with open(Authetication.CONFIG_DIRECTORY_LOCATION + '\\config.txt') as json_file:
 | 
				
			||||||
            data = json.load(json_file)
 | 
					            data = json.load(json_file)
 | 
				
			||||||
@@ -57,16 +57,13 @@ class Authetication:
 | 
				
			|||||||
        return False
 | 
					        return False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def initConfig(self):
 | 
					    def initConfig(self):
 | 
				
			||||||
        data = {}
 | 
					        data = {'index': 0, 'user': []}
 | 
				
			||||||
        data['index'] = 0
 | 
					 | 
				
			||||||
        data['user'] = []
 | 
					 | 
				
			||||||
        with open(self.CONFIG_DIRECTORY_LOCATION + "\\config.txt", 'w+') as outfile:
 | 
					        with open(self.CONFIG_DIRECTORY_LOCATION + "\\config.txt", 'w+') as outfile:
 | 
				
			||||||
            json.dump(data, outfile)
 | 
					            json.dump(data, outfile)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        shutil.rmtree(self.HOME_DIRECTORY_LOCATION)
 | 
					        shutil.rmtree(self.HOME_DIRECTORY_LOCATION)
 | 
				
			||||||
        os.mkdir(self.HOME_DIRECTORY_LOCATION)
 | 
					        os.mkdir(self.HOME_DIRECTORY_LOCATION)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
    def saveUser(self, username, password):
 | 
					    def saveUser(self, username, password):
 | 
				
			||||||
        bytePass = password.encode('utf-8')
 | 
					        bytePass = password.encode('utf-8')
 | 
				
			||||||
        b64pwd = b64encode(SHA256.new(bytePass).digest())
 | 
					        b64pwd = b64encode(SHA256.new(bytePass).digest())
 | 
				
			||||||
@@ -78,7 +75,7 @@ class Authetication:
 | 
				
			|||||||
        user = {
 | 
					        user = {
 | 
				
			||||||
            'username': username,
 | 
					            'username': username,
 | 
				
			||||||
            'password': bcrypt_hash.decode('utf-8'),
 | 
					            'password': bcrypt_hash.decode('utf-8'),
 | 
				
			||||||
            'homeDir':  data['index']+1
 | 
					            'homeDir': data['index'] + 1
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        data['index'] = data['index'] + 1
 | 
					        data['index'] = data['index'] + 1
 | 
				
			||||||
@@ -97,7 +94,3 @@ class Authetication:
 | 
				
			|||||||
                auth_logger.debug("User NOT saved! Home directory already exists")
 | 
					                auth_logger.debug("User NOT saved! Home directory already exists")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            return True
 | 
					            return True
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,10 +2,10 @@ from authentication import Authetication
 | 
				
			|||||||
import json
 | 
					import json
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
test_logger = logging.getLogger('TEST ')
 | 
					test_logger = logging.getLogger('TEST ')
 | 
				
			||||||
test_logger.root.setLevel(logging.INFO)
 | 
					test_logger.root.setLevel(logging.INFO)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def testSaveUser(username, password):
 | 
					def testSaveUser(username, password):
 | 
				
			||||||
    logging.info('STARTING SAVE USER TEST')
 | 
					    logging.info('STARTING SAVE USER TEST')
 | 
				
			||||||
    auth = Authetication()
 | 
					    auth = Authetication()
 | 
				
			||||||
@@ -61,7 +61,8 @@ def testUserExists(username, password):
 | 
				
			|||||||
    else:
 | 
					    else:
 | 
				
			||||||
        logging.info('TEST 2 --> User exists with INVALID user :: PASSED')
 | 
					        logging.info('TEST 2 --> User exists with INVALID user :: PASSED')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
    testSaveUser("Diósbejglia","Diósbejgli")
 | 
					    testSaveUser("Diósbejglia", "Diósbejgli")
 | 
				
			||||||
    testAuth("Diósbejglia","Diósbejgli")
 | 
					    testAuth("Diósbejglia", "Diósbejgli")
 | 
				
			||||||
    testUserExists("Diósbejglia","Diósbejgli")
 | 
					    testUserExists("Diósbejglia", "Diósbejgli")
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user