passwdtools/credentials.py

53 lines
1.5 KiB
Python
Raw Normal View History

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
def get_creds(self):
2019-03-12 12:27:34 +00:00
return self.creds
def read_cred(self, service):
2019-03-12 12:27:34 +00:00
return self.creds[service]
def expose_cred(self, service):
decrypted = {
"username": self.creds[service]["username"],
"password": Password().decrypt(
self.creds[service]["password"].encode()
)
}
return decrypted
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,
"password": Password().encrypt(password).decode()
2019-03-12 12:27:34 +00:00
}
self.creds[service] = new_entry
Pwdfile().write(self.creds)
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)
def delete_cred(self, service):
2019-03-12 12:27:34 +00:00
del self.creds[service]
Pwdfile().write(self.creds)