Skip to content

Commit 5c4c975

Browse files
RohanBhattaraiNPRohan Bhattarai
andauthored
Swtiched to encrypted token (#33)
* switch to encrypted token and support multiple files --------- Co-authored-by: Rohan Bhattarai <[email protected]>
1 parent affa5b2 commit 5c4c975

File tree

1 file changed

+74
-37
lines changed

1 file changed

+74
-37
lines changed

caltechdata_api/cli.py

Lines changed: 74 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from .md_to_json import parse_readme_to_json
66
import json
77
import os
8-
import configparser
8+
#import configparser
9+
from cryptography.fernet import Fernet
910

1011
CALTECHDATA_API = "https://data.caltech.edu/api/names?q=identifiers.identifier:{}"
1112
ORCID_API = "https://orcid.org/"
@@ -22,30 +23,61 @@
2223
funderName = ""
2324

2425

25-
CONFIG_FILE = "caltechdata_config.ini"
26+
#CONFIG_FILE = "caltechdata_config.ini"
2627

2728

28-
def get_or_set_token():
29-
config = configparser.ConfigParser()
29+
home_directory = os.path.expanduser("~")
30+
caltechdata_directory = os.path.join(home_directory, ".caltechdata")
31+
32+
33+
if not os.path.exists(caltechdata_directory):
34+
os.makedirs(caltechdata_directory)
3035

31-
if os.path.isfile(CONFIG_FILE):
32-
config.read(CONFIG_FILE)
33-
if "CaltechDATA" in config and "token" in config["CaltechDATA"]:
34-
return config["CaltechDATA"]["token"]
36+
def generate_key():
37+
return Fernet.generate_key()
38+
39+
# Load the key from a file or generate a new one if not present
40+
def load_or_generate_key(key_file="key.key"):
41+
if os.path.exists(key_file):
42+
with open(key_file, "rb") as f:
43+
return f.read()
3544
else:
45+
key = generate_key()
46+
with open(key_file, "wb") as f:
47+
f.write(key)
48+
return key
49+
50+
# Encrypt the token
51+
def encrypt_token(token, key):
52+
f = Fernet(key)
53+
return f.encrypt(token.encode())
54+
55+
# Decrypt the token
56+
def decrypt_token(encrypted_token, key):
57+
f = Fernet(key)
58+
return f.decrypt(encrypted_token).decode()
59+
60+
# Function to get or set token
61+
def get_or_set_token():
62+
63+
key = load_or_generate_key()
64+
try:
65+
with open("token.txt", "rb") as f:
66+
encrypted_token = f.read()
67+
token = decrypt_token(encrypted_token, key)
68+
return token
69+
except FileNotFoundError:
3670
while True:
37-
token = get_user_input("Enter your CaltechDATA token: ")
38-
confirm_token = get_user_input("Confirm your CaltechDATA token: ")
71+
token = input("Enter your CaltechDATA token: ").strip()
72+
confirm_token = input("Confirm your CaltechDATA token: ").strip()
3973
if token == confirm_token:
40-
config.add_section("CaltechDATA")
41-
config.set("CaltechDATA", "token", token)
42-
with open(CONFIG_FILE, "w") as configfile:
43-
config.write(configfile)
74+
encrypted_token = encrypt_token(token, key)
75+
with open("token.txt", "wb") as f:
76+
f.write(encrypted_token)
4477
return token
4578
else:
4679
print("Tokens do not match. Please try again.")
47-
48-
80+
4981
def welcome_message():
5082
print("Welcome to CaltechDATA CLI")
5183

@@ -218,6 +250,7 @@ def get_names(orcid):
218250

219251
def upload_supporting_file(record_id=None):
220252
filepath = ""
253+
filepaths = []
221254
file_link = ""
222255
while True:
223256
choice = get_user_input(
@@ -264,34 +297,38 @@ def upload_supporting_file(record_id=None):
264297
f for f in os.listdir() if not f.endswith(".json") and os.path.isfile(f)
265298
]
266299
print("\n".join(files))
267-
filename = get_user_input(
268-
"Enter the filename to upload as a supporting file: "
269-
)
270-
if filename in files:
271-
file_size = os.path.getsize(filename)
272-
if file_size > 1024 * 1024 * 1024:
273-
file_link = get_user_input(
274-
"Enter the S3 link to the file (File size is more than 1GB): "
275-
)
276-
if file_link:
277-
return filepath, file_link
300+
while True:
301+
filename = get_user_input(
302+
"Enter the filename to upload as a supporting file (or 'n' to finish): "
303+
)
304+
if filename == "n":
305+
break
306+
if filename in files:
307+
file_size = os.path.getsize(filename)
308+
if file_size > 1024 * 1024 * 1024:
309+
file_link = get_user_input(
310+
"Enter the S3 link to the file (File size is more than 1GB): "
311+
)
312+
if file_link:
313+
file_links.append(file_link)
278314
else:
279-
print("Link is required for files larger than 1GB.")
280-
continue
315+
filepath = os.path.abspath(filename)
316+
filepaths.append(filepath)
281317
else:
282-
filepath = os.path.abspath(filename)
283-
break
284-
else:
285-
print(
286-
f"Error: File '{filename}' not found. Please enter a valid filename."
287-
)
318+
print(
319+
f"Error: File '{filename}' not found. Please enter a valid filename."
320+
)
321+
322+
add_more = get_user_input("Do you want to add more files? (y/n): ").lower()
323+
if add_more != "y":
324+
break
325+
288326
elif choice == "n":
289327
break
290328
else:
291329
print("Invalid input. Please enter 'link' or 'upload' or 'n'.")
292330

293-
return filepath, file_link
294-
331+
return filepaths, file_links
295332

296333
def upload_data_from_file():
297334
while True:

0 commit comments

Comments
 (0)