Beispiel #1
0
 def _verify_token(self, session_id, message, token):
     if token is None:
         raise InvalidAuthenticationToken('Authentication token not found')
     to_auth = get_auth_message(message)
     api_key = self.sessions.get(session_id)
     if not api_key.compare_hmac(to_auth, token):
         raise InvalidAuthenticationToken('Invalid authentication token')
Beispiel #2
0
 def _verify_token(self, session_id, message, token):
     if token is None:
         raise InvalidAuthenticationToken('Authentication token not found')
     to_auth = get_auth_message(message)
     api_key = self.sessions.get(session_id)
     if not api_key.compare_hmac(to_auth, token):
         raise InvalidAuthenticationToken('Invalid authentication token')
Beispiel #3
0
    def call(self, method, params={}):
        self.__id_count += 1
        pre_auth_post_data = {
            'version': '2',
            'method': method,
            'params': params,
            'id': self.__id_count
        }
        to_auth = get_auth_message(pre_auth_post_data)
        token = self.__api_key.get_hmac(to_auth)
        pre_auth_post_data.update({'hmac': token})
        post_data = json.dumps(pre_auth_post_data)
        service_url = self.__service_url
        auth_header = self.__auth_header
        cookies = self.__cookies
        host = self.__url.hostname

        req = requests.Request(method='POST',
                               url=service_url,
                               data=post_data,
                               headers={
                                   'Host': host,
                                   'User-Agent': USER_AGENT,
                                   'Authorization': auth_header,
                                   'Content-type': 'application/json'
                               },
                               cookies=cookies)
        r = req.prepare()
        http_response = self.__conn.send(r)
        cookies = http_response.cookies
        headers = http_response.headers
        next_secret = headers.get(LBRY_SECRET, False)
        if next_secret:
            self.__api_key.secret = next_secret
            self.__cookies = cookies

        if http_response is None:
            raise JSONRPCException({
                'code':
                -342,
                'message':
                'missing HTTP response from server'
            })

        http_response.raise_for_status()

        response = http_response.json()

        if response.get('error') is not None:
            raise JSONRPCException(response['error'])
        elif 'result' not in response:
            raise JSONRPCException({
                'code': -343,
                'message': 'missing JSON-RPC result'
            })
        else:
            return response['result']
Beispiel #4
0
    async def call(self, method, params=None):
        params = params or {}
        self.__id_count += 1

        pre_auth_post_data = {
            'version': '2',
            'method': method,
            'params': params,
            'id': self.__id_count
        }
        to_auth = get_auth_message(pre_auth_post_data)
        auth_msg = self.__api_key.get_hmac(to_auth).decode()
        pre_auth_post_data.update({'hmac': auth_msg})
        post_data = json.dumps(pre_auth_post_data)

        headers = {
            'Host': self.__url.hostname,
            'User-Agent': USER_AGENT,
            'Content-type': 'application/json'
        }

        async with self.session.post(self.__login_url,
                                     data=post_data,
                                     headers=headers) as resp:
            if resp is None:
                raise JSONRPCException({
                    'code':
                    -342,
                    'message':
                    'missing HTTP response from server'
                })
            resp.raise_for_status()

            next_secret = resp.headers.get(LBRY_SECRET, False)
            if next_secret:
                self.__api_key.secret = next_secret

            return await resp.json()