diff --git a/buildbot_netauth/__init__.py b/buildbot_netauth/__init__.py index 99447de..4bcbbd0 100644 --- a/buildbot_netauth/__init__.py +++ b/buildbot_netauth/__init__.py @@ -5,7 +5,7 @@ from buildbot.www import resource from buildbot.www.avatar import AvatarBase from buildbot.www.auth import UserInfoProviderBase, bytes2unicode -from twisted.internet import defer +from twisted.internet import defer, threads from twisted.cred.error import UnauthorizedLogin __version__ = "0.0.1" @@ -34,7 +34,7 @@ def __init__(self, *, conf: Path | None = None, **kwargs): def requestAvatarId(self, cred): if self.check_credentials(cred.username, cred.password): - return defer.succeed(f"{cred.username}@netauth") + return defer.succeed(cred.username + b"@netauth") return defer.fail(UnauthorizedLogin()) def check_credentials(self, username: str, password: str) -> bool: @@ -51,39 +51,43 @@ def getUserInfo(self, username): if not username: return defer.fail(ValueError("username not found")) - try: - entity = self.netauth.entity_info(username) - - if entity is None: - return defer.fail(ValueError("entity not found")) - - id = entity.id - email = f"{id}@netauth" - if (meta := entity.meta) is not None: - full_name = meta.display_name or meta.legal_name or id - groups = meta.groups or [] - else: - full_name = entity.id - groups = [] - - return defer.succeed( - { - "email": email, - "full_name": full_name, - "groups": groups, - } - ) - except netauth.error.NetAuthRpcError as e: - return defer.fail(e) + def thr(): + try: + entity = self.netauth.entity_info(username) + + if entity is None: + return defer.fail(ValueError("entity not found")) + + id = entity.id + email = f"{id}@netauth" + if (meta := entity.meta) is not None: + full_name = meta.display_name or meta.legal_name or id + groups = meta.groups or [] + else: + full_name = entity.id + groups = [] + + return defer.succeed( + { + "email": email, + "full_name": full_name, + "groups": groups, + } + ) + except netauth.error.NetAuthRpcError as e: + return defer.fail(e) + return threads.deferToThread(thr) def getUserAvatar(self, email, username, size, defaultAvatarUrl): print(repr(email), repr(username), repr(size), repr(defaultAvatarUrl)) username = bytes2unicode(username) if username and username.endswith("@netauth"): - try: - kv = self.netauth.entity_kv_get(username.removesuffix("@netauth"), "avatar") - avatar = kv.get("avatar") - if avatar: - raise resource.Redirect(avatar[0]) - except netauth.error.NetAuthRpcError: - pass + def thr(): + try: + kv = self.netauth.entity_kv_get(username.removesuffix("@netauth"), "avatar") + avatar = kv.get("avatar") + if avatar: + raise resource.Redirect(avatar[0]) + except netauth.error.NetAuthRpcError: + pass + return threads.deferToThread(thr)