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

Commit

Permalink
move out RequestToken body to models
Browse files Browse the repository at this point in the history
it's about to get reused
  • Loading branch information
David Robertson committed Jul 5, 2022
1 parent 0a87861 commit 3e2d003
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
28 changes: 3 additions & 25 deletions synapse/rest/client/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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$")

Expand Down Expand Up @@ -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:
Expand Down
28 changes: 26 additions & 2 deletions synapse/rest/client/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

0 comments on commit 3e2d003

Please sign in to comment.