Start the OpenID authentication process.  Renders an
    authentication form and accepts its POST.

    * Renders an error message if OpenID cannot be initiated

    * Requests some Simple Registration data using the OpenID
      library's Simple Registration machinery

    * Generates the appropriate trust root and return URL values for
      this application (tweak where appropriate)

    * Generates the appropriate redirect based on the OpenID protocol
      version.

def startOpenID(request):
    """
    Start the OpenID authentication process.  Renders an
    authentication form and accepts its POST.

    * Renders an error message if OpenID cannot be initiated

    * Requests some Simple Registration data using the OpenID
      library's Simple Registration machinery

    * Generates the appropriate trust root and return URL values for
      this application (tweak where appropriate)

    * Generates the appropriate redirect based on the OpenID protocol
      version.
    """
    if request.POST:
        # Start OpenID authentication (mojeid url)
        openid_url = request.POST['openid_identifier']
        c = getConsumer(request)
        error = None

        if request.session.has_key(Discovery.PREFIX + c.session_key_prefix):
            del request.session[Discovery.PREFIX + c.session_key_prefix]

        if not request.POST.has_key('ns'):
            OpenIDServiceEndpoint.openid_type_uris = SERVICE_NS
        else:
            OpenIDServiceEndpoint.openid_type_uris = request.POST.getlist('ns')

        try:
            auth_request = c.begin(openid_url)
        except DiscoveryFailure, e:
            # Some other protocol-level failure occurred.
            error = "OpenID discovery error: %s" % (str(e),)

        if error:
            # Render the page with an error.
            return renderIndexPage(request, error=error)

        # Add Simple Registration request information.  Some fields
        # are optional, some are required.  It's possible that the
        # server doesn't support sreg or won't return any of the
        # fields.
        if request.POST.has_key('sreg'):
            sreg_request = sreg.SRegRequest(
                optional=[], required=request.POST.getlist('sreg'))
            auth_request.addExtension(sreg_request)

        if True:  # request.POST.has_key('ax'):
            # Add Attribute Exchange request information.
            ax_request = ax.FetchRequest()
            # XXX - uses myOpenID-compatible schema values, which are
            # not those listed at axschema.org.
            # for uri in request.POST.getlist('ax'):
            for uri in AX_ATTRIBUTES:
                ax_request.add(
                    ax.AttrInfo(uri, required=True))
            auth_request.addExtension(ax_request)

        # Add PAPE request information.  We'll ask for
        # phishing-resistant auth and display any policies we get in
        # the response.
        requested_policies = []
        policy_prefix = 'policy_'
        for k, v in request.POST.iteritems():
            # print k,v
            if k.startswith(policy_prefix):
                policy_attr = k[len(policy_prefix):]
                if policy_attr in PAPE_POLICIES:
                    requested_policies.append(getattr(pape, policy_attr))

        print requested_policies
        if requested_policies:
            pape_request = pape.Request(requested_policies)
            auth_request.addExtension(pape_request)

        # Compute the trust root and return URL values to build the
        # redirect information.
        trust_root = util.getViewURL(request, startOpenID)
        return_to = util.getViewURL(request, finishOpenID)

        # Send the browser to the server either by sending a redirect
        # URL or by generating a POST form.
        if auth_request.shouldSendRedirect():
            url = auth_request.redirectURL(trust_root, return_to)
            return HttpResponseRedirect(url)
        else:
            # Beware: this renders a template whose content is a form
            # and some javascript to submit it upon page load.  Non-JS
            # users will have to click the form submit button to
            # initiate OpenID authentication.
            form_id = 'openid_message'
            form_html = auth_request.formMarkup(trust_root, return_to,
                                                False, {'id': form_id})
            return direct_to_template(
                request, 'consumer/request_form.html', {'html': form_html})

    return renderIndexPage(request)