You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

93 lines
3.0 KiB

  1. # Copyright 2022 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import TYPE_CHECKING, Dict, Optional
  15. from synapse._pydantic_compat import HAS_PYDANTIC_V2
  16. if TYPE_CHECKING or HAS_PYDANTIC_V2:
  17. from pydantic.v1 import Extra, StrictInt, StrictStr, constr, validator
  18. else:
  19. from pydantic import Extra, StrictInt, StrictStr, constr, validator
  20. from synapse.rest.models import RequestBodyModel
  21. from synapse.util.threepids import validate_email
  22. class AuthenticationData(RequestBodyModel):
  23. """
  24. Data used during user-interactive authentication.
  25. (The name "Authentication Data" is taken directly from the spec.)
  26. Additional keys will be present, depending on the `type` field. Use
  27. `.dict(exclude_unset=True)` to access them.
  28. """
  29. class Config:
  30. extra = Extra.allow
  31. session: Optional[StrictStr] = None
  32. type: Optional[StrictStr] = None
  33. if TYPE_CHECKING:
  34. ClientSecretStr = StrictStr
  35. else:
  36. # See also assert_valid_client_secret()
  37. ClientSecretStr = constr(
  38. regex="[0-9a-zA-Z.=_-]", # noqa: F722
  39. min_length=1,
  40. max_length=255,
  41. strict=True,
  42. )
  43. class ThreepidRequestTokenBody(RequestBodyModel):
  44. client_secret: ClientSecretStr
  45. id_server: Optional[StrictStr]
  46. id_access_token: Optional[StrictStr]
  47. next_link: Optional[StrictStr]
  48. send_attempt: StrictInt
  49. @validator("id_access_token", always=True)
  50. def token_required_for_identity_server(
  51. cls, token: Optional[str], values: Dict[str, object]
  52. ) -> Optional[str]:
  53. if values.get("id_server") is not None and token is None:
  54. raise ValueError("id_access_token is required if an id_server is supplied.")
  55. return token
  56. class EmailRequestTokenBody(ThreepidRequestTokenBody):
  57. email: StrictStr
  58. # Canonicalise the email address. The addresses are all stored canonicalised
  59. # in the database. This allows the user to reset his password without having to
  60. # know the exact spelling (eg. upper and lower case) of address in the database.
  61. # Without this, an email stored in the database as "foo@bar.com" would cause
  62. # user requests for "FOO@bar.com" to raise a Not Found error.
  63. _email_validator = validator("email", allow_reuse=True)(validate_email)
  64. if TYPE_CHECKING:
  65. ISO3116_1_Alpha_2 = StrictStr
  66. else:
  67. # Per spec: two-letter uppercase ISO-3166-1-alpha-2
  68. ISO3116_1_Alpha_2 = constr(regex="[A-Z]{2}", strict=True)
  69. class MsisdnRequestTokenBody(ThreepidRequestTokenBody):
  70. country: ISO3116_1_Alpha_2
  71. phone_number: StrictStr