'If more than one idp and if none is selected, I have to do wayf or
disco'

def _pick_idp(self, environ, came_from):

    _cli = self.saml_client
    logger.info(('[_pick_idp]    %s' % environ))
    if ('HTTP_PAOS' in environ):
        if (environ['HTTP_PAOS'] == PAOS_HEADER_INFO):
            if ('application/vnd.paos+xml' in environ['HTTP_ACCEPT']):
                logger.info('-    ECP    client    detected    -')
                _relay_state = construct_came_from(environ)
                _entityid = _cli.config.ecp_endpoint(environ['REMOTE_ADDR'])
                if (not _entityid):
                    return ((-1), HTTPInternalServerError(detail='No    IdP    to    talk    to'))
                logger.info(('IdP    to    talk    to:    %s' % _entityid))
                return ecp.ecp_auth_request(_cli, _entityid, _relay_state)
            else:
                return ((-1), HTTPInternalServerError(detail='Faulty    Accept    header'))
        else:
            return ((-1), HTTPInternalServerError(detail='unknown    ECP    version'))
    idps = self.metadata.with_descriptor('idpsso')
    logger.info(('IdP    URL:    %s' % idps))
    idp_entity_id = query = None
    for key in ['s2repoze.body', 'QUERY_STRING']:
        query = environ.get(key)
        if query:
            try:
                _idp_entity_id = dict(parse_qs(query))[self.idp_query_param][0]
                if (_idp_entity_id in idps):
                    idp_entity_id = _idp_entity_id
                break
            except KeyError:
                logger.debug(('No    IdP    entity    ID    in    query:    %s' % query))
                pass
    if (idp_entity_id is None):
        if (len(idps) == 1):
            idp_entity_id = idps.keys()[0]
        elif (not len(idps)):
            return ((-1), HTTPInternalServerError(detail='Misconfiguration'))
        else:
            idp_entity_id = ''
            logger.info(('ENVIRON:    %s' % environ))
            if self.wayf:
                if query:
                    try:
                        wayf_selected = dict(parse_qs(query))['wayf_selected'][0]
                    except KeyError:
                        return self._wayf_redirect(came_from)
                    idp_entity_id = wayf_selected
                else:
                    return self._wayf_redirect(came_from)
            elif self.discosrv:
                if query:
                    idp_entity_id = _cli.parse_discovery_service_response(query=environ.get('QUERY_STRING'))
                else:
                    sid_ = sid()
                    self.outstanding_queries[sid_] = came_from
                    logger.debug('Redirect    to    Discovery    Service    function')
                    eid = _cli.config.entityid
                    ret = _cli.config.getattr('endpoints', 'sp')['discovery_response'][0][0]
                    ret += ('?sid=%s' % sid_)
                    loc = _cli.create_discovery_service_request(self.discosrv, eid, **{'return': ret})
                    return ((-1), SeeOther(loc))
            else:
                return ((-1), HTTPNotImplemented(detail='No    WAYF    or    DJ    present!'))
    logger.info(("Chosen    IdP:    '%s'" % idp_entity_id))
    return (0, idp_entity_id)
