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 copy import copy
import uuid
app = Flask(__name__)
# Vitals
@ -111,33 +112,24 @@ def setFitness():
# Cyberware management
currentId = 1
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.
# 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
# the end user.
#
# Arguments: { name: STRING, hotpluggable: BOOL }
# Returns: { id: INT }
# Arguments: { name: STRING, hotpluggable: BOOL, canSet: ARRAY }
# Returns: { uuid: INT }
# name: A human-readable name
# 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'])
def addCyberware():
global cyberware
global currentId
json = request.get_json()
try:
@ -145,46 +137,59 @@ def addCyberware():
tempHotpluggable = json['hotpluggable']
tempNewCyberware = copy(newCyberwareTemplate)
tempNewCyberware['id'] = currentId
tempNewCyberware['uuid'] = str(uuid.uuid4())
tempNewCyberware['name'] = tempName
tempNewCyberware['hotpluggable'] = tempHotpluggable
currentId = currentId + 1
cyberware.append(tempNewCyberware)
except:
return 'Incorrect usage.\nUsage: { name: STRING, hotpluggable: BOOL }\n', 400
return jsonify([ { "id": currentId-1 } ]), 204
return 'Incorrect usage.\nUsage: { name: STRING, hotpluggable: BOOL, canSet: ARRAY }\n', 400
return jsonify([ { "uuid": tempNewCyberware['uuid'] } ]), 204
# Arguments: { id: INT }
# Arguments: { uuid: INT }
@app.route('/api/cyberware/remove', methods=['POST'])
def removeCyberware():
global cyberware
json = request.get_json()
try:
desiredId = json['id']
desiredId = json['uuid']
i = 0
for c in cyberware:
if c['id'] == desiredId:
if c['uuid'] == desiredId:
del cyberware[i]
return 'Cyberware removed', 204
i = i + 1
except:
return 'Incorrect usage.\nUsage: { id: INT }\n', 400
return 'ID Invalid\n', 400
return 'Incorrect usage.\nUsage: { uuid: STRING }\n', 400
return 'UUID Invalid\n', 400
# Returns: { id: INT, name: STRING, hotpluggable: BOOL, lastMalfunction: STRING, frontendAwknowledge: BOOL }
# id: Unique identifier of the hardware
# Returns: { uuid: STRING, name: STRING, hotpluggable: BOOL, lastMalfunction: STRING, frontendAwknowledge: BOOL }
# uuid: Unique identifier of the hardware
# name: Human-readable name
# hotpluggable: Hardware can be removed during runtime.
# 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.
# TODO: If this method is to exist, it needs to return everything WITHOUT UUID's.
@app.route('/api/cyberware')
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'])
def resetCyberwareMalfunction():
return 'Not Implemented', 501