Skip to content

Commit

Permalink
Lazy Keyring intialization for PasswordManager (#1892)
Browse files Browse the repository at this point in the history
  • Loading branch information
k4nar authored and sdispater committed Jan 20, 2020
1 parent a0c9357 commit 380e09b
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions poetry/utils/password_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,35 +117,40 @@ def _check(self):
class PasswordManager:
def __init__(self, config):
self._config = config
self._keyring = KeyRing("poetry-repository")
if not self._keyring.is_available():
logger.warning("Using a plaintext file to store and retrieve credentials")
self._keyring = None

@property
def keyring(self):
if self._keyring is None:
self._keyring = KeyRing("poetry-repository")
if not self._keyring.is_available():
logger.warning(
"Using a plaintext file to store and retrieve credentials"
)

return self._keyring

def set_pypi_token(self, name, token):
if not self._keyring.is_available():
if not self.keyring.is_available():
self._config.auth_config_source.add_property(
"pypi-token.{}".format(name), token
)
else:
self._keyring.set_password(name, "__token__", token)
self.keyring.set_password(name, "__token__", token)

def get_pypi_token(self, name):
if not self._keyring.is_available():
if not self.keyring.is_available():
return self._config.get("pypi-token.{}".format(name))

return self._keyring.get_password(name, "__token__")
return self.keyring.get_password(name, "__token__")

def delete_pypi_token(self, name):
if not self._keyring.is_available():
if not self.keyring.is_available():
return self._config.auth_config_source.remove_property(
"pypi-token.{}".format(name)
)

self._keyring.delete_password(name, "__token__")
self.keyring.delete_password(name, "__token__")

def get_http_auth(self, name):
auth = self._config.get("http-basic.{}".format(name))
Expand All @@ -154,7 +159,7 @@ def get_http_auth(self, name):

username, password = auth["username"], auth.get("password")
if password is None:
password = self._keyring.get_password(name, username)
password = self.keyring.get_password(name, username)

return {
"username": username,
Expand All @@ -164,10 +169,10 @@ def get_http_auth(self, name):
def set_http_password(self, name, username, password):
auth = {"username": username}

if not self._keyring.is_available():
if not self.keyring.is_available():
auth["password"] = password
else:
self._keyring.set_password(name, username, password)
self.keyring.set_password(name, username, password)

self._config.auth_config_source.add_property("http-basic.{}".format(name), auth)

Expand All @@ -177,7 +182,7 @@ def delete_http_password(self, name):
return

try:
self._keyring.delete_password(name, auth["username"])
self.keyring.delete_password(name, auth["username"])
except KeyRingError:
pass

Expand Down

0 comments on commit 380e09b

Please sign in to comment.