|
26 | 26 | SynapseError, |
27 | 27 | ) |
28 | 28 | from synapse.api.ratelimiting import Ratelimiter |
| 29 | +from synapse.config.emailconfig import ThreepidBehaviour |
29 | 30 | from synapse.http import RequestTimedOutError |
30 | 31 | from synapse.http.client import SimpleHttpClient |
31 | 32 | from synapse.http.site import SynapseRequest |
@@ -415,6 +416,48 @@ async def send_threepid_validation( |
415 | 416 |
|
416 | 417 | return session_id |
417 | 418 |
|
| 419 | + async def request_email_token( |
| 420 | + self, |
| 421 | + id_server: str, |
| 422 | + email: str, |
| 423 | + client_secret: str, |
| 424 | + send_attempt: int, |
| 425 | + next_link: Optional[str] = None, |
| 426 | + ) -> JsonDict: |
| 427 | + """ |
| 428 | + Request an external server send an email on our behalf for the purposes of threepid |
| 429 | + validation. |
| 430 | +
|
| 431 | + Args: |
| 432 | + id_server: The identity server to proxy to |
| 433 | + email: The email to send the message to |
| 434 | + client_secret: The unique client_secret sends by the user |
| 435 | + send_attempt: Which attempt this is |
| 436 | + next_link: A link to redirect the user to once they submit the token |
| 437 | +
|
| 438 | + Returns: |
| 439 | + The json response body from the server |
| 440 | + """ |
| 441 | + params = { |
| 442 | + "email": email, |
| 443 | + "client_secret": client_secret, |
| 444 | + "send_attempt": send_attempt, |
| 445 | + } |
| 446 | + if next_link: |
| 447 | + params["next_link"] = next_link |
| 448 | + |
| 449 | + try: |
| 450 | + data = await self.http_client.post_json_get_json( |
| 451 | + id_server + "/_matrix/identity/api/v1/validate/email/requestToken", |
| 452 | + params, |
| 453 | + ) |
| 454 | + return data |
| 455 | + except HttpResponseException as e: |
| 456 | + logger.info("Proxied requestToken failed: %r", e) |
| 457 | + raise e.to_synapse_error() |
| 458 | + except RequestTimedOutError: |
| 459 | + raise SynapseError(500, "Timed out contacting identity server") |
| 460 | + |
418 | 461 | async def requestMsisdnToken( |
419 | 462 | self, |
420 | 463 | id_server: str, |
@@ -488,7 +531,18 @@ async def validate_threepid_session( |
488 | 531 | validation_session = None |
489 | 532 |
|
490 | 533 | # Try to validate as email |
491 | | - if self.hs.config.email.can_verify_email: |
| 534 | + if self.hs.config.email.threepid_behaviour_email == ThreepidBehaviour.REMOTE: |
| 535 | + # Remote emails will only be used if a valid identity server is provided. |
| 536 | + assert ( |
| 537 | + self.hs.config.registration.account_threepid_delegate_email is not None |
| 538 | + ) |
| 539 | + |
| 540 | + # Ask our delegated email identity server |
| 541 | + validation_session = await self.threepid_from_creds( |
| 542 | + self.hs.config.registration.account_threepid_delegate_email, |
| 543 | + threepid_creds, |
| 544 | + ) |
| 545 | + elif self.hs.config.email.threepid_behaviour_email == ThreepidBehaviour.LOCAL: |
492 | 546 | # Get a validated session matching these details |
493 | 547 | validation_session = await self.store.get_threepid_validation_session( |
494 | 548 | "email", client_secret, sid=sid, validated=True |
|
0 commit comments