'Handle\'s response from OpenID Providers'

@web.expose
def openid_process(self, trans, **kwd):

    if (not trans.app.config.enable_openid):
        return trans.show_error_message('OpenID    authentication    is    not    enabled    in    this    instance    of    Galaxy')
    auto_associate = util.string_as_bool(kwd.get('auto_associate', False))
    action = 'login'
    if trans.user:
        action = 'openid_manage'
    if (trans.app.config.support_url is not None):
        contact = ('<a    href="%s">support</a>' % trans.app.config.support_url)
    else:
        contact = 'support'
    message = ('Verification    failed    for    an    unknown    reason.        Please    contact    %s    for    assistance.' % contact)
    status = 'error'
    consumer = trans.app.openid_manager.get_consumer(trans)
    info = consumer.complete(kwd, trans.request.url)
    display_identifier = info.getDisplayIdentifier()
    redirect = kwd.get('redirect', '').strip()
    openid_provider = kwd.get('openid_provider', None)
    if ((info.status == trans.app.openid_manager.FAILURE) and display_identifier):
        message = ('Login    via    OpenID    failed.        The    technical    reason    for    this    follows,    please    include    this    message    in    your    email    if    you    need    to    %s    to    resolve    this    problem:    %s' % (contact, info.message))
        return trans.response.send_redirect(url_for(controller='user', action=action, use_panels=True, redirect=redirect, message=message, status='error'))
    elif (info.status == trans.app.openid_manager.SUCCESS):
        if info.endpoint.canonicalID:
            display_identifier = info.endpoint.canonicalID
        openid_provider_obj = trans.app.openid_providers.get(openid_provider)
        user_openid = trans.sa_session.query(trans.app.model.UserOpenID).filter((trans.app.model.UserOpenID.table.c.openid == display_identifier)).first()
        if ((not openid_provider_obj) and user_openid and user_openid.provider):
            openid_provider_obj = trans.app.openid_providers.get(user_openid.provider)
        if (not openid_provider_obj):
            openid_provider_obj = trans.app.openid_providers.new_provider_from_identifier(display_identifier)
        if (not user_openid):
            user_openid = trans.app.model.UserOpenID(session=trans.galaxy_session, openid=display_identifier)
        if (not user_openid.user):
            user_openid.session = trans.galaxy_session
        if ((not user_openid.provider) and openid_provider):
            user_openid.provider = openid_provider
        if trans.user:
            if (user_openid.user and (user_openid.user.id != trans.user.id)):
                message = ('The    OpenID    <strong>%s</strong>    is    already    associated    with    another    Galaxy    account,    <strong>%s</strong>.        Please    disassociate    it    from    that    account    before    attempting    to    associate    it    with    a    new    account.' % (escape(display_identifier), escape(user_openid.user.email)))
            if ((not trans.user.active) and trans.app.config.user_activation_on):
                if (trans.app.config.activation_grace_period != 0):
                    if self.is_outside_grace_period(trans, trans.user.create_time):
                        (message, status) = self.resend_verification_email(trans, trans.user.email, trans.user.username)
                    else:
                        pass
                else:
                    (message, status) = self.resend_verification_email(trans, trans.user.email, trans.user.username)
            elif ((not user_openid.user) or (user_openid.user == trans.user)):
                if openid_provider_obj.id:
                    user_openid.provider = openid_provider_obj.id
                user_openid.session = trans.galaxy_session
                if (not openid_provider_obj.never_associate_with_user):
                    if ((not auto_associate) and (user_openid.user and (user_openid.user.id == trans.user.id))):
                        message = ('The    OpenID    <strong>%s</strong>    is    already    associated    with    your    Galaxy    account,    <strong>%s</strong>.' % (escape(display_identifier), escape(trans.user.email)))
                        status = 'warning'
                    else:
                        message = ('The    OpenID    <strong>%s</strong>    has    been    associated    with    your    Galaxy    account,    <strong>%s</strong>.' % (escape(display_identifier), escape(trans.user.email)))
                        status = 'done'
                    user_openid.user = trans.user
                    trans.sa_session.add(user_openid)
                    trans.sa_session.flush()
                    trans.log_event(('User    associated    OpenID:    %s' % display_identifier))
                else:
                    message = ('The    OpenID    <strong>%s</strong>    cannot    be    used    to    log    into    your    Galaxy    account,    but    any    post    authentication    actions    have    been    performed.' % escape(openid_provider_obj.name))
                    status = 'info'
                openid_provider_obj.post_authentication(trans, trans.app.openid_manager, info)
                if redirect:
                    message = ('%s<br>Click    <a    href="%s"><strong>here</strong></a>    to    return    to    the    page    you    were    previously    viewing.' % (message, escape(self.__get_redirect_url(redirect))))
            if (redirect and (status != 'error')):
                return trans.response.send_redirect(self.__get_redirect_url(redirect))
            return trans.response.send_redirect(url_for(controller='user', action='openid_manage', use_panels=True, redirect=redirect, message=message, status=status))
        elif user_openid.user:
            trans.handle_user_login(user_openid.user)
            trans.log_event(('User    logged    in    via    OpenID:    %s' % display_identifier))
            openid_provider_obj.post_authentication(trans, trans.app.openid_manager, info)
            if (not redirect):
                redirect = url_for('/')
            redirect = self.__get_redirect_url(redirect)
            return trans.response.send_redirect(redirect)
        trans.sa_session.add(user_openid)
        trans.sa_session.flush()
        message = 'OpenID    authentication    was    successful,    but    you    need    to    associate    your    OpenID    with    a    Galaxy    account.'
        sreg_resp = trans.app.openid_manager.get_sreg(info)
        try:
            sreg_username_name = openid_provider_obj.use_for.get('username')
            username = sreg_resp.get(sreg_username_name, '')
        except AttributeError:
            username = ''
        try:
            sreg_email_name = openid_provider_obj.use_for.get('email')
            email = sreg_resp.get(sreg_email_name, '')
        except AttributeError:
            email = ''
        return trans.response.send_redirect(url_for(controller='user', action='openid_associate', use_panels=True, redirect=redirect, username=username, email=email, message=message, status='warning'))
    elif (info.status == trans.app.openid_manager.CANCEL):
        message = "Login    via    OpenID    was    cancelled    by    an    action    at    the    OpenID    provider's    site."
        status = 'warning'
    elif (info.status == trans.app.openid_manager.SETUP_NEEDED):
        if info.setup_url:
            return trans.response.send_redirect(info.setup_url)
        else:
            message = "Unable    to    log    in    via    OpenID.        Setup    at    the    provider    is    required    before    this    OpenID    can    be    used.        Please    visit    your    provider's    site    to    complete    this    step."
    return trans.response.send_redirect(url_for(controller='user', action=action, use_panels=True, redirect=redirect, message=message, status=status))
