From b6a69b2ed9563875eaa110898ce416807324c3ff Mon Sep 17 00:00:00 2001 From: tenma Date: Mon, 25 Jan 2021 15:14:56 +0100 Subject: [PATCH] gitea.lister: improve handling of credentials --- swh/lister/gitea/lister.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/swh/lister/gitea/lister.py b/swh/lister/gitea/lister.py index a358cf9..ebb7b6e 100644 --- a/swh/lister/gitea/lister.py +++ b/swh/lister/gitea/lister.py @@ -4,6 +4,7 @@ # See top-level LICENSE file for more information import logging +import random from typing import Any, Dict, Iterator, List, Optional from urllib.parse import urljoin @@ -65,12 +66,18 @@ class GiteaLister(StatelessLister[RepoListPage]): {"Accept": "application/json", "User-Agent": USER_AGENT,} ) - if api_token is None and len(self.credentials) > 0: - logger.warning( - "Gitea lister support only API token authentication " - " as of now. Will use the first password as token." - ) - api_token = self.credentials[0]["password"] + if api_token is None: + if len(self.credentials) > 0: + cred = random.choice(self.credentials) + username = cred.get("username") + api_token = cred["password"] + logger.warning( + "Using authentication token from user %s", username or "???" + ) + else: + logger.warning( + "No authentication token set in configuration, using anonymous mode" + ) if api_token: self.session.headers["Authorization"] = "Token %s" % api_token