2023-01-18 15:25:22 +02:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
from json import dumps
|
|
|
|
from uuid import uuid4
|
|
|
|
from modules.database import col_apikeys
|
|
|
|
from modules.security import passEncode
|
|
|
|
|
|
|
|
# Args =====================================================================================================================================
|
|
|
|
parser = ArgumentParser(
|
|
|
|
prog = "Stardew Valley Sync API",
|
|
|
|
description = "Small subprogram made for API keys generation."
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument("-u", "--username", help="Enter username without input prompt", action="store")
|
2023-01-19 16:31:40 +02:00
|
|
|
parser.add_argument("-e", "--email", help="Enter email without input prompt", action="store")
|
2023-01-19 22:15:07 +02:00
|
|
|
parser.add_argument("-l", "--local", help="Do not save user's email to make it completely local and unrecoverable", action="store_true")
|
2023-01-18 15:25:22 +02:00
|
|
|
parser.add_argument("-j", "--json", help="Return output as a json. Username must be provided as an argument", action="store_true")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
#===========================================================================================================================================
|
|
|
|
|
|
|
|
username = input("Enter username: ") if args.username is None else args.username
|
|
|
|
|
2023-01-19 16:31:40 +02:00
|
|
|
if args.local is False:
|
|
|
|
email = input("Enter email: ") if args.email is None else args.email
|
|
|
|
if email.strip() == "":
|
|
|
|
email = None
|
|
|
|
else:
|
|
|
|
email = None
|
|
|
|
|
2023-01-18 15:25:22 +02:00
|
|
|
new_key = str(uuid4())
|
2023-01-19 16:31:40 +02:00
|
|
|
col_apikeys.insert_one({"user": username, "email": email, "hash": passEncode(new_key)})
|
2023-01-18 15:25:22 +02:00
|
|
|
|
|
|
|
if args.json is True and args.username is not None:
|
|
|
|
print(dumps({"apikey": new_key}))
|
|
|
|
else:
|
|
|
|
print(f"Generated API key for {username}: {new_key}", flush=True)
|