Example #1
0
 def testGetBody(self):
     "Test result value of document body"
     source = DocumentSourceMock((200, CORRECT_CERTIFICATE_WITH_VALID_SIGNATURE))
     document = Document(source)
     self.assertTrue(document.verify())
     self.assertEquals(document.get_body(), VALID_BODY)
    def index(self, email=None, pgp=None, mime=None, sn=None, **params):
        cherrypy.response.headers["Content-Type"] = "text/html"
        page = [render_header()]
        if cherrypy.request.method == "POST":
            # TODO: check email for validity
            if email is None or email == "":
                return self._render_error_page(_("Missing parameter"), _("Email parameter is mandatory."))
            # TODO: check serial number for validity
            if sn is None or sn == "":
                return self._render_error_page(
                    _("Missing parameter"), _("Certificate serial number parameter is mandatory.")
                )
            document_source = DocumentSource(settings.ISSUER, sn=sn)
            document = Document(document_source)
            verified = document.verify()
            # document.verify() returns False if verification failed
            # and None upon another error
            if verified != False:
                _id = document.get_id()
                if mime:
                    signature = document.get_signature()
                    body = document.get_body()
                    message = MIMESignedMessage(settings.SMTP_USERNAME, email, render_subject(_id), body, signature)
                else:
                    if not pgp:
                        body = document.get_raw_data()
                        message = TextMessage(settings.SMTP_USERNAME, email, render_subject(_id), body)
                if pgp:
                    gnupg = GPG(gnupghome=ws_settings.GNUPG_HOME)
                    received = gnupg.receive_keys(pgp, always_trust=True)
                    if received:
                        if mime:
                            encrypted = gnupg.encrypt(message.as_string(), pgp, always_trust=True)
                            if encrypted:
                                message = MIMEEncryptedMessage(
                                    settings.SMTP_USERNAME, email, render_subject(_id), str(encrypted)
                                )
                            else:
                                return self._render_error_page(
                                    _("Something went wrong"), _("Encryption problem. Sorry for the inconvenience!")
                                )
                        else:
                            message = make_document(document.get_raw_data())
                            encrypted = gnupg.encrypt(message, pgp, always_trust=True, no_literal=True)
                            if encrypted:
                                message = TextMessage(
                                    settings.SMTP_USERNAME, email, render_subject(_id), str(encrypted)
                                )
                            else:
                                return self._render_error_page(
                                    _("Something went wrong"), _("Encryption problem. Sorry for the inconvenience!")
                                )
                    else:
                        return self._render_error_page(
                            _("Something went wrong"), _("Can not receive key. Sorry for the inconvenience!")
                        )

                m = Mailer(suppress_exceptions=False)
                try:
                    m.send(message)
                except MailerException:
                    return self._render_error_page(
                        _("Something went wrong"), _("Please try again later. Sorry for the inconvenience!")
                    )
                page.append(
                    render_message(
                        _("Request was completed successfully"),
                        _("Your receipt will be emailed shortly. Thank you for your patience!"),
                    )
                )
            else:
                return self._render_error_page(
                    _("Something went wrong"), _("Please try again later. Sorry for the inconvenience!")
                )
        else:
            page.append(
                render_message(
                    _("Malformed request"),
                    _(
                        "You should use POST request with Content-Type: application/x-www-form-urlencoded and appropriate parameters list."
                    ),
                )
            )
        page.append(render_footer())
        return page