def test_toUnicode(self):
     # Unicode objects pass through
     self.failUnless(isinstance(oidutil.toUnicode(u'fööbär'), unicode))
     self.assertEquals(oidutil.toUnicode(u'fööbär'), u'fööbär')
     # UTF-8 encoded string are decoded
     self.failUnless(isinstance(oidutil.toUnicode('fööbär'), unicode))
     self.assertEquals(oidutil.toUnicode('fööbär'), u'fööbär')
     # Other encodings raise exceptions
     self.assertRaises(UnicodeDecodeError, lambda: oidutil.toUnicode(u'fööbär'.encode('latin-1')))
Exemple #2
0
    def toFormMarkup(self,
                     action_url,
                     form_tag_attrs=None,
                     submit_text="Continue"):
        """Generate HTML form markup that contains the values in this
        message, to be HTTP POSTed as x-www-form-urlencoded UTF-8.

        @param action_url: The URL to which the form will be POSTed
        @type action_url: str

        @param form_tag_attrs: Dictionary of attributes to be added to
            the form tag. 'accept-charset' and 'enctype' have defaults
            that can be overridden. If a value is supplied for
            'action' or 'method', it will be replaced.
        @type form_tag_attrs: {unicode: unicode}

        @param submit_text: The text that will appear on the submit
            button for this form.
        @type submit_text: unicode

        @returns: A string containing (X)HTML markup for a form that
            encodes the values in this Message object.
        @rtype: str
        """
        if ElementTree is None:
            raise RuntimeError('This function requires ElementTree.')

        assert action_url is not None

        form = ElementTree.Element('form')

        if form_tag_attrs:
            for name, attr in form_tag_attrs.items():
                form.attrib[name] = attr

        form.attrib['action'] = oidutil.toUnicode(action_url)
        form.attrib['method'] = 'post'
        form.attrib['accept-charset'] = 'UTF-8'
        form.attrib['enctype'] = 'application/x-www-form-urlencoded'

        for name, value in self.toPostArgs().items():
            attrs = {
                'type': 'hidden',
                'name': oidutil.toUnicode(name),
                'value': oidutil.toUnicode(value)
            }
            form.append(ElementTree.Element('input', attrs))

        submit = ElementTree.Element('input', {
            'type': 'submit',
            'value': oidutil.toUnicode(submit_text)
        })
        form.append(submit)

        return str(ElementTree.tostring(form, encoding='utf-8'),
                   encoding="utf-8")
Exemple #3
0
    def toFormMarkup(self, action_url, form_tag_attrs=None,
                     submit_text=u"Continue"):
        """Generate HTML form markup that contains the values in this
        message, to be HTTP POSTed as x-www-form-urlencoded UTF-8.

        @param action_url: The URL to which the form will be POSTed
        @type action_url: str

        @param form_tag_attrs: Dictionary of attributes to be added to
            the form tag. 'accept-charset' and 'enctype' have defaults
            that can be overridden. If a value is supplied for
            'action' or 'method', it will be replaced.
        @type form_tag_attrs: {unicode: unicode}

        @param submit_text: The text that will appear on the submit
            button for this form.
        @type submit_text: unicode

        @returns: A string containing (X)HTML markup for a form that
            encodes the values in this Message object.
        @rtype: str or unicode
        """
        if ElementTree is None:
            raise RuntimeError('This function requires ElementTree.')

        assert action_url is not None

        form = ElementTree.Element(u'form')

        if form_tag_attrs:
            for name, attr in form_tag_attrs.iteritems():
                form.attrib[name] = attr

        form.attrib['action'] = oidutil.toUnicode(action_url)
        form.attrib['method'] = u'post'
        form.attrib['accept-charset'] = u'UTF-8'
        form.attrib['enctype'] = u'application/x-www-form-urlencoded'

        for name, value in self.toPostArgs().iteritems():
            attrs = {u'type': u'hidden',
                     u'name': oidutil.toUnicode(name),
                     u'value': oidutil.toUnicode(value)}

            form.append(ElementTree.Element(u'input', attrs))
        submit = ElementTree.Element(
                  u'input',{
                        u'type':'submit', 
                        u'value':oidutil.toUnicode(submit_text)}
                  )

        form.append(submit)
        return ElementTree.tostring(form, encoding='utf-8')
Exemple #4
0
    def toPostArgs(self):
        """Return all arguments with openid. in front of namespaced arguments.
        """
        args = {}

        # Add namespace definitions to the output
        for ns_uri, alias in self.namespaces.iteritems():
            if self.namespaces.isImplicit(ns_uri):
                continue
            if alias == NULL_NAMESPACE:
                ns_key = 'openid.ns'
            else:
                ns_key = 'openid.ns.' + alias
            args[ns_key] = oidutil.toUnicode(ns_uri).encode('UTF-8')

        for (ns_uri, ns_key), value in self.args.iteritems():
            key = self.getKey(ns_uri, ns_key)
            # Ensure the resulting value is an UTF-8 encoded bytestring.
            args[key] = oidutil.toUnicode(value).encode('UTF-8')

        return args
Exemple #5
0
    def toPostArgs(self):
        """Return all arguments with openid. in front of namespaced arguments.
        """
        args = {}

        # Add namespace definitions to the output
        for ns_uri, alias in self.namespaces.iteritems():
            if self.namespaces.isImplicit(ns_uri):
                continue
            if alias == NULL_NAMESPACE:
                ns_key = 'openid.ns'
            else:
                ns_key = 'openid.ns.' + alias
            args[ns_key] = oidutil.toUnicode(ns_uri).encode('UTF-8')

        for (ns_uri, ns_key), value in self.args.iteritems():
            key = self.getKey(ns_uri, ns_key)
            # Ensure the resulting value is an UTF-8 encoded bytestring.
            args[key] = oidutil.toUnicode(value).encode('UTF-8')

        return args
Exemple #6
0
    def defaultFormBuilder(msg, action_url, form_tag_attrs, submit_text):
        form = ElementTree.Element(u'form')

        if form_tag_attrs:
            for name, attr in form_tag_attrs.iteritems():
                form.attrib[name] = attr

        form.attrib[u'action'] = oidutil.toUnicode(action_url)
        form.attrib[u'method'] = u'post'
        form.attrib[u'accept-charset'] = u'UTF-8'
        form.attrib[u'enctype'] = u'application/x-www-form-urlencoded'
        
        for name, value in msg.toPostArgs().iteritems():
            attrs = {u'type': u'hidden',
                     u'name': oidutil.toUnicode(name),
                     u'value': oidutil.toUnicode(value)}
            form.append(ElementTree.Element(u'input', attrs))

        submit = ElementTree.Element(u'input',
            {u'type':'submit', u'value':oidutil.toUnicode(submit_text)})
        form.append(submit)
        return form