Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit a15c003

Browse files
authored
Make DomainSpecificString an attrs class (#9875)
1 parent ceaa769 commit a15c003

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

changelog.d/9875.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make `DomainSpecificString` an `attrs` class.

synapse/handlers/oidc.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,11 @@ async def grandfather_existing_users() -> Optional[str]:
957957
# and attempt to match it.
958958
attributes = await oidc_response_to_user_attributes(failures=0)
959959

960+
if attributes.localpart is None:
961+
# If no localpart is returned then we will generate one, so
962+
# there is no need to search for existing users.
963+
return None
964+
960965
user_id = UserID(attributes.localpart, self._server_name).to_string()
961966
users = await self._store.get_users_by_id_case_insensitive(user_id)
962967
if users:

synapse/rest/synapse/client/new_user_consent.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ async def _async_render_GET(self, request: Request) -> None:
6161
self._sso_handler.render_error(request, "bad_session", e.msg, code=e.code)
6262
return
6363

64+
# It should be impossible to get here without having first been through
65+
# the pick-a-username step, which ensures chosen_localpart gets set.
66+
if not session.chosen_localpart:
67+
logger.warning("Session has no user name selected")
68+
self._sso_handler.render_error(
69+
request, "no_user", "No user name has been selected.", code=400
70+
)
71+
return
72+
6473
user_id = UserID(session.chosen_localpart, self._server_name)
6574
user_profile = {
6675
"display_name": session.display_name,

synapse/types.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,8 @@ def get_localpart_from_id(string):
199199
DS = TypeVar("DS", bound="DomainSpecificString")
200200

201201

202-
class DomainSpecificString(
203-
namedtuple("DomainSpecificString", ("localpart", "domain")), metaclass=abc.ABCMeta
204-
):
202+
@attr.s(slots=True, frozen=True, repr=False)
203+
class DomainSpecificString(metaclass=abc.ABCMeta):
205204
"""Common base class among ID/name strings that have a local part and a
206205
domain name, prefixed with a sigil.
207206
@@ -213,11 +212,8 @@ class DomainSpecificString(
213212

214213
SIGIL = abc.abstractproperty() # type: str # type: ignore
215214

216-
# Deny iteration because it will bite you if you try to create a singleton
217-
# set by:
218-
# users = set(user)
219-
def __iter__(self):
220-
raise ValueError("Attempted to iterate a %s" % (type(self).__name__,))
215+
localpart = attr.ib(type=str)
216+
domain = attr.ib(type=str)
221217

222218
# Because this class is a namedtuple of strings and booleans, it is deeply
223219
# immutable.
@@ -272,30 +268,35 @@ def is_valid(cls: Type[DS], s: str) -> bool:
272268
__repr__ = to_string
273269

274270

271+
@attr.s(slots=True, frozen=True, repr=False)
275272
class UserID(DomainSpecificString):
276273
"""Structure representing a user ID."""
277274

278275
SIGIL = "@"
279276

280277

278+
@attr.s(slots=True, frozen=True, repr=False)
281279
class RoomAlias(DomainSpecificString):
282280
"""Structure representing a room name."""
283281

284282
SIGIL = "#"
285283

286284

285+
@attr.s(slots=True, frozen=True, repr=False)
287286
class RoomID(DomainSpecificString):
288287
"""Structure representing a room id. """
289288

290289
SIGIL = "!"
291290

292291

292+
@attr.s(slots=True, frozen=True, repr=False)
293293
class EventID(DomainSpecificString):
294294
"""Structure representing an event id. """
295295

296296
SIGIL = "$"
297297

298298

299+
@attr.s(slots=True, frozen=True, repr=False)
299300
class GroupID(DomainSpecificString):
300301
"""Structure representing a group ID."""
301302

0 commit comments

Comments
 (0)