Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support username and password from keyring #1734

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Set password directly if keyring credentials are used
stollero committed Jan 27, 2025
commit 69e4596f226d7f5c8aa03a59f3524401a62049f0
6 changes: 2 additions & 4 deletions src/hatch/publish/auth.py
Original file line number Diff line number Diff line change
@@ -46,10 +46,6 @@ def __get_password(self) -> str:
if password is not None:
return password

password = keyring.get_password(self._repo, self.username)
if password is not None:
return password

if self._options['no_prompt']:
self._app.abort('Missing required option: auth')

@@ -77,6 +73,8 @@ def _read_keyring(self) -> str | None:
creds = keyring.get_credential(self._repo, None)
if not creds:
return None
self.__password = creds.password
self.__password_was_read = True
return creds.username

def _read_previous_working_user_data(self) -> str | None:
32 changes: 17 additions & 15 deletions tests/utils/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import pytest

import hatch.publish.auth
from hatch.publish.auth import AuthenticationCredentials
from hatch.utils.fs import Path


@pytest.fixture(autouse=True)
def mock_keyring(monkeypatch):
class MockKeyring:
@staticmethod
def get_credential(*_):
class Credential:
username = 'gat'
password = 'guido'

return Credential()

monkeypatch.setattr(hatch.publish.auth, 'keyring', MockKeyring)


def test_pypirc(fs):
fs.create_file(
Path.home() / '.pypirc',
@@ -45,21 +61,7 @@ def test_pypirc(fs):
assert credentials.password == 'gat'


def test_keyring_credentials(monkeypatch):
class MockKeyring:
@staticmethod
def get_credential(*_):
class Credential:
username = 'gat'

return Credential()

@staticmethod
def get_password(*_):
return 'guido'

monkeypatch.setattr(hatch.publish.auth, 'keyring', MockKeyring)

def test_keyring_credentials():
credentials = AuthenticationCredentials(
app=None,
cache_dir=Path('/none'),