def _v2_auth(self, token_url):
        def get_endpoint(service_catalog):
            """
            Select an endpoint from the service catalog

            We search the full service catalog for services
            matching both type and region. If the client
            supplied no region then any endpoint for the service
            is considered a match. There must be one -- and
            only one -- successful match in the catalog,
            otherwise we will raise an exception.
            """
            region = self.creds.get('region')

            service_type_matches = lambda s: s.get('type') == self.service_type
            region_matches = lambda e: region is None or e['region'] == region

            endpoints = [ep for s in service_catalog if service_type_matches(s)
                         for ep in s['endpoints'] if region_matches(ep)]

            if len(endpoints) > 1:
                raise exception.RegionAmbiguity(region=region)
            elif not endpoints:
                raise exception.NoServiceEndpoint()
            else:
                # FIXME(sirp): for now just use the public url.
                return endpoints[0]['publicURL']

        creds = self.creds

        creds = {
            "auth": {
                "tenantName": creds['tenant'],
                "passwordCredentials": {
                    "username": creds['username'],
                    "password": creds['password']}}}

        headers = {}
        headers['Content-Type'] = 'application/json'
        req_body = json.dumps(creds)

        resp, resp_body = self._do_request(
            token_url, 'POST', headers=headers, body=req_body)

        if resp.status == 200:
            resp_auth = json.loads(resp_body)['access']
            self.management_url = get_endpoint(resp_auth['serviceCatalog'])
            self.auth_token = resp_auth['token']['id']
        elif resp.status == 305:
            raise exception.RedirectException(resp['location'])
        elif resp.status == 400:
            raise exception.AuthBadRequest(url=token_url)
        elif resp.status == 401:
            raise exception.NotAuthorized()
        elif resp.status == 404:
            raise exception.AuthUrlNotFound(url=token_url)
        else:
            try:
                body = json.loads(resp_body)
                msg = body['error']['message']
            except (__HOLE__, KeyError):
                msg = resp_body
            raise exception.KeystoneError(resp.status, msg)
dataset/ETHPy150Open openstack-dev/heat-cfnclient/heat_cfnclient/common/auth.py/KeystoneStrategy._v2_auth