Move token generation function

main
Bob Mottram 2021-07-26 10:40:51 +01:00
parent 47575a52a7
commit 646254c8a9
3 changed files with 33 additions and 32 deletions

31
auth.py
View File

@ -148,37 +148,6 @@ def authorizeBasic(baseDir: str, path: str, authHeader: str,
return False return False
def generateSharedItemFederationTokens(sharedItemsFederatedDomains: [],
baseDir: str) -> None:
"""Generates tokens for shared item federated domains
"""
if not sharedItemsFederatedDomains:
return
tokensFile = baseDir + '/accounts/sharedItemsFederationTokens'
if not os.path.isfile(tokensFile):
with open(tokensFile, 'w+') as fp:
fp.write('')
tokens = []
with open(tokensFile, 'r') as fp:
tokens = fp.read().split('\n')
tokensAdded = False
for domain in sharedItemsFederatedDomains:
domainFound = False
for line in tokens:
if line.startswith(domain + ':'):
domainFound = True
break
if not domainFound:
newLine = domain + ':' + secrets.token_urlsafe(64)
tokens.append(newLine)
tokensAdded = True
if not tokensAdded:
return
with open(tokensFile, 'w+') as fp:
for line in tokens:
fp.write(line + '\n')
def authorizeSharedItems(sharedItemsFederatedDomains: [], def authorizeSharedItems(sharedItemsFederatedDomains: [],
baseDir: str, baseDir: str,
callingDomain: str, callingDomain: str,

View File

@ -103,7 +103,6 @@ from skills import noOfActorSkills
from skills import actorHasSkill from skills import actorHasSkill
from skills import actorSkillValue from skills import actorSkillValue
from skills import setActorSkillLevel from skills import setActorSkillLevel
from auth import generateSharedItemFederationTokens
from auth import recordLoginFailure from auth import recordLoginFailure
from auth import authorize from auth import authorize
from auth import authorizeSharedItems from auth import authorizeSharedItems
@ -205,6 +204,7 @@ from webapp_welcome import htmlWelcomeScreen
from webapp_welcome import isWelcomeScreenComplete from webapp_welcome import isWelcomeScreenComplete
from webapp_welcome_profile import htmlWelcomeProfile from webapp_welcome_profile import htmlWelcomeProfile
from webapp_welcome_final import htmlWelcomeFinal from webapp_welcome_final import htmlWelcomeFinal
from shares import generateSharedItemFederationTokens
from shares import getSharesFeedForPerson from shares import getSharesFeedForPerson
from shares import addShare from shares import addShare
from shares import removeSharedItem from shares import removeSharedItem

View File

@ -9,6 +9,7 @@ __module_group__ = "Timeline"
import os import os
import re import re
import secrets
import time import time
import datetime import datetime
from webfinger import webfingerHandle from webfinger import webfingerHandle
@ -940,3 +941,34 @@ def sharesCatalogCSVEndpoint(baseDir: str, httpPrefix: str,
csvStr += item['DFC:Image'] + ',' csvStr += item['DFC:Image'] + ','
csvStr += item['DFC:description'] + '\n' csvStr += item['DFC:description'] + '\n'
return csvStr return csvStr
def generateSharedItemFederationTokens(sharedItemsFederatedDomains: [],
baseDir: str) -> None:
"""Generates tokens for shared item federated domains
"""
if not sharedItemsFederatedDomains:
return
tokensFile = baseDir + '/accounts/sharedItemsFederationTokens'
if not os.path.isfile(tokensFile):
with open(tokensFile, 'w+') as fp:
fp.write('')
tokens = []
with open(tokensFile, 'r') as fp:
tokens = fp.read().split('\n')
tokensAdded = False
for domain in sharedItemsFederatedDomains:
domainFound = False
for line in tokens:
if line.startswith(domain + ':'):
domainFound = True
break
if not domainFound:
newLine = domain + ':' + secrets.token_urlsafe(64)
tokens.append(newLine)
tokensAdded = True
if not tokensAdded:
return
with open(tokensFile, 'w+') as fp:
for line in tokens:
fp.write(line + '\n')