-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
106 lines (74 loc) · 2.97 KB
/
auth.py
File metadata and controls
106 lines (74 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import getpass
import keyring
def load_account():
active = keyring.get_password("CLI-Inbox", "active") or ""
if not active:
return None
return active
def setup_account():
print("No account found. Please set up your Gmail account.")
print("Note: You need a Gmail App Password, not your regular password. Get one here: https://myaccount.google.com/apppasswords")
email = input("Email: ").strip()
password = getpass.getpass("App Password: ")
accounts = keyring.get_password("CLI-Inbox", "accounts") or ""
accounts_list = []
for account in accounts.split(","):
if account:
accounts_list.append(account)
if email not in accounts_list:
accounts_list.append(email)
keyring.set_password("CLI-Inbox", "accounts", ",".join(accounts_list))
keyring.set_password("CLI-Inbox", f"{email}:password", password)
keyring.set_password("CLI-Inbox", "active", email)
return email, password
def add_account():
print("Add a new Gmail account.")
print("Note: You need a Gmail App Password, not your regular password. Get one here: https://myaccount.google.com/apppasswords")
email = input("Email: ").strip()
if email in list_accounts():
print("Account already added.")
return None, None
password = getpass.getpass("App Password: ")
accounts = keyring.get_password("CLI-Inbox", "accounts") or ""
accounts_list = []
for account in accounts.split(","):
if account:
accounts_list.append(account)
if email not in accounts_list:
accounts_list.append(email)
keyring.set_password("CLI-Inbox", f"{email}:password", password)
keyring.set_password("CLI-Inbox", "accounts", ",".join(accounts_list))
return email, password
def remove_account(email):
accounts = keyring.get_password("CLI-Inbox", "accounts") or ""
accounts_list = []
for account in accounts.split(","):
if account:
accounts_list.append(account)
if email not in accounts_list:
return False
accounts_list.remove(email)
keyring.set_password("CLI-Inbox", "accounts", ",".join(accounts_list))
keyring.delete_password("CLI-Inbox", f"{email}:password")
active = keyring.get_password("CLI-Inbox", "active")
if active == email:
if accounts_list:
keyring.set_password("CLI-Inbox", "active", accounts_list[0])
else:
keyring.delete_password("CLI-Inbox", "active")
return True
def get_password(email):
password = keyring.get_password("CLI-Inbox", f"{email}:password")
return password
def list_accounts():
accounts = keyring.get_password("CLI-Inbox", "accounts") or ""
account_list = []
for account in accounts.split(","):
if account:
account_list.append(account)
return account_list
def active(email):
if email not in list_accounts():
return False
keyring.set_password("CLI-Inbox", "active", email)
return True