1: config init in separate file
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is passing
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	continuous-integration/drone/push Build is passing
				
			2: auth_test doesnt generate production state
This commit is contained in:
		@@ -54,7 +54,6 @@ class Authetication:
 | 
				
			|||||||
        auth_logger.debug("User logged in: " + username)
 | 
					        auth_logger.debug("User logged in: " + username)
 | 
				
			||||||
        return user['homeDir']
 | 
					        return user['homeDir']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
    def checkUserExists(self, username: str) -> bool:
 | 
					    def checkUserExists(self, username: str) -> bool:
 | 
				
			||||||
        with open(self.CONFIG_FILE_LOCATION) as json_file:
 | 
					        with open(self.CONFIG_FILE_LOCATION) as json_file:
 | 
				
			||||||
            data = json.load(json_file)
 | 
					            data = json.load(json_file)
 | 
				
			||||||
@@ -76,39 +75,6 @@ class Authetication:
 | 
				
			|||||||
        os.mkdir(self.PRIVATE_KEY_DIRECTORY_LOCATION)
 | 
					        os.mkdir(self.PRIVATE_KEY_DIRECTORY_LOCATION)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def generatePrivateKeyForUser(self, username:str, user_passphrase:str, public_server_key='') -> bool:
 | 
					 | 
				
			||||||
        if self.checkUserExists(username):
 | 
					 | 
				
			||||||
            with open(self.CONFIG_FILE_LOCATION) as json_file:
 | 
					 | 
				
			||||||
                data = json.load(json_file)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            private_key = RSA.generate(2048)
 | 
					 | 
				
			||||||
            public_key = private_key.publickey()
 | 
					 | 
				
			||||||
            private_key_value = bytes.hex(private_key.exportKey('DER', passphrase=user_passphrase, pkcs=8))
 | 
					 | 
				
			||||||
            public_key_value = bytes.hex(public_key.exportKey('DER', pkcs=8))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            ##Save private key in separate file
 | 
					 | 
				
			||||||
            user_privatekey = {'passphrase': user_passphrase, 'privateClientKey': private_key_value, 'publicServerKey': public_server_key}
 | 
					 | 
				
			||||||
            with open(self.PRIVATE_KEY_DIRECTORY_LOCATION + os.path.sep + str(data['index']) + '.txt', 'w+') as outfile:
 | 
					 | 
				
			||||||
                json.dump(user_privatekey, outfile)
 | 
					 | 
				
			||||||
            outfile.close()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            ##Save public key in users
 | 
					 | 
				
			||||||
            for user in data['user']:
 | 
					 | 
				
			||||||
                if username == user['username']:
 | 
					 | 
				
			||||||
                    user['publicKey'] = public_key_value
 | 
					 | 
				
			||||||
                    with open(self.CONFIG_FILE_LOCATION, 'w') as outfile:
 | 
					 | 
				
			||||||
                        json.dump(data, outfile)
 | 
					 | 
				
			||||||
                    break
 | 
					 | 
				
			||||||
            outfile.close()
 | 
					 | 
				
			||||||
            return True
 | 
					 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            return False
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def saveUser(self, username: str, password: str) -> bool:
 | 
					    def saveUser(self, username: str, password: str) -> bool:
 | 
				
			||||||
        bytePass = password.encode('utf-8')
 | 
					        bytePass = password.encode('utf-8')
 | 
				
			||||||
        b64pwd = b64encode(SHA256.new(bytePass).digest())
 | 
					        b64pwd = b64encode(SHA256.new(bytePass).digest())
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,5 @@
 | 
				
			|||||||
from authentication import Authetication
 | 
					from authentication import Authetication
 | 
				
			||||||
 | 
					import config_init as init
 | 
				
			||||||
import json
 | 
					import json
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
@@ -63,26 +64,14 @@ def testUserExists(username: str, password: str):
 | 
				
			|||||||
        logging.info('TEST 2 --> User exists with INVALID user :: PASSED')
 | 
					        logging.info('TEST 2 --> User exists with INVALID user :: PASSED')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def productionInit():
 | 
					 | 
				
			||||||
    logging.info('INITIATING CONFIG FILE')
 | 
					 | 
				
			||||||
    auth = Authetication()
 | 
					 | 
				
			||||||
    auth.initConfig()
 | 
					 | 
				
			||||||
    auth.saveUser('alma','alma')
 | 
					 | 
				
			||||||
    auth.generatePrivateKeyForUser('alma', 'amla')
 | 
					 | 
				
			||||||
    auth.saveUser('citrom','citrom')
 | 
					 | 
				
			||||||
    auth.generatePrivateKeyForUser('citrom', 'mortic')
 | 
					 | 
				
			||||||
    auth.saveUser('dinnye','dinnye')
 | 
					 | 
				
			||||||
    auth.generatePrivateKeyForUser('dinnye', 'eynnid')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def testPersistency():
 | 
					def testPersistency():
 | 
				
			||||||
    logging.info('PERSISTENCY TEST')
 | 
					    logging.info('PERSISTENCY TEST')
 | 
				
			||||||
    auth = Authetication()
 | 
					    auth = Authetication()
 | 
				
			||||||
    auth.initConfig()
 | 
					    auth.initConfig()
 | 
				
			||||||
    auth.saveUser('alma','alma')
 | 
					    auth.saveUser('alma','alma')
 | 
				
			||||||
    auth.generatePrivateKeyForUser('alma', 'amla')
 | 
					    init.generatePrivateKeyForUser(auth, 'alma', 'amla')
 | 
				
			||||||
    auth.saveUser('citrom','citrom')
 | 
					    auth.saveUser('citrom','citrom')
 | 
				
			||||||
    auth.generatePrivateKeyForUser('citrom', 'mortic')
 | 
					    init.generatePrivateKeyForUser(auth, 'citrom', 'mortic')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    auth2 = Authetication()
 | 
					    auth2 = Authetication()
 | 
				
			||||||
    if auth2.checkUserExists('alma'):
 | 
					    if auth2.checkUserExists('alma'):
 | 
				
			||||||
@@ -127,4 +116,3 @@ if __name__ == '__main__':
 | 
				
			|||||||
    testAuth("Diósbejglia", "Diósbejgli")
 | 
					    testAuth("Diósbejglia", "Diósbejgli")
 | 
				
			||||||
    testUserExists("Diósbejglia", "Diósbejgli")
 | 
					    testUserExists("Diósbejglia", "Diósbejgli")
 | 
				
			||||||
    testPersistency()
 | 
					    testPersistency()
 | 
				
			||||||
    productionInit()
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										44
									
								
								server/config_init.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								server/config_init.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
				
			|||||||
 | 
					from authentication import Authetication
 | 
				
			||||||
 | 
					from Crypto.PublicKey import RSA
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
 | 
					import logging
 | 
				
			||||||
 | 
					import os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def generatePrivateKeyForUser(auth: Authetication, username: str, user_passphrase: str, public_server_key='') -> bool:
 | 
				
			||||||
 | 
					    if auth.checkUserExists(username):
 | 
				
			||||||
 | 
					        with open(auth.CONFIG_FILE_LOCATION) as json_file:
 | 
				
			||||||
 | 
					            data = json.load(json_file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private_key = RSA.generate(2048)
 | 
				
			||||||
 | 
					        public_key = private_key.publickey()
 | 
				
			||||||
 | 
					        private_key_value = bytes.hex(private_key.exportKey('DER', passphrase=user_passphrase, pkcs=8))
 | 
				
			||||||
 | 
					        public_key_value = bytes.hex(public_key.exportKey('DER', pkcs=8))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ##Save private key in separate file
 | 
				
			||||||
 | 
					        user_privatekey = {'passphrase': user_passphrase, 'privateClientKey': private_key_value,
 | 
				
			||||||
 | 
					                           'publicServerKey': public_server_key}
 | 
				
			||||||
 | 
					        with open(auth.PRIVATE_KEY_DIRECTORY_LOCATION + os.path.sep + str(data['index']) + '.txt', 'w+') as outfile:
 | 
				
			||||||
 | 
					            json.dump(user_privatekey, outfile)
 | 
				
			||||||
 | 
					        outfile.close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ##Save public key in users
 | 
				
			||||||
 | 
					        for user in data['user']:
 | 
				
			||||||
 | 
					            if username == user['username']:
 | 
				
			||||||
 | 
					                user['publicKey'] = public_key_value
 | 
				
			||||||
 | 
					                with open(auth.CONFIG_FILE_LOCATION, 'w') as outfile:
 | 
				
			||||||
 | 
					                    json.dump(data, outfile)
 | 
				
			||||||
 | 
					                break
 | 
				
			||||||
 | 
					        outfile.close()
 | 
				
			||||||
 | 
					        return True
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        return False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 | 
					    auth = Authetication()
 | 
				
			||||||
 | 
					    auth.initConfig()
 | 
				
			||||||
 | 
					    auth.saveUser('alma' ,'alma')
 | 
				
			||||||
 | 
					    generatePrivateKeyForUser('alma', 'amla')
 | 
				
			||||||
 | 
					    auth.saveUser('citrom' ,'citrom')
 | 
				
			||||||
 | 
					    generatePrivateKeyForUser('citrom', 'mortic')
 | 
				
			||||||
		Reference in New Issue
	
	Block a user