From 3e2d00316327a7cced099f63423000b555e374ac Mon Sep 17 00:00:00 2001 From: David Robertson Date: Tue, 5 Jul 2022 18:50:45 +0100 Subject: [PATCH] move out RequestToken body to models it's about to get reused --- synapse/rest/client/account.py | 28 +++------------------------- synapse/rest/client/models.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/synapse/rest/client/account.py b/synapse/rest/client/account.py index a73d040aaa01..31394bb27298 100644 --- a/synapse/rest/client/account.py +++ b/synapse/rest/client/account.py @@ -19,7 +19,7 @@ from typing import TYPE_CHECKING, Optional, Tuple from urllib.parse import urlparse -from pydantic import BaseModel, StrictBool, StrictStr, constr, validator +from pydantic import BaseModel, StrictBool, StrictStr, constr from twisted.web.server import Request @@ -43,7 +43,7 @@ from synapse.http.site import SynapseRequest from synapse.metrics import threepid_send_requests from synapse.push.mailer import Mailer -from synapse.rest.client.models import AuthenticationData +from synapse.rest.client.models import AuthenticationData, EmailRequestTokenBody from synapse.types import JsonDict from synapse.util.msisdn import phone_number_to_msisdn from synapse.util.stringutils import assert_valid_client_secret, random_string @@ -58,28 +58,6 @@ logger = logging.getLogger(__name__) -class EmailPasswordRequestBody(BaseModel): - if TYPE_CHECKING: - client_secret: str - else: - # See also assert_valid_client_secret() - client_secret: constr( - regex="[0-9a-zA-Z.=_-]", min_length=0, max_length=255 # noqa: F722 - ) - email: str - id_access_token: Optional[str] - id_server: Optional[str] - next_link: Optional[str] - send_attempt: int - - # Canonicalise the email address. The addresses are all stored canonicalised - # in the database. This allows the user to reset his password without having to - # know the exact spelling (eg. upper and lower case) of address in the database. - # Without this, an email stored in the database as "foo@bar.com" would cause - # user requests for "FOO@bar.com" to raise a Not Found error. - _email_validator = validator("email", allow_reuse=True)(validate_email) - - class EmailPasswordRequestTokenRestServlet(RestServlet): PATTERNS = client_patterns("/account/password/email/requestToken$") @@ -111,7 +89,7 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]: ) body = parse_and_validate_json_object_from_request( - request, EmailPasswordRequestBody + request, EmailRequestTokenBody ) if body.next_link: diff --git a/synapse/rest/client/models.py b/synapse/rest/client/models.py index 12f2c0917432..4eca862217e5 100644 --- a/synapse/rest/client/models.py +++ b/synapse/rest/client/models.py @@ -11,9 +11,11 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from typing import Optional +from typing import TYPE_CHECKING, Optional -from pydantic import BaseModel, Extra, StrictStr +from pydantic import BaseModel, Extra, StrictStr, constr, validator + +from synapse.util.threepids import validate_email class AuthenticationData(BaseModel): @@ -22,3 +24,25 @@ class Config: session: Optional[StrictStr] = None type: Optional[StrictStr] = None + + +class EmailRequestTokenBody(BaseModel): + if TYPE_CHECKING: + client_secret: str + else: + # See also assert_valid_client_secret() + client_secret: constr( + regex="[0-9a-zA-Z.=_-]", min_length=0, max_length=255 # noqa: F722 + ) + email: str + id_access_token: Optional[str] + id_server: Optional[str] + next_link: Optional[str] + send_attempt: int + + # Canonicalise the email address. The addresses are all stored canonicalised + # in the database. This allows the user to reset his password without having to + # know the exact spelling (eg. upper and lower case) of address in the database. + # Without this, an email stored in the database as "foo@bar.com" would cause + # user requests for "FOO@bar.com" to raise a Not Found error. + _email_validator = validator("email", allow_reuse=True)(validate_email)