From 33bbe7194001d930bc094f3b0df59913dc8f43cd Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 5 Aug 2020 14:06:04 +0000 Subject: [PATCH] Add and remove device functions --- devices.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/devices.py b/devices.py index 9fd97b45..81e74a4f 100644 --- a/devices.py +++ b/devices.py @@ -8,6 +8,56 @@ __status__ = "Production" import os from utils import loadJson +from utils import saveJson + + +def removeDevice(baseDir: str, nickname: str, domain: str, + deviceId: str) -> bool: + """Unregisters a device for e2ee + """ + personDir = baseDir + '/accounts/' + nickname + '@' + domain + deviceFilename = personDir + '/devices/' + deviceId + '.json' + if os.path.isfile(deviceFilename): + os.remove(deviceFilename) + return True + return False + + +def addDevice(baseDir: str, nickname: str, domain: str, + deviceId: str, name: str, claimUrl: str, + fingerprintPublicKey: str, + identityPublicKey: str, + fingerprintKeyType="Ed25519Key", + identityKeyType="Curve25519Key") -> bool: + """Registers a device for e2ee + claimUrl could be something like: + http://localhost:3000/users/admin/claim?id=11119 + """ + if ' ' in deviceId or '/' in deviceId or \ + '?' in deviceId or '#' in deviceId or \ + '.' in deviceId: + return False + personDir = baseDir + '/accounts/' + nickname + '@' + domain + if not os.path.isdir(personDir): + return False + if not os.path.isdir(personDir + '/devices'): + os.mkdir(personDir + '/devices') + deviceDict = { + "deviceId": deviceId, + "type": "Device", + "name": name, + "claim": claimUrl, + "fingerprintKey": { + "type": fingerprintKeyType, + "publicKeyBase64": fingerprintPublicKey + }, + "identityKey": { + "type": identityKeyType, + "publicKeyBase64": identityPublicKey + } + } + deviceFilename = personDir + '/devices/' + deviceId + '.json' + return saveJson(deviceDict, deviceFilename) def devicesCollection(baseDir: str, nickname: str, domain: str,