Пример #1
0
    def _Refresh(self):
        """Refreshes the Bearer token credentials underlying this transport.

    This utilizes the "realm" and "service" established during _Ping to
    set up _creds with up-to-date credentials, by passing the
    client-provided _basic_creds to the authorization realm.

    This is generally called under two circumstances:
      1) When the transport is created (eagerly)
      2) When a request fails on a 401 Unauthorized

    Raises:
      TokenRefreshException: Error during token exchange.
    """
        headers = {
            'content-type': 'application/json',
            'user-agent': docker_name.USER_AGENT,
            'Authorization': self._basic_creds.Get()
        }
        parameters = {
            'scope': self._Scope(),
            'service': self._service,
        }
        resp, content = self._transport.request(
            # 'realm' includes scheme and path
            '{realm}?{query}'.format(
                realm=self._realm,
                query=six.moves.urllib.parse.urlencode(parameters)),
            'GET',
            body=None,
            headers=headers)

        if resp.status != six.moves.http_client.OK:
            raise TokenRefreshException(
                'Bad status during token exchange: %d\n%s' %
                (resp.status, content))

        try:
            content = content.decode('utf8')
        except:  # pylint: disable=bare-except
            # Assume it's already decoded. Defensive coding for old py2 habits that
            # are hard to break. Passing does not make the problem worse.
            pass
        wrapper_object = json.loads(content)
        token = wrapper_object.get('token') or wrapper_object.get(
            'access_token')
        _CheckState(token is not None, 'Malformed JSON response: %s' % content)

        with self._lock:
            # We have successfully reauthenticated.
            self._creds = v2_2_creds.Bearer(token)
Пример #2
0
    def _Refresh(self):
        """Refreshes the Bearer token credentials underlying this transport.

    This utilizes the "realm" and "service" established during _Ping to
    set up _creds with up-to-date credentials, by passing the
    client-provided _basic_creds to the authorization realm.

    This is generally called under two circumstances:
      1) When the transport is created (eagerly)
      2) When a request fails on a 401 Unauthorized

    Raises:
      TokenRefreshException: Error during token exchange.
    """
        headers = {
            'content-type': 'application/json',
            'user-agent': docker_name.USER_AGENT,
            'Authorization': self._basic_creds.Get()
        }
        parameters = {
            'scope': self._Scope(),
            'service': self._service,
        }
        resp, content = self._transport.request(
            # 'realm' includes scheme and path
            '{realm}?{query}'.format(realm=self._realm,
                                     query=urllib.urlencode(parameters)),
            'GET',
            body=None,
            headers=headers)

        if resp.status != httplib.OK:
            raise TokenRefreshException(
                'Bad status during token exchange: %d\n%s' %
                (resp.status, content))

        wrapper_object = json.loads(content)
        _CheckState('token' in wrapper_object,
                    'Malformed JSON response: %s' % content)

        with self._lock:
            # We have successfully reauthenticated.
            self._creds = v2_2_creds.Bearer(wrapper_object['token'])