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

Commit

Permalink
Simplify config handling for email verification
Browse files Browse the repository at this point in the history
Rather than an enum and a boolean, all we need here is a single bool, which
says whether we are or are not doing email verification.
  • Loading branch information
richvdh committed Jul 6, 2022
1 parent 9a1487f commit 25de8ec
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 81 deletions.
3 changes: 1 addition & 2 deletions synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
register_start,
)
from synapse.config._base import ConfigError, format_config_error
from synapse.config.emailconfig import ThreepidBehaviour
from synapse.config.homeserver import HomeServerConfig
from synapse.config.server import ListenerConfig
from synapse.federation.transport.server import TransportLayerServer
Expand Down Expand Up @@ -202,7 +201,7 @@ def _configure_named_resource(
}
)

if self.config.email.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
if self.config.email.can_verify_email:
from synapse.rest.synapse.client.password_reset import (
PasswordResetSubmitTokenResource,
)
Expand Down
28 changes: 4 additions & 24 deletions synapse/config/emailconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import email.utils
import logging
import os
from enum import Enum
from typing import Any

import attr
Expand Down Expand Up @@ -137,21 +136,16 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
"is no longer supported. Please remove it from the config file."
)

self.local_threepid_handling_disabled_due_to_email_config = False
if email_config == {}:
# We cannot warn the user this has happened here
# Instead do so when a user attempts to reset their password
self.local_threepid_handling_disabled_due_to_email_config = True
self.threepid_behaviour_email = ThreepidBehaviour.OFF
else:
self.threepid_behaviour_email = ThreepidBehaviour.LOCAL
# If we have email config settings, assume that we can verify ownership of
# email addresses.
self.can_verify_email = email_config != {}

# Get lifetime of a validation token in milliseconds
self.email_validation_token_lifetime = self.parse_duration(
email_config.get("validation_token_lifetime", "1h")
)

if self.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
if self.can_verify_email:
missing = []
if not self.email_notif_from:
missing.append("email.notif_from")
Expand Down Expand Up @@ -342,17 +336,3 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
"Config option email.invite_client_location must be a http or https URL",
path=("email", "invite_client_location"),
)


class ThreepidBehaviour(Enum):
"""
Enum to define the behaviour of Synapse with regards to when it contacts an identity
server for 3pid registration and password resets
LOCAL = send tokens ourselves
OFF = disable registration via 3pid and password resets
"""

REMOTE = "remote"
LOCAL = "local"
OFF = "off"
3 changes: 1 addition & 2 deletions synapse/handlers/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
SynapseError,
)
from synapse.api.ratelimiting import Ratelimiter
from synapse.config.emailconfig import ThreepidBehaviour
from synapse.http import RequestTimedOutError
from synapse.http.client import SimpleHttpClient
from synapse.http.site import SynapseRequest
Expand Down Expand Up @@ -507,7 +506,7 @@ async def validate_threepid_session(
validation_session = None

# Try to validate as email
if self.hs.config.email.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
if self.hs.config.email.can_verify_email:
# Get a validated session matching these details
validation_session = await self.store.get_threepid_validation_session(
"email", client_secret, sid=sid, validated=True
Expand Down
10 changes: 3 additions & 7 deletions synapse/handlers/ui_auth/checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

from synapse.api.constants import LoginType
from synapse.api.errors import Codes, LoginError, SynapseError
from synapse.config.emailconfig import ThreepidBehaviour
from synapse.util import json_decoder

if TYPE_CHECKING:
Expand Down Expand Up @@ -153,7 +152,7 @@ async def _check_threepid(self, medium: str, authdict: dict) -> dict:

logger.info("Getting validated threepid. threepidcreds: %r", (threepid_creds,))

# msisdns are currently always ThreepidBehaviour.REMOTE
# msisdns are currently always verified via the IS
if medium == "msisdn":
if not self.hs.config.registration.account_threepid_delegate_msisdn:
raise SynapseError(
Expand All @@ -164,7 +163,7 @@ async def _check_threepid(self, medium: str, authdict: dict) -> dict:
threepid_creds,
)
elif medium == "email":
if self.hs.config.email.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
if self.hs.config.email.can_verify_email:
threepid = None
row = await self.store.get_threepid_validation_session(
medium,
Expand Down Expand Up @@ -216,10 +215,7 @@ def __init__(self, hs: "HomeServer"):
_BaseThreepidAuthChecker.__init__(self, hs)

def is_enabled(self) -> bool:
return self.hs.config.email.threepid_behaviour_email in (
ThreepidBehaviour.REMOTE,
ThreepidBehaviour.LOCAL,
)
return self.hs.config.email.can_verify_email

async def check_auth(self, authdict: dict, clientip: str) -> Any:
return await self._check_threepid("email", authdict)
Expand Down
40 changes: 15 additions & 25 deletions synapse/rest/client/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
SynapseError,
ThreepidValidationError,
)
from synapse.config.emailconfig import ThreepidBehaviour
from synapse.handlers.ui_auth import UIAuthSessionDataConstants
from synapse.http.server import HttpServer, finish_request, respond_with_html
from synapse.http.servlet import (
Expand Down Expand Up @@ -64,7 +63,7 @@ def __init__(self, hs: "HomeServer"):
self.config = hs.config
self.identity_handler = hs.get_identity_handler()

if self.config.email.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
if self.config.email.can_verify_email:
self.mailer = Mailer(
hs=self.hs,
app_name=self.config.email.email_app_name,
Expand All @@ -73,11 +72,10 @@ def __init__(self, hs: "HomeServer"):
)

async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
if self.config.email.threepid_behaviour_email == ThreepidBehaviour.OFF:
if self.config.email.local_threepid_handling_disabled_due_to_email_config:
logger.warning(
"User password resets have been disabled due to lack of email config"
)
if not self.config.email.can_verify_email:
logger.warning(
"User password resets have been disabled due to lack of email config"
)
raise SynapseError(
400, "Email-based password resets have been disabled on this server"
)
Expand Down Expand Up @@ -335,7 +333,7 @@ def __init__(self, hs: "HomeServer"):
self.identity_handler = hs.get_identity_handler()
self.store = self.hs.get_datastores().main

if self.config.email.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
if self.config.email.can_verify_email:
self.mailer = Mailer(
hs=self.hs,
app_name=self.config.email.email_app_name,
Expand All @@ -344,11 +342,10 @@ def __init__(self, hs: "HomeServer"):
)

async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
if self.config.email.threepid_behaviour_email == ThreepidBehaviour.OFF:
if self.config.email.local_threepid_handling_disabled_due_to_email_config:
logger.warning(
"Adding emails have been disabled due to lack of an email config"
)
if not self.config.email.can_verify_email:
logger.warning(
"Adding emails have been disabled due to lack of an email config"
)
raise SynapseError(
400, "Adding an email to your account is disabled on this server"
)
Expand Down Expand Up @@ -505,25 +502,18 @@ def __init__(self, hs: "HomeServer"):
self.config = hs.config
self.clock = hs.get_clock()
self.store = hs.get_datastores().main
if self.config.email.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
if self.config.email.can_verify_email:
self._failure_email_template = (
self.config.email.email_add_threepid_template_failure_html
)

async def on_GET(self, request: Request) -> None:
if self.config.email.threepid_behaviour_email == ThreepidBehaviour.OFF:
if self.config.email.local_threepid_handling_disabled_due_to_email_config:
logger.warning(
"Adding emails have been disabled due to lack of an email config"
)
raise SynapseError(
400, "Adding an email to your account is disabled on this server"
if not self.config.email.can_verify_email:
logger.warning(
"Adding emails have been disabled due to lack of an email config"
)
elif self.config.email.threepid_behaviour_email == ThreepidBehaviour.REMOTE:
raise SynapseError(
400,
"This homeserver is not validating threepids. Use an identity server "
"instead.",
400, "Adding an email to your account is disabled on this server"
)

sid = parse_string(request, "sid", required=True)
Expand Down
25 changes: 10 additions & 15 deletions synapse/rest/client/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
)
from synapse.api.ratelimiting import Ratelimiter
from synapse.config import ConfigError
from synapse.config.emailconfig import ThreepidBehaviour
from synapse.config.homeserver import HomeServerConfig
from synapse.config.ratelimiting import FederationRateLimitConfig
from synapse.config.server import is_threepid_reserved
Expand Down Expand Up @@ -74,7 +73,7 @@ def __init__(self, hs: "HomeServer"):
self.identity_handler = hs.get_identity_handler()
self.config = hs.config

if self.hs.config.email.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
if self.hs.config.email.can_verify_email:
self.mailer = Mailer(
hs=self.hs,
app_name=self.config.email.email_app_name,
Expand All @@ -83,13 +82,10 @@ def __init__(self, hs: "HomeServer"):
)

async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
if self.hs.config.email.threepid_behaviour_email == ThreepidBehaviour.OFF:
if (
self.hs.config.email.local_threepid_handling_disabled_due_to_email_config
):
logger.warning(
"Email registration has been disabled due to lack of email config"
)
if not self.hs.config.email.can_verify_email:
logger.warning(
"Email registration has been disabled due to lack of email config"
)
raise SynapseError(
400, "Email-based registration has been disabled on this server"
)
Expand Down Expand Up @@ -246,7 +242,7 @@ def __init__(self, hs: "HomeServer"):
self.clock = hs.get_clock()
self.store = hs.get_datastores().main

if self.config.email.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
if self.config.email.can_verify_email:
self._failure_email_template = (
self.config.email.email_registration_template_failure_html
)
Expand All @@ -256,11 +252,10 @@ async def on_GET(self, request: Request, medium: str) -> None:
raise SynapseError(
400, "This medium is currently not supported for registration"
)
if self.config.email.threepid_behaviour_email == ThreepidBehaviour.OFF:
if self.config.email.local_threepid_handling_disabled_due_to_email_config:
logger.warning(
"User registration via email has been disabled due to lack of email config"
)
if not self.config.email.can_verify_email:
logger.warning(
"User registration via email has been disabled due to lack of email config"
)
raise SynapseError(
400, "Email-based registration is disabled on this server"
)
Expand Down
8 changes: 2 additions & 6 deletions synapse/rest/synapse/client/password_reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from twisted.web.server import Request

from synapse.api.errors import ThreepidValidationError
from synapse.config.emailconfig import ThreepidBehaviour
from synapse.http.server import DirectServeHtmlResource
from synapse.http.servlet import parse_string
from synapse.util.stringutils import assert_valid_client_secret
Expand Down Expand Up @@ -46,9 +45,6 @@ def __init__(self, hs: "HomeServer"):
self.clock = hs.get_clock()
self.store = hs.get_datastores().main

self._local_threepid_handling_disabled_due_to_email_config = (
hs.config.email.local_threepid_handling_disabled_due_to_email_config
)
self._confirmation_email_template = (
hs.config.email.email_password_reset_template_confirmation_html
)
Expand All @@ -59,8 +55,8 @@ def __init__(self, hs: "HomeServer"):
hs.config.email.email_password_reset_template_failure_html
)

# This resource should not be mounted if threepid behaviour is not LOCAL
assert hs.config.email.threepid_behaviour_email == ThreepidBehaviour.LOCAL
# This resource should only be mounted if email validation is enabled
assert hs.config.email.can_verify_email

async def _async_render_GET(self, request: Request) -> Tuple[int, bytes]:
sid = parse_string(request, "sid", required=True)
Expand Down

0 comments on commit 25de8ec

Please sign in to comment.