forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_manager.py
161 lines (136 loc) · 4.84 KB
/
password_manager.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import sqlite3
from getpass import getpass
import os
# set the environment variable ADMIN_PASS to your desired string, which will be your password.
ADMIN_PASSWORD = os.environ["ADMIN_PASS"]
connect = getpass("What is your admin password?\n")
while connect != ADMIN_PASSWORD:
connect = getpass("What is your admin password?\n")
if connect == "q":
break
conn = sqlite3.connect("password_manager.db")
cursor_ = conn.cursor()
def get_password(service_):
command = 'SELECT * from STORE WHERE SERVICE = "' + service_ + '"'
cursor = conn.execute(command)
for row in cursor:
username_ = row[1]
password_ = row[2]
return [username_, password_]
def add_password(service_, username_, password_):
command = (
'INSERT INTO STORE (SERVICE,USERNAME,PASSWORD) VALUES("'
+ service_
+ '","'
+ username_
+ '","'
+ password_
+ '");'
)
conn.execute(command)
conn.commit()
def update_password(service_, password_):
command = (
'UPDATE STORE set PASSWORD = "'
+ password_
+ '" where SERVICE = "'
+ service_
+ '"'
)
conn.execute(command)
conn.commit()
print(service_ + " password updated successfully.")
def delete_service(service_):
command = 'DELETE from STORE where SERVICE = "' + service_ + '"'
conn.execute(command)
conn.commit()
print(service_ + " deleted from the database successfully.")
def get_all():
cursor_.execute("SELECT * from STORE")
data = cursor_.fetchall()
if len(data) == 0:
print("No Data")
else:
for row in data:
print("service = ", row[0])
print("username = ", row[1])
print("password = ", row[2])
print()
def is_service_present(service_):
cursor_.execute("SELECT SERVICE from STORE where SERVICE = ?", (service_,))
data = cursor_.fetchall()
if len(data) == 0:
print("There is no service named %s" % service_)
return False
else:
return True
if connect == ADMIN_PASSWORD:
try:
conn.execute(
"""CREATE TABLE STORE
(SERVICE TEXT PRIMARY KEY NOT NULL,
USERNAME TEXT NOT NULL,
PASSWORD TEXT NOT NULL);
"""
)
print("Your safe has been created!\nWhat would you like to store in it today?")
except:
print("You have a safe, what would you like to do today?")
while True:
print("\n" + "*" * 15)
print("Commands:")
print("quit = quit program")
print("get = get username and password")
print("getall = show all the details in the database")
print("store = store username and password")
print("update = update password")
print("delete = delete a service details")
print("*" * 15)
input_ = input(":")
if input_ == "quit":
print("\nGoodbye, have a great day.\n")
conn.close()
break
elif input_ == "store":
service = input("What is the name of the service?\n")
cursor_.execute("SELECT SERVICE from STORE where SERVICE = ?", (service,))
data = cursor_.fetchall()
if len(data) == 0:
username = input("Enter username : ")
password = getpass("Enter password : ")
if username == "" or password == "":
print("Your username or password is empty.")
else:
add_password(service, username, password)
print("\n" + service.capitalize() + " password stored\n")
else:
print("Service named {} already exists.".format(service))
elif input_ == "get":
service = input("What is the name of the service?\n")
flag = is_service_present(service)
if flag:
username, password = get_password(service)
print(service.capitalize() + " Details")
print("Username : ", username)
print("Password : ", password)
elif input_ == "update":
service = input("What is the name of the service?\n")
if service == "":
print("Service is not entered.")
else:
flag = is_service_present(service)
if flag:
password = getpass("Enter new password : ")
update_password(service, password)
elif input_ == "delete":
service = input("What is the name of the service?\n")
if service == "":
print("Service is not entered.")
else:
flag = is_service_present(service)
if flag:
delete_service(service)
elif input_ == "getall":
get_all()
else:
print("Invalid command.")