(self, www_authenticate, retry)
return auth_header
Authentication for v2 API
Authentication for v2 API
Authentication for v2 API
def _get_v2_auth(self, www_authenticate, retry):
        """Authentication for v2 API"""
        auth_header = ""
        (bearer, auth_data) = www_authenticate.rsplit(' ', 1)
        if bearer == "Bearer":
            auth_fields = self._split_fields(auth_data)
            if "realm" in auth_fields:
                auth_url = auth_fields["realm"] + '?'
                for field in auth_fields:
                    if field != "realm":
                        auth_url += field + '=' + auth_fields[field] + '&'
                header = []
                if self.v2_auth_token:
                    header = ["Authorization: Basic %s" % (self.v2_auth_token)]
                (dummy, auth_buf) = self._get_url(auth_url, header=header,
                                                  RETRY=retry)
                if sys.version_info[0] >= 3:
                    token_buf = auth_buf.getvalue().decode()
                else:
                    token_buf = auth_buf.getvalue()
                if token_buf and "token" in token_buf:
                    try:
                        auth_token = json.loads(token_buf)
                    except (IOError, OSError, AttributeError,
                            ValueError, TypeError):
                        return auth_header
                    auth_header = "Authorization: Bearer " + \
                        auth_token["token"]
                    self.v2_auth_header = auth_header
        # PR #126
        elif 'BASIC' in bearer or 'Basic' in bearer:
            auth_header = "Authorization: Basic %s" %(self.v2_auth_token)
            self.v2_auth_header = auth_header
        return auth_header
def _get_v2_auth ( self , www_authenticate , retry ) : auth_header = "" ( bearer , auth_data ) = www_authenticate . rsplit ( ' ' , 1 ) if bearer == "Bearer" : auth_fields = self . _split_fields ( auth_data ) if "realm" in auth_fields : auth_url = auth_fields [ "realm" ] + '?' for field in auth_fields : if field != "realm" : auth_url += field + '=' + auth_fields [ field ] + '&' header = [ ] if self . v2_auth_token : header = [ "Authorization: Basic %s" % ( self . v2_auth_token ) ] ( dummy , auth_buf ) = self . _get_url ( auth_url , header = header , RETRY = retry ) if sys . version_info [ 0 ] >= 3 : token_buf = auth_buf . getvalue ( ) . decode ( ) else : token_buf = auth_buf . getvalue ( ) if token_buf and "token" in token_buf : try : auth_token = json . loads ( token_buf ) except ( IOError , OSError , AttributeError , ValueError , TypeError ) : return auth_header auth_header = "Authorization: Bearer " + auth_token [ "token" ] self . v2_auth_header = auth_header # PR #126 elif 'BASIC' in bearer or 'Basic' in bearer : auth_header = "Authorization: Basic %s" % ( self . v2_auth_token ) self . v2_auth_header = auth_header return auth_header