Beispiel #1
0
    def __init__(self, hs):
        """
        Args:
            hs (synapse.server.HomeServer): server
        """
        super(EmailRegisterRequestTokenRestServlet, self).__init__()
        self.hs = hs
        self.identity_handler = hs.get_handlers().identity_handler
        self.config = hs.config

        if self.hs.config.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
            from synapse.push.mailer import Mailer, load_jinja2_templates

            template_html, template_text = load_jinja2_templates(
                self.config.email_template_dir,
                [
                    self.config.email_registration_template_html,
                    self.config.email_registration_template_text,
                ],
                apply_format_ts_filter=True,
                apply_mxc_to_http_filter=True,
                public_baseurl=self.config.public_baseurl,
            )
            self.mailer = Mailer(
                hs=self.hs,
                app_name=self.config.email_app_name,
                template_html=template_html,
                template_text=template_text,
            )
Beispiel #2
0
 def _create_email_pusher(self, _hs, pusherdict):
     app_name = self._app_name_from_pusherdict(pusherdict)
     mailer = self.mailers.get(app_name)
     if not mailer:
         mailer = Mailer(
             hs=self.hs,
             app_name=app_name,
             notif_template_html=self.notif_template_html,
             notif_template_text=self.notif_template_text,
         )
         self.mailers[app_name] = mailer
     return EmailPusher(self.hs, pusherdict, mailer)
Beispiel #3
0
    def __init__(self, hs):
        super(EmailPasswordRequestTokenRestServlet, self).__init__()
        self.hs = hs
        self.datastore = hs.get_datastore()
        self.config = hs.config
        self.identity_handler = hs.get_handlers().identity_handler

        if self.config.email_password_reset_behaviour == "local":
            from synapse.push.mailer import Mailer, load_jinja2_templates

            templates = load_jinja2_templates(
                config=hs.config,
                template_html_name=hs.config.email_password_reset_template_html,
                template_text_name=hs.config.email_password_reset_template_text,
            )
            self.mailer = Mailer(
                hs=self.hs,
                app_name=self.config.email_app_name,
                template_html=templates[0],
                template_text=templates[1],
            )
Beispiel #4
0
 def _create_email_pusher(self, _hs: "HomeServer",
                          pusher_config: PusherConfig) -> EmailPusher:
     app_name = self._app_name_from_pusherdict(pusher_config)
     mailer = self.mailers.get(app_name)
     if not mailer:
         mailer = Mailer(
             hs=self.hs,
             app_name=app_name,
             template_html=self._notif_template_html,
             template_text=self._notif_template_text,
         )
         self.mailers[app_name] = mailer
     return EmailPusher(self.hs, pusher_config, mailer)
Beispiel #5
0
    def __init__(self, hs):
        super().__init__()
        self.hs = hs
        self.datastore = hs.get_datastore()
        self.config = hs.config
        self.identity_handler = hs.get_handlers().identity_handler

        if self.config.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
            self.mailer = Mailer(
                hs=self.hs,
                app_name=self.config.email_app_name,
                template_html=self.config.email_password_reset_template_html,
                template_text=self.config.email_password_reset_template_text,
            )
Beispiel #6
0
    def __init__(self, hs):
        super(EmailThreepidRequestTokenRestServlet, self).__init__()
        self.hs = hs
        self.config = hs.config
        self.identity_handler = hs.get_handlers().identity_handler
        self.store = self.hs.get_datastore()

        if self.config.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
            self.mailer = Mailer(
                hs=self.hs,
                app_name=self.config.email_app_name,
                template_html=self.config.email_add_threepid_template_html,
                template_text=self.config.email_add_threepid_template_text,
            )
Beispiel #7
0
    def __init__(self, hs: "HomeServer"):
        super().__init__()
        self.hs = hs
        self.config = hs.config
        self.identity_handler = hs.get_identity_handler()
        self.store = self.hs.get_datastore()

        if self.config.email.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
            self.mailer = Mailer(
                hs=self.hs,
                app_name=self.config.email.email_app_name,
                template_html=self.config.email.email_add_threepid_template_html,
                template_text=self.config.email.email_add_threepid_template_text,
            )
Beispiel #8
0
    def __init__(self, hs):
        """
        Args:
            hs (synapse.server.HomeServer): server
        """
        super().__init__()
        self.hs = hs
        self.identity_handler = hs.get_identity_handler()
        self.config = hs.config

        if self.hs.config.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
            self.mailer = Mailer(
                hs=self.hs,
                app_name=self.config.email_app_name,
                template_html=self.config.email_registration_template_html,
                template_text=self.config.email_registration_template_text,
            )
Beispiel #9
0
    def __init__(self, hs):
        super(EmailPasswordRequestTokenRestServlet, self).__init__()
        self.hs = hs
        self.datastore = hs.get_datastore()
        self.config = hs.config
        self.identity_handler = hs.get_handlers().identity_handler

        if self.config.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
            template_html, template_text = load_jinja2_templates(
                self.config.email_template_dir,
                [
                    self.config.email_password_reset_template_html,
                    self.config.email_password_reset_template_text,
                ],
                apply_format_ts_filter=True,
                apply_mxc_to_http_filter=True,
                public_baseurl=self.config.public_baseurl,
            )
            self.mailer = Mailer(
                hs=self.hs,
                app_name=self.config.email_app_name,
                template_html=template_html,
                template_text=template_text,
            )
Beispiel #10
0
class EmailPasswordRequestTokenRestServlet(RestServlet):
    PATTERNS = client_patterns("/account/password/email/requestToken$")

    def __init__(self, hs):
        super(EmailPasswordRequestTokenRestServlet, self).__init__()
        self.hs = hs
        self.datastore = hs.get_datastore()
        self.config = hs.config
        self.identity_handler = hs.get_handlers().identity_handler

        if self.config.email_password_reset_behaviour == "local":
            from synapse.push.mailer import Mailer, load_jinja2_templates

            templates = load_jinja2_templates(
                config=hs.config,
                template_html_name=hs.config.email_password_reset_template_html,
                template_text_name=hs.config.email_password_reset_template_text,
            )
            self.mailer = Mailer(
                hs=self.hs,
                app_name=self.config.email_app_name,
                template_html=templates[0],
                template_text=templates[1],
            )

    @defer.inlineCallbacks
    def on_POST(self, request):
        if self.config.email_password_reset_behaviour == "off":
            if self.config.password_resets_were_disabled_due_to_email_config:
                logger.warn(
                    "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"
            )

        body = parse_json_object_from_request(request)

        assert_params_in_dict(body, ["client_secret", "email", "send_attempt"])

        # Extract params from body
        client_secret = body["client_secret"]
        email = body["email"]
        send_attempt = body["send_attempt"]
        next_link = body.get("next_link")  # Optional param

        if not check_3pid_allowed(self.hs, "email", email):
            raise SynapseError(
                403,
                "Your email domain is not authorized on this server",
                Codes.THREEPID_DENIED,
            )

        existingUid = yield self.hs.get_datastore().get_user_id_by_threepid(
            "email", email
        )

        if existingUid is None:
            raise SynapseError(400, "Email not found", Codes.THREEPID_NOT_FOUND)

        if self.config.email_password_reset_behaviour == "remote":
            if "id_server" not in body:
                raise SynapseError(400, "Missing 'id_server' param in body")

            # Have the identity server handle the password reset flow
            ret = yield self.identity_handler.requestEmailToken(
                body["id_server"], email, client_secret, send_attempt, next_link
            )
        else:
            # Send password reset emails from Synapse
            sid = yield self.send_password_reset(
                email, client_secret, send_attempt, next_link
            )

            # Wrap the session id in a JSON object
            ret = {"sid": sid}

        defer.returnValue((200, ret))

    @defer.inlineCallbacks
    def send_password_reset(self, email, client_secret, send_attempt, next_link=None):
        """Send a password reset email

        Args:
            email (str): The user's email address
            client_secret (str): The provided client secret
            send_attempt (int): Which send attempt this is

        Returns:
            The new session_id upon success

        Raises:
            SynapseError is an error occurred when sending the email
        """
        # Check that this email/client_secret/send_attempt combo is new or
        # greater than what we've seen previously
        session = yield self.datastore.get_threepid_validation_session(
            "email", client_secret, address=email, validated=False
        )

        # Check to see if a session already exists and that it is not yet
        # marked as validated
        if session and session.get("validated_at") is None:
            session_id = session["session_id"]
            last_send_attempt = session["last_send_attempt"]

            # Check that the send_attempt is higher than previous attempts
            if send_attempt <= last_send_attempt:
                # If not, just return a success without sending an email
                defer.returnValue(session_id)
        else:
            # An non-validated session does not exist yet.
            # Generate a session id
            session_id = random_string(16)

        # Generate a new validation token
        token = random_string(32)

        # Send the mail with the link containing the token, client_secret
        # and session_id
        try:
            yield self.mailer.send_password_reset_mail(
                email, token, client_secret, session_id
            )
        except Exception:
            logger.exception("Error sending a password reset email to %s", email)
            raise SynapseError(
                500, "An error was encountered when sending the password reset email"
            )

        token_expires = (
            self.hs.clock.time_msec() + self.config.email_validation_token_lifetime
        )

        yield self.datastore.start_or_continue_validation_session(
            "email",
            email,
            session_id,
            client_secret,
            send_attempt,
            next_link,
            token,
            token_expires,
        )

        defer.returnValue(session_id)