Exemplo n.º 1
0
def renderer_globals_factory(system):
    """Returns a dictionary of mappings that are available as global
    parameters in each renderer.
    """
    request = system.get('request')
    username = authenticated_userid(request)
    identity = None
    if username is not None:
        try:
            identity = identity_url(request, username)
        except ValueError:
            # We received a curious error in production from the underlying
            # openid.urinorm.urinorm() function which indicated that the
            # username passed contains an invalid charact (most likely a
            # whitespace character within a domain name).
            # Until we can fully understand the cause of the error we simply
            # log the incident here and let the error propagate.
            log = logging.getLogger('webidentity')
            log.warn('identity_url() failed for username "{0}".'.format(username))
            raise

    return {
        'flash_messages': request.session.pop_flash(),
        'identity': identity,
        'main': get_renderer('templates/master.pt').implementation(),
        'username': username,
    }
Exemplo n.º 2
0
    def create_message(self, user, reset):
        """Returns an email.message.Message object representing the password
        reset message.
        """
        from_address = self.request.registry.settings['webidentity_from_address'].strip()
        date_format = self.request.registry.settings['webidentity_date_format'].strip()
        locale = get_localizer(self.request)
        subject = locale.translate(
            _(u'Password reset for ${identity}',
            mapping={'identity': identity_url(self.request, user.username)}))

        message = Message()
        message['From'] = Header(from_address, 'utf-8')
        message['To'] = Header(u'{0} <{1}>'.format(user.username, user.email), 'utf-8')
        message['Subject'] = Header(subject, 'utf-8')

        message.set_payload(locale.translate(_(
            u'password-reset-email',
            default=textwrap.dedent(u'''
            Hi ${username}

            A password retrieval process has been initiated for your OpenID
            identity

              ${identity}

            If the process was initiated by you you can continue the process
            of resetting your password by opening the following link in your
            browser

              ${reset_url}

            The link will will expire at ${expiration}.

            If you did not initiate this request you can just ignore this
            email. Your password has not been changed.
            ''').lstrip(),
            mapping=dict(
                username=user.username,
                identity=identity_url(self.request, user.username),
                expiration=reset.expires.strftime(date_format),
                reset_url=route_url('reset_password_token', self.request, token=reset.token)))))

        return message