2019-03-12 15:06:52 +00:00
|
|
|
from password import Password
|
2019-03-12 12:27:34 +00:00
|
|
|
from pwdfile import Pwdfile
|
2019-03-11 16:29:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Credentials:
|
|
|
|
def __init__(self):
|
2019-03-12 12:27:34 +00:00
|
|
|
self.creds = Pwdfile().read()
|
2019-03-11 16:29:14 +00:00
|
|
|
|
2019-03-12 14:57:58 +00:00
|
|
|
def get_creds(self):
|
2019-03-12 12:27:34 +00:00
|
|
|
return self.creds
|
|
|
|
|
2019-03-12 14:57:58 +00:00
|
|
|
def read_cred(self, service):
|
2019-03-12 12:27:34 +00:00
|
|
|
return self.creds[service]
|
|
|
|
|
2019-03-12 15:49:52 +00:00
|
|
|
def expose_cred(self, service):
|
|
|
|
decrypted = {
|
|
|
|
"username": self.creds[service]["username"],
|
|
|
|
"password": Password().decrypt(
|
|
|
|
self.creds[service]["password"].encode()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return decrypted
|
|
|
|
|
2019-03-12 15:06:52 +00:00
|
|
|
def create_cred(self, service, username, password=None):
|
|
|
|
if password is None:
|
|
|
|
password = Password.generate(mn=64)
|
|
|
|
|
2019-03-12 12:27:34 +00:00
|
|
|
new_entry = {
|
|
|
|
"username": username,
|
2019-03-12 15:49:52 +00:00
|
|
|
"password": Password().encrypt(password).decode()
|
2019-03-12 12:27:34 +00:00
|
|
|
}
|
|
|
|
self.creds[service] = new_entry
|
|
|
|
Pwdfile().write(self.creds)
|
|
|
|
|
2019-03-12 14:57:58 +00:00
|
|
|
def update_cred(self, service, username=None, password=None):
|
2019-03-12 12:27:34 +00:00
|
|
|
current_entry = self.creds[service]
|
|
|
|
if username is None and password is not None:
|
|
|
|
self.creds[service] = {
|
|
|
|
"username": current_entry["username"],
|
2019-03-12 15:32:12 +00:00
|
|
|
"password": Password().encrypt(password).decode()
|
2019-03-12 12:27:34 +00:00
|
|
|
}
|
|
|
|
elif username is not None and password is None:
|
|
|
|
self.creds[service] = {
|
|
|
|
"username": username,
|
|
|
|
"password": current_entry["password"]
|
|
|
|
}
|
|
|
|
Pwdfile().write(self.creds)
|
|
|
|
|
2019-03-12 14:57:58 +00:00
|
|
|
def delete_cred(self, service):
|
2019-03-12 12:27:34 +00:00
|
|
|
del self.creds[service]
|
|
|
|
Pwdfile().write(self.creds)
|
|
|
|
|