Initial Subject Structure

This commit is contained in:
JoaoBastos023 2024-11-12 10:26:56 +00:00
parent 54e9a5190b
commit b4fa815ba9
6 changed files with 206 additions and 0 deletions

View File

@ -0,0 +1,43 @@
#!/bin/python3
import os
import sys
import logging
import requests
import json
import re
logging.basicConfig(format='%(levelname)s\t- %(message)s')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
state = {}
# Create organization
def createOrganization(args):
# {'name', 'username' , 'full_name', 'email' , 'public_key' }
if len(args) != 5:
logger.error("Missing parameters. Expected: 'name' 'username' 'full name' 'email' 'public key' ")
sys.exit(-1)
for item in args:
if item == '':
logger.error("Need a valid " + item)
sys.exit(-1)
input = {'name' : args[0], 'username' : args[1], 'full_name' : args[2], 'email' : args[3], 'public_key' : args[4]}
if not re.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', input['email']):
logger.error("Need a valid email.")
sys.exit(-1)
if (not os.path.isfile(input['public_key'])):
logger.error("File '" + input['public_key'] + "' not found.")
sys.exit(-1)
req = requests.post(f'http://{state['REP_ADDRESS']}/organization/create', json=json.dumps(input))
if __name__ == '__main__':
createOrganization(sys.argv[1:])

View File

@ -0,0 +1,24 @@
#!/bin/python3
import os
import sys
import argparse
import logging
import json
import requests
import re
from getpass import getpass
# Identity attributes
# {'username' : '', 'full_name' : '', 'email' : '', public_key : '' }
logging.basicConfig(format='%(levelname)s\t- %(message)s')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
state = {}
def createSession(args):
pass
if __name__ == '__main__':
createSession(sys.argv[1:])

View File

@ -0,0 +1,37 @@
#!/bin/python3
import os
import sys
import logging
logging.basicConfig(format='%(levelname)s\t- %(message)s')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
#send to stdout contents of decrypted file
# encrypted file - encryption metadata
def decryptFile(args):
if len(args) != 3:
logger.error("Need encrypted file and it's metadata.")
sys.exit(-1)
# If first argument is not a file or not found
if (not os.path.isfile(args[1])):
logger.error("File '" + args[1] + "' not found.")
sys.exit(-1)
if (not os.path.isfile(args[2])):
logger.error("File '" + args[2] + "' not found.")
sys.exit(-1)
#Get private key to decrypt
privateKey = ''
#Decrypt file
content = 'decrypt(privateKey, args[1])'
# Send decrypted content to stdout
sys.stdout.write(content)
if __name__ == '__main__':
decryptFile(sys.argv)

View File

@ -0,0 +1,42 @@
#!/bin/python3
import os
import sys
import logging
import json
import requests
# Identity attributes
# {'username' : '', 'full_name' : '', 'email' : '', public_key : '' }
logging.basicConfig(format='%(levelname)s\t- %(message)s')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
state = {}
#get file
#file handle - file
def getFile(args):
if len(args) < 1:
logger.error("Need a file handle.")
sys.exit(-1)
if len(args) == 2:
if (not os.path.isfile(args[1])):
logger.error("File '" + args[1] + "' not found.")
sys.exit(-1)
file = json.loads(requests.get(f'http://{state['REP_ADDRESS']}/file'), params = {"file_handle" : args[0]})
# decrypt file
content = '' #decrypt(file.encode('utf-8'))
if len(args) == 1:
sys.stdout.write(content)
else:
with open(args[1], "wb") as f:
f.write(content)
if __name__ == '__main__':
getFile(sys.argv[1:])

View File

@ -0,0 +1,29 @@
#!/bin/python3
import sys
import logging
import json
import requests
# Identity attributes
# {'username' : '', 'full_name' : '', 'email' : '', public_key : '' }
logging.basicConfig(format='%(levelname)s\t- %(message)s')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
state = {}
def listOrganizations():
try:
orgs = json.loads(requests.get(f'http://{state['REP_ADDRESS']}/organization/list'))
orgs.raise_for_status()
except requests.exceptions.RequestException as errex:
logger.error("Failed to obtain response from server.")
sys.exit(1)
for org in orgs:
sys.stdout.write(org['id'] + " - " + org['name'])
if __name__ == '__main__':
listOrganizations(sys.argv[1:])

View File

@ -0,0 +1,31 @@
#!/bin/python3
import sys
import logging
logging.basicConfig(format='%(levelname)s\t- %(message)s')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# Generate a key pair for a subject
# password - file for keys
def generateKeyPair(args):
if len(args) != 3:
logger.error("Need password and file to store keys")
sys.exit(-1)
#Generate the key pair
keyPair = ''
#Get the 2 different keys
pubKey = ''
privateKey = ''
with open(args[2], "wb") as f:
f.write(pubKey.encode() + b"\n\n" + privateKey.encode())
if __name__ == '__main__':
generateKeyPair(sys.argv)