Init script extension and key loading for netwrapper
This commit is contained in:
@@ -6,7 +6,7 @@ from Crypto.PublicKey import RSA
|
||||
from authentication import Authetication
|
||||
|
||||
|
||||
def generatePrivateKeyForUser(auth: Authetication, username: str, user_passphrase: str, public_server_key='') -> bool:
|
||||
def generatePrivateKeyForUser(auth: Authetication,username: str, user_passphrase: str, public_server_key: str) -> bool:
|
||||
if auth.checkUserExists(username):
|
||||
with open(auth.CONFIG_FILE_LOCATION) as json_file:
|
||||
data = json.load(json_file)
|
||||
@@ -17,9 +17,10 @@ def generatePrivateKeyForUser(auth: Authetication, username: str, user_passphras
|
||||
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,
|
||||
user_privatekey = {'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:
|
||||
with open(auth.PRIVATE_KEY_DIRECTORY_LOCATION + os.path.sep + str(data['index']) + '.txt',
|
||||
'w+') as outfile:
|
||||
json.dump(user_privatekey, outfile)
|
||||
outfile.close()
|
||||
|
||||
@@ -27,7 +28,7 @@ def generatePrivateKeyForUser(auth: Authetication, username: str, user_passphras
|
||||
for user in data['user']:
|
||||
if username == user['username']:
|
||||
user['publicKey'] = public_key_value
|
||||
with open(auth.CONFIG_FILE_LOCATION, 'w') as outfile:
|
||||
with open(auth.CONFIG_FILE_LOCATION, 'w+') as outfile:
|
||||
json.dump(data, outfile)
|
||||
break
|
||||
outfile.close()
|
||||
@@ -36,10 +37,28 @@ def generatePrivateKeyForUser(auth: Authetication, username: str, user_passphras
|
||||
return False
|
||||
|
||||
|
||||
def generatePrivateKeyForServer(auth: Authetication,passphrase: str) -> str:
|
||||
with open(auth.CONFIG_FILE_LOCATION) as json_file:
|
||||
data = json.load(json_file)
|
||||
json_file.close()
|
||||
|
||||
private_key = RSA.generate(2048)
|
||||
public_key = private_key.publickey()
|
||||
private_key_value = bytes.hex(private_key.exportKey('DER', passphrase=passphrase, pkcs=8))
|
||||
public_key_value = bytes.hex(public_key.exportKey('DER', pkcs=8))
|
||||
|
||||
data['serverPrivateKey'] = private_key_value
|
||||
with open(auth.CONFIG_FILE_LOCATION, 'w+') as outfile:
|
||||
json.dump(data, outfile)
|
||||
|
||||
return public_key_value
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
auth = Authetication()
|
||||
auth.initConfig()
|
||||
auth.saveUser('alma' ,'alma')
|
||||
generatePrivateKeyForUser('alma', 'amla')
|
||||
auth.saveUser('citrom' ,'citrom')
|
||||
generatePrivateKeyForUser('citrom', 'mortic')
|
||||
serverPublicKey = generatePrivateKeyForServer(auth, 'admin')
|
||||
auth.saveUser('alma', 'alma')
|
||||
generatePrivateKeyForUser(auth, 'alma', 'amla', serverPublicKey)
|
||||
auth.saveUser('citrom', 'citrom')
|
||||
generatePrivateKeyForUser(auth, 'citrom', 'mortic', serverPublicKey)
|
||||
|
||||
Reference in New Issue
Block a user