From 05b7312cc92e6a2ed7d18db38c2bc19fa91ee2d2 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 25 Oct 2021 15:30:22 +0100 Subject: [PATCH 1/6] Fix cyclic import in the module API --- synapse/module_api/__init__.py | 2 +- synapse/rest/client/login.py | 16 ++-------------- synapse/types.py | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index d37252b6b3b2..d84d94c7fad3 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -45,7 +45,6 @@ from synapse.http.site import SynapseRequest from synapse.logging.context import make_deferred_yieldable, run_in_background from synapse.metrics.background_process_metrics import run_as_background_process -from synapse.rest.client.login import LoginResponse from synapse.storage import DataStore from synapse.storage.database import DatabasePool, LoggingTransaction from synapse.storage.databases.main.roommember import ProfileInfo @@ -53,6 +52,7 @@ from synapse.types import ( DomainSpecificString, JsonDict, + LoginResponse, Requester, UserID, UserInfo, diff --git a/synapse/rest/client/login.py b/synapse/rest/client/login.py index d49a647b0314..77eff7c062a4 100644 --- a/synapse/rest/client/login.py +++ b/synapse/rest/client/login.py @@ -14,9 +14,7 @@ import logging import re -from typing import TYPE_CHECKING, Any, Awaitable, Callable, Dict, List, Optional, Tuple - -from typing_extensions import TypedDict +from typing import TYPE_CHECKING, Awaitable, Callable, Dict, List, Optional, Tuple from synapse.api.errors import Codes, LoginError, SynapseError from synapse.api.ratelimiting import Ratelimiter @@ -36,7 +34,7 @@ from synapse.http.site import SynapseRequest from synapse.rest.client._base import client_patterns from synapse.rest.well_known import WellKnownBuilder -from synapse.types import JsonDict, UserID +from synapse.types import JsonDict, LoginResponse, UserID if TYPE_CHECKING: from synapse.server import HomeServer @@ -44,16 +42,6 @@ logger = logging.getLogger(__name__) -class LoginResponse(TypedDict, total=False): - user_id: str - access_token: str - home_server: str - expires_in_ms: Optional[int] - refresh_token: Optional[str] - device_id: str - well_known: Optional[Dict[str, Any]] - - class LoginRestServlet(RestServlet): PATTERNS = client_patterns("/login$", v1=True) CAS_TYPE = "m.login.cas" diff --git a/synapse/types.py b/synapse/types.py index 364ecf7d450a..171f12ceca44 100644 --- a/synapse/types.py +++ b/synapse/types.py @@ -28,6 +28,7 @@ TypeVar, Union, ) +from typing_extensions import TypedDict import attr from frozendict import frozendict @@ -796,3 +797,22 @@ class UserInfo: is_deactivated: bool is_guest: bool is_shadow_banned: bool + + +class LoginResponse(TypedDict, total=False): + """Represents a response to a POST /login request. + + The description of the fields can be found here: + https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-login + + This response may also include the two additional fields from MSC2918 (refresh + tokens), which are `expires_in_ms` and `refresh_token`. + """ + user_id: str + access_token: str + home_server: str + expires_in_ms: Optional[int] + refresh_token: Optional[str] + device_id: str + well_known: Optional[Dict[str, Any]] + From a085f05c2d4352ea15a3214742d136851050bbee Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 25 Oct 2021 15:36:01 +0100 Subject: [PATCH 2/6] Changelog --- changelog.d/11180.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/11180.feature diff --git a/changelog.d/11180.feature b/changelog.d/11180.feature new file mode 100644 index 000000000000..82c40bf1b222 --- /dev/null +++ b/changelog.d/11180.feature @@ -0,0 +1 @@ +Port the Password Auth Providers module interface to the new generic interface. From c62a4cffd90a084333099cfef6cc0304f972d3f9 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 25 Oct 2021 15:38:41 +0100 Subject: [PATCH 3/6] Lint --- synapse/types.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/types.py b/synapse/types.py index 171f12ceca44..d56d8c3931e8 100644 --- a/synapse/types.py +++ b/synapse/types.py @@ -28,11 +28,11 @@ TypeVar, Union, ) -from typing_extensions import TypedDict import attr from frozendict import frozendict from signedjson.key import decode_verify_key_bytes +from typing_extensions import TypedDict from unpaddedbase64 import decode_base64 from zope.interface import Interface @@ -808,6 +808,7 @@ class LoginResponse(TypedDict, total=False): This response may also include the two additional fields from MSC2918 (refresh tokens), which are `expires_in_ms` and `refresh_token`. """ + user_id: str access_token: str home_server: str @@ -815,4 +816,3 @@ class LoginResponse(TypedDict, total=False): refresh_token: Optional[str] device_id: str well_known: Optional[Dict[str, Any]] - From 6cf6964a11d4ce934c795103e383eaff81eed21f Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 25 Oct 2021 17:15:32 +0100 Subject: [PATCH 4/6] Revert "Fix cyclic import in the module API" This reverts commit 05b7312cc92e6a2ed7d18db38c2bc19fa91ee2d2. --- synapse/module_api/__init__.py | 2 +- synapse/rest/client/login.py | 16 ++++++++++++++-- synapse/types.py | 18 ------------------ 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index d84d94c7fad3..d37252b6b3b2 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -45,6 +45,7 @@ from synapse.http.site import SynapseRequest from synapse.logging.context import make_deferred_yieldable, run_in_background from synapse.metrics.background_process_metrics import run_as_background_process +from synapse.rest.client.login import LoginResponse from synapse.storage import DataStore from synapse.storage.database import DatabasePool, LoggingTransaction from synapse.storage.databases.main.roommember import ProfileInfo @@ -52,7 +53,6 @@ from synapse.types import ( DomainSpecificString, JsonDict, - LoginResponse, Requester, UserID, UserInfo, diff --git a/synapse/rest/client/login.py b/synapse/rest/client/login.py index 77eff7c062a4..d49a647b0314 100644 --- a/synapse/rest/client/login.py +++ b/synapse/rest/client/login.py @@ -14,7 +14,9 @@ import logging import re -from typing import TYPE_CHECKING, Awaitable, Callable, Dict, List, Optional, Tuple +from typing import TYPE_CHECKING, Any, Awaitable, Callable, Dict, List, Optional, Tuple + +from typing_extensions import TypedDict from synapse.api.errors import Codes, LoginError, SynapseError from synapse.api.ratelimiting import Ratelimiter @@ -34,7 +36,7 @@ from synapse.http.site import SynapseRequest from synapse.rest.client._base import client_patterns from synapse.rest.well_known import WellKnownBuilder -from synapse.types import JsonDict, LoginResponse, UserID +from synapse.types import JsonDict, UserID if TYPE_CHECKING: from synapse.server import HomeServer @@ -42,6 +44,16 @@ logger = logging.getLogger(__name__) +class LoginResponse(TypedDict, total=False): + user_id: str + access_token: str + home_server: str + expires_in_ms: Optional[int] + refresh_token: Optional[str] + device_id: str + well_known: Optional[Dict[str, Any]] + + class LoginRestServlet(RestServlet): PATTERNS = client_patterns("/login$", v1=True) CAS_TYPE = "m.login.cas" diff --git a/synapse/types.py b/synapse/types.py index d56d8c3931e8..c34a2ce17ecd 100644 --- a/synapse/types.py +++ b/synapse/types.py @@ -798,21 +798,3 @@ class UserInfo: is_guest: bool is_shadow_banned: bool - -class LoginResponse(TypedDict, total=False): - """Represents a response to a POST /login request. - - The description of the fields can be found here: - https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-login - - This response may also include the two additional fields from MSC2918 (refresh - tokens), which are `expires_in_ms` and `refresh_token`. - """ - - user_id: str - access_token: str - home_server: str - expires_in_ms: Optional[int] - refresh_token: Optional[str] - device_id: str - well_known: Optional[Dict[str, Any]] From aff0276866f9476302821bac5166299745b79cd7 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 25 Oct 2021 17:16:54 +0100 Subject: [PATCH 5/6] Instead, just make the import conditional --- synapse/handlers/auth.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/handlers/auth.py b/synapse/handlers/auth.py index ebe75a9e9b22..6e22a1231a3a 100644 --- a/synapse/handlers/auth.py +++ b/synapse/handlers/auth.py @@ -62,7 +62,6 @@ from synapse.http.site import SynapseRequest from synapse.logging.context import defer_to_thread from synapse.metrics.background_process_metrics import run_as_background_process -from synapse.module_api import ModuleApi from synapse.storage.roommember import ProfileInfo from synapse.types import JsonDict, Requester, UserID from synapse.util import stringutils as stringutils @@ -73,6 +72,7 @@ from synapse.util.threepids import canonicalise_email if TYPE_CHECKING: + from synapse.module_api import ModuleApi from synapse.rest.client.login import LoginResponse from synapse.server import HomeServer @@ -1818,7 +1818,7 @@ def load_legacy_password_auth_providers(hs: "HomeServer") -> None: def load_single_legacy_password_auth_provider( - module: Type, config: JsonDict, api: ModuleApi + module: Type, config: JsonDict, api: "ModuleApi", ) -> None: try: provider = module(config=config, account_handler=api) From fd0f8a7e5ad4e2e949d3bfd5663044d9f3ad09b8 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 25 Oct 2021 17:20:37 +0100 Subject: [PATCH 6/6] Oops, revert didn't work perfectly --- synapse/handlers/auth.py | 4 +++- synapse/types.py | 2 -- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/synapse/handlers/auth.py b/synapse/handlers/auth.py index 6e22a1231a3a..d508d7d32ab1 100644 --- a/synapse/handlers/auth.py +++ b/synapse/handlers/auth.py @@ -1818,7 +1818,9 @@ def load_legacy_password_auth_providers(hs: "HomeServer") -> None: def load_single_legacy_password_auth_provider( - module: Type, config: JsonDict, api: "ModuleApi", + module: Type, + config: JsonDict, + api: "ModuleApi", ) -> None: try: provider = module(config=config, account_handler=api) diff --git a/synapse/types.py b/synapse/types.py index c34a2ce17ecd..364ecf7d450a 100644 --- a/synapse/types.py +++ b/synapse/types.py @@ -32,7 +32,6 @@ import attr from frozendict import frozendict from signedjson.key import decode_verify_key_bytes -from typing_extensions import TypedDict from unpaddedbase64 import decode_base64 from zope.interface import Interface @@ -797,4 +796,3 @@ class UserInfo: is_deactivated: bool is_guest: bool is_shadow_banned: bool -