Switched from incrementing numeric ID's to UUID's for later security features.

This commit is contained in:
Innovation 2024-03-29 09:43:12 -05:00
parent 6c4d2b1eaa
commit 93e474d581

View file

@ -1,5 +1,6 @@
from flask import Flask, render_template, jsonify, request from flask import Flask, render_template, jsonify, request
from copy import copy from copy import copy
import uuid
app = Flask(__name__) app = Flask(__name__)
# Vitals # Vitals
@ -111,33 +112,24 @@ def setFitness():
# Cyberware management # Cyberware management
currentId = 1
cyberware = [] cyberware = []
newCyberwareTemplate = { "id": -1, "name": "", "hotpluggable": False, "lastMalfunction": "", "frontendAwknowledge": False } newCyberwareTemplate = { "uuid": -1, "name": "", "hotpluggable": False, "lastMalfunction": "", "frontendAwknowledge": False, "canSet": [] }
# VERY descriptive variable names, huh?
#cyberwareIdIndex = 0
#cyberwareNameIndex = 1
#cyberwareHotpluggableIndex = 2
#cyberwareLastMalfunction = 3
#cyberwareFrontwareAwknowledge = 4
# This makes the system aware of a new piece of hardware. # This makes the system aware of a new piece of hardware.
# While, for the most part, not required due to the design of this project, # While, for the most part, not required due to the design of this project,
# it's handy for error reporting and communication between hardware and # it's handy for error reporting and communication between hardware and
# the end user. # the end user.
# #
# Arguments: { name: STRING, hotpluggable: BOOL } # Arguments: { name: STRING, hotpluggable: BOOL, canSet: ARRAY }
# Returns: { id: INT } # Returns: { uuid: INT }
# name: A human-readable name # name: A human-readable name
# hotpluggable argument: Used for removable modules known as shards. # hotpluggable argument: Used for removable modules known as shards.
# #
# id: Returns an id of the hardware. # uuid: Returns a uuid for the hardware.
@app.route('/api/cyberware/add', methods=['POST']) @app.route('/api/cyberware/add', methods=['POST'])
def addCyberware(): def addCyberware():
global cyberware global cyberware
global currentId
json = request.get_json() json = request.get_json()
try: try:
@ -145,46 +137,59 @@ def addCyberware():
tempHotpluggable = json['hotpluggable'] tempHotpluggable = json['hotpluggable']
tempNewCyberware = copy(newCyberwareTemplate) tempNewCyberware = copy(newCyberwareTemplate)
tempNewCyberware['id'] = currentId tempNewCyberware['uuid'] = str(uuid.uuid4())
tempNewCyberware['name'] = tempName tempNewCyberware['name'] = tempName
tempNewCyberware['hotpluggable'] = tempHotpluggable tempNewCyberware['hotpluggable'] = tempHotpluggable
currentId = currentId + 1
cyberware.append(tempNewCyberware) cyberware.append(tempNewCyberware)
except: except:
return 'Incorrect usage.\nUsage: { name: STRING, hotpluggable: BOOL }\n', 400 return 'Incorrect usage.\nUsage: { name: STRING, hotpluggable: BOOL, canSet: ARRAY }\n', 400
return jsonify([ { "id": currentId-1 } ]), 204 return jsonify([ { "uuid": tempNewCyberware['uuid'] } ]), 204
# Arguments: { id: INT } # Arguments: { uuid: INT }
@app.route('/api/cyberware/remove', methods=['POST']) @app.route('/api/cyberware/remove', methods=['POST'])
def removeCyberware(): def removeCyberware():
global cyberware global cyberware
json = request.get_json() json = request.get_json()
try: try:
desiredId = json['id'] desiredId = json['uuid']
i = 0 i = 0
for c in cyberware: for c in cyberware:
if c['id'] == desiredId: if c['uuid'] == desiredId:
del cyberware[i] del cyberware[i]
return 'Cyberware removed', 204 return 'Cyberware removed', 204
i = i + 1 i = i + 1
except: except:
return 'Incorrect usage.\nUsage: { id: INT }\n', 400 return 'Incorrect usage.\nUsage: { uuid: STRING }\n', 400
return 'ID Invalid\n', 400 return 'UUID Invalid\n', 400
# Returns: { id: INT, name: STRING, hotpluggable: BOOL, lastMalfunction: STRING, frontendAwknowledge: BOOL } # Returns: { uuid: STRING, name: STRING, hotpluggable: BOOL, lastMalfunction: STRING, frontendAwknowledge: BOOL }
# id: Unique identifier of the hardware # uuid: Unique identifier of the hardware
# name: Human-readable name # name: Human-readable name
# hotpluggable: Hardware can be removed during runtime. # hotpluggable: Hardware can be removed during runtime.
# lastMalfunction: A string with information on the last malfunction. # lastMalfunction: A string with information on the last malfunction.
# frontendAwknowlege: A tool for the frontend to keep track of whether it's aware of the hardware. # frontendAwknowlege: A tool for the frontend to keep track of whether it's aware of the hardware.
# TODO: If this method is to exist, it needs to return everything WITHOUT UUID's.
@app.route('/api/cyberware') @app.route('/api/cyberware')
def getCyberware(): def getCyberware():
return jsonify(cyberware) return 'Not implemented', 501
#return jsonify(cyberware)
# Arguments { id: INT } @app.route('/api/cyberware/get')
def getCyberwareSpecific():
try:
desiredId = json['uuid']
i = 0
for c in cyberware:
if c['uuid'] == desiredId:
return jsonify(c), 204
break;
except:
return 'Incorrect usage.\nUsage: { uuid: STRING }\n', 400
return 'UUID Invalid\n', 400
# Arguments { uuid: INT }
@app.route('/api/cyberware/reset', methods=['POST']) @app.route('/api/cyberware/reset', methods=['POST'])
def resetCyberwareMalfunction(): def resetCyberwareMalfunction():
return 'Not Implemented', 501 return 'Not Implemented', 501