From ebac11f502e41c96f83a59be5e44878288e3eab4 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 26 Jul 2021 13:20:07 +0100 Subject: [PATCH] Invert logic --- daemon.py | 4 ++++ shares.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++-------- tests.py | 20 ++++++++++++------ 3 files changed, 70 insertions(+), 15 deletions(-) diff --git a/daemon.py b/daemon.py index 0f575338b..4651ae921 100644 --- a/daemon.py +++ b/daemon.py @@ -203,6 +203,7 @@ from webapp_welcome import htmlWelcomeScreen from webapp_welcome import isWelcomeScreenComplete from webapp_welcome_profile import htmlWelcomeProfile from webapp_welcome_final import htmlWelcomeFinal +from shares import createSharedItemFederationToken from shares import authorizeSharedItems from shares import generateSharedItemFederationTokens from shares import getSharesFeedForPerson @@ -15219,6 +15220,9 @@ def runDaemon(sharedItemsFederatedDomains: [], httpd.sharedItemFederationTokens = \ generateSharedItemFederationTokens(httpd.sharedItemsFederatedDomains, baseDir) + httpd.sharedItemFederationTokens = \ + createSharedItemFederationToken(baseDir, domain, + httpd.sharedItemFederationTokens) # load peertube instances from file into a list httpd.peertubeInstances = [] diff --git a/shares.py b/shares.py index 4e27d1f3c..8a4c1ebe9 100644 --- a/shares.py +++ b/shares.py @@ -216,7 +216,7 @@ def addShare(baseDir: str, sharesFilename = acctDir(baseDir, nickname, domain) + '/shares.json' sharesJson = {} if os.path.isfile(sharesFilename): - sharesJson = loadJson(sharesFilename) + sharesJson = loadJson(sharesFilename, 1, 2) duration = duration.lower() published = int(time.time()) @@ -304,7 +304,7 @@ def _expireSharesForAccount(baseDir: str, nickname: str, domain: str) -> None: sharesFilename = baseDir + '/accounts/' + handle + '/shares.json' if not os.path.isfile(sharesFilename): return - sharesJson = loadJson(sharesFilename) + sharesJson = loadJson(sharesFilename, 1, 2) if not sharesJson: return currTime = int(time.time()) @@ -788,7 +788,7 @@ def sharesCatalogAccountEndpoint(baseDir: str, httpPrefix: str, sharesFilename = acctDir(baseDir, nickname, domain) + '/shares.json' if not os.path.isfile(sharesFilename): return endpoint - sharesJson = loadJson(sharesFilename) + sharesJson = loadJson(sharesFilename, 1, 2) if not sharesJson: return endpoint @@ -871,7 +871,7 @@ def sharesCatalogEndpoint(baseDir: str, httpPrefix: str, acctDir(baseDir, nickname, domain) + '/shares.json' if not os.path.isfile(sharesFilename): continue - sharesJson = loadJson(sharesFilename) + sharesJson = loadJson(sharesFilename, 1, 2) if not sharesJson: continue @@ -949,19 +949,21 @@ def generateSharedItemFederationTokens(sharedItemsFederatedDomains: [], """Generates tokens for shared item federated domains """ if not sharedItemsFederatedDomains: - return + return {} tokensJson = {} if baseDir: tokensFilename = \ baseDir + '/accounts/sharedItemsFederationTokens.json' - if not os.path.isfile(tokensFilename): - tokensJson = loadJson(tokensFilename) + if os.path.isfile(tokensFilename): + tokensJson = loadJson(tokensFilename, 1, 2) + if tokensJson is None: + tokensJson = {} tokensAdded = False for domain in sharedItemsFederatedDomains: if not tokensJson.get(domain): - tokensJson[domain] = secrets.token_urlsafe(64) + tokensJson[domain] = '' tokensAdded = True if not tokensAdded: @@ -971,6 +973,47 @@ def generateSharedItemFederationTokens(sharedItemsFederatedDomains: [], return tokensJson +def updateSharedItemFederationToken(baseDir: str, + tokenDomain: str, newToken: str, + tokensJson: {} = None) -> {}: + """Updates a token for shared item federation + """ + if not tokensJson: + tokensJson = {} + if baseDir: + tokensFilename = \ + baseDir + '/accounts/sharedItemsFederationTokens.json' + if os.path.isfile(tokensFilename): + tokensJson = loadJson(tokensFilename, 1, 2) + if tokensJson is None: + tokensJson = {} + tokensJson[tokenDomain] = newToken + if baseDir: + saveJson(tokensJson, tokensFilename) + return tokensJson + + +def createSharedItemFederationToken(baseDir: str, + tokenDomain: str, + tokensJson: {} = None) -> {}: + """Updates a token for shared item federation + """ + if not tokensJson: + tokensJson = {} + if baseDir: + tokensFilename = \ + baseDir + '/accounts/sharedItemsFederationTokens.json' + if os.path.isfile(tokensFilename): + tokensJson = loadJson(tokensFilename, 1, 2) + if tokensJson is None: + tokensJson = {} + if not tokensJson.get(tokenDomain): + tokensJson[tokenDomain] = secrets.token_urlsafe(64) + if baseDir: + saveJson(tokensJson, tokensFilename) + return tokensJson + + def authorizeSharedItems(sharedItemsFederatedDomains: [], baseDir: str, callingDomain: str, @@ -1009,7 +1052,7 @@ def authorizeSharedItems(sharedItemsFederatedDomains: [], print('DEBUG: shared item federation tokens file missing ' + tokensFilename) return False - tokensJson = loadJson(tokensFilename) + tokensJson = loadJson(tokensFilename, 1, 2) if not tokensJson: return False if not tokensJson.get(callingDomain): diff --git a/tests.py b/tests.py index b8203b0ff..229be046d 100644 --- a/tests.py +++ b/tests.py @@ -130,6 +130,8 @@ from languages import getLinksFromContent from languages import addLinksToContent from shares import authorizeSharedItems from shares import generateSharedItemFederationTokens +from shares import createSharedItemFederationToken +from shares import updateSharedItemFederationToken testServerAliceRunning = False testServerBobRunning = False @@ -4276,19 +4278,25 @@ def _testAuthorizeSharedItems(): ['dog.domain', 'cat.domain', 'birb.domain'] tokensJson = \ generateSharedItemFederationTokens(sharedItemsFederatedDomains, None) + tokensJson = \ + createSharedItemFederationToken(None, 'cat.domain', tokensJson) assert tokensJson - assert tokensJson.get('dog.domain') + assert not tokensJson.get('dog.domain') assert tokensJson.get('cat.domain') - assert tokensJson.get('birb.domain') - assert len(tokensJson['dog.domain']) >= 64 + assert not tokensJson.get('birb.domain') + assert len(tokensJson['dog.domain']) == 0 assert len(tokensJson['cat.domain']) >= 64 - assert len(tokensJson['birb.domain']) >= 64 + assert len(tokensJson['birb.domain']) == 0 assert not authorizeSharedItems(sharedItemsFederatedDomains, None, - 'dog.domain', 'w' * 86, + 'cat.domain', 'M' * 86, False, tokensJson) assert authorizeSharedItems(sharedItemsFederatedDomains, None, - 'dog.domain', tokensJson['dog.domain'], + 'cat.domain', tokensJson['cat.domain'], False, tokensJson) + tokensJson = \ + updateSharedItemFederationToken(None, + 'dog.domain', 'testToken', tokensJson) + assert tokensJson['dog.domain'] == 'testToken' def runAllTests():