Create temporary dunikey if no dunikey

This commit is contained in:
poka 2020-12-02 09:54:11 +01:00
parent 7ea608c4fb
commit ba9e474ea7
1 changed files with 33 additions and 13 deletions

View File

@ -1,9 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse, sys, os import argparse, sys, os, random, string, getpass
from os.path import join, dirname from os.path import join, dirname
from shutil import copyfile from shutil import copyfile
from dotenv import load_dotenv from dotenv import load_dotenv
from duniterpy.key import SigningKey
from lib.cesium import ReadFromCesium, SendToCesium, DeleteFromCesium, Profiles from lib.cesium import ReadFromCesium, SendToCesium, DeleteFromCesium, Profiles
from lib.likes import ReadLikes, SendLikes, UnLikes from lib.likes import ReadLikes, SendLikes, UnLikes
@ -15,18 +16,6 @@ if not os.path.isfile('.env'):
dotenv_path = join(dirname(__file__), '.env') dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path) load_dotenv(dotenv_path)
dunikey = os.getenv('DUNIKEY')
pod = os.getenv('POD')
if not dunikey or not pod:
sys.stderr.write("Please fill the path of your private key (PubSec), and a Cesium ES address in .env file\n")
sys.exit(1)
if not os.path.isfile(dunikey):
HOME = os.getenv("HOME")
dunikey = HOME + os.getenv('DUNIKEY')
if not os.path.isfile(dunikey):
sys.stderr.write("File {0} doesn't exist.\n".format(dunikey))
sys.exit(1)
# Parse arguments # Parse arguments
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-v', '--version', action='store_true', help="Affiche la version actuelle du programme") parser.add_argument('-v', '--version', action='store_true', help="Affiche la version actuelle du programme")
@ -80,6 +69,34 @@ if args.version:
print(VERSION) print(VERSION)
sys.exit(0) sys.exit(0)
def createTmpDunikey():
# Generate pseudo-random nonce
nonce=[]
for i in range(32):
nonce.append(random.choice(string.ascii_letters + string.digits))
nonce = ''.join(nonce)
keyPath = "/tmp/secret.dunikey-" + nonce
key = SigningKey.from_credentials(getpass.getpass("Identifiant: "), getpass.getpass("Mot de passe: "), None)
key.save_pubsec_file(keyPath)
return keyPath
pod = os.getenv('POD')
if not pod:
pod="https://g1.data.le-sou.org"
dunikey = os.getenv('DUNIKEY')
if not dunikey:
keyPath = createTmpDunikey()
dunikey = keyPath
else:
keyPath = False
if not os.path.isfile(dunikey):
HOME = os.getenv("HOME")
dunikey = HOME + os.getenv('DUNIKEY')
# Build cesiumMessaging class # Build cesiumMessaging class
if sys.argv[1] == "read": if sys.argv[1] == "read":
messages = ReadFromCesium(dunikey, pod) messages = ReadFromCesium(dunikey, pod)
@ -125,3 +142,6 @@ elif sys.argv[1] == "unlike":
gchange = UnLikes(dunikey, pod) gchange = UnLikes(dunikey, pod)
gchange.unLike(args.profile) gchange.unLike(args.profile)
if keyPath:
os.remove(keyPath)