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

ask for creds again when token was expired #17127

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 4 additions & 20 deletions conans/client/rest/auth_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,10 @@ def call_rest_api_method(self, remote, method_name, *args, **kwargs):
raise ForbiddenException(f"Permission denied for user: '{user}': {e}")
except AuthenticationException:
# User valid but not enough permissions
if user is None or token is None:
# token is None when you change user with user command
# Anonymous is not enough, ask for a user
ConanOutput().info('Please log in to "%s" to perform this action. '
'Execute "conan remote login" command.' % remote.name)
if self._get_credentials_and_authenticate(rest_client, user, remote):
return self.call_rest_api_method(remote, method_name, *args, **kwargs)
else:
# Token expired or not valid, so clean the token and repeat the call
# (will be anonymous call but exporting who is calling)
# logger.info("Token expired or not valid, cleaning the saved token and retrying")
self._clear_user_tokens_in_db(user, remote)
# token is None when you change user with user command
# Anonymous is not enough, ask for a user
ConanOutput().info(f"Remote '{remote.name}' needs authentication, obtaining credentials")
if self._get_credentials_and_authenticate(rest_client, user, remote):
return self.call_rest_api_method(remote, method_name, *args, **kwargs)

def _get_credentials_and_authenticate(self, rest_client, user, remote):
Expand All @@ -95,14 +87,6 @@ def _get_credentials_and_authenticate(self, rest_client, user, remote):
return True
raise AuthenticationException("Too many failed login attempts, bye!")

def _clear_user_tokens_in_db(self, user, remote):
try:
self._creds.set(remote, user, token=None)
except Exception as e:
out = ConanOutput()
out.error('Your credentials could not be stored in local cache', error_type="exception")
out.debug(str(e) + '\n')

def _authenticate(self, rest_client, remote, user, password):
try:
token = rest_client.authenticate(user, password)
Expand Down
11 changes: 5 additions & 6 deletions test/integration/remote/auth_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ def test_token_expired():
*/*@*/*: admin
""")
save(os.path.join(server_folder, ".conan_server", "server.conf"), server_conf)
server = TestServer(base_path=server_folder, users={"admin": "password"})
server = TestServer(base_path=server_folder, users={"admin": "password", "other": "pass"})

c = TestClient(servers={"default": server}, inputs=["admin", "password"])
c = TestClient(servers={"default": server}, inputs=["admin", "password", "other", "pass"])
c.save({"conanfile.py": GenConanfile()})
c.run("create . --name=pkg --version=0.1 --user=user --channel=stable")
c.run("upload * -r=default -c")
Expand All @@ -216,13 +216,12 @@ def test_token_expired():
import time
time.sleep(3)
c.users = {}
conan_conf = "core:non_interactive=True"
c.save_home({"global.conf": conan_conf})
c.run("remove * -c")
c.run("install --requires=pkg/0.1@user/stable")
assert "Remote 'default' needs authentication, obtaining credentials" in c.out
user, token, _ = localdb.get_login(server.fake_url)
assert user == "admin"
assert token is None
assert user == "other"
assert token is not None


def test_auth_username_space():
Expand Down