-
Notifications
You must be signed in to change notification settings - Fork 0
/
secureAuth.py
46 lines (34 loc) · 1.51 KB
/
secureAuth.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
from server import Server
import keyring
class Auth:
def __init__(self, ip, user, folder, password=None, service_name="musicSMBsync",
store_credential=False, name="DEFAULT", port=139, ntlm=True):
self.name = name
self.ip = ip
self.user = user
# If required get password from Windows Credential Manager or from another supported keyring
if not password or password is None:
password = Auth.get_password(service_name, self.user)
assert password, "No password stored for service {} and user {}".format(service_name, self.user)
else:
if store_credential and service_name:
Auth.store_password(service_name, self.user, password)
self.server = Server(self.ip, self.user, password, folder, name=self.name, port=port, ntlm=ntlm)
def __str__(self):
return "Authenticator: SMB server {} at {}, logged by user {}".format(self.name, self.ip, self.user)
def __bool__(self):
return self.server.status
def __call__(self, *args, **kwargs):
return self.server
def get_connection(self):
return self.server
@staticmethod
def get_password(service, user):
password = keyring.get_password(service, user)
if password is None:
return False
else:
return password
@staticmethod
def store_password(service, user, password):
keyring.set_password(service, user, password)