示例#1
0
def main(args):
    username = args.username
    if username is None:
        username = input("Username: ")

    password = args.password
    if password is None:
        password = getpass.getpass()

    # Set up logging
    logging.basicConfig(format='local proxy: %(asctime)s %(message)s',
                        datefmt='%m/%d/%Y %I:%M:%S %p',
                        level=args.loglvl,
                        filename=args.logfile)

    app = web.Application()
    basic_auth = BasicAuth(username, password)
    proxy_handler = partial(_proxy_handler,
                            target_url=args.target_url,
                            auth=basic_auth)
    app.router.add_route('*', '/{path:.*?}', proxy_handler)
    try:
        web.run_app(app, host='0.0.0.0', port=args.listen_port)
    except KeyboardInterrupt:
        asyncio.get_event_loop().run_until_complete(_cleanup())
def get_rabbitmq_queues_stats(agent):
    yield from agent.run_event.wait()
    config = agent.config['rabbitmq']
    logger.info('starting "get_rabbitmq_queues_stats" task for "%s"',
                config['hostname'])
    db_config = config['database']
    monitored_queues = config['queues'].split(',')
    auth = BasicAuth(config['username'], config['password'])
    yield from agent.async_create_database(**db_config)

    while agent.run_event.is_set():
        yield from asyncio.sleep(config['frequency'])
        api_url = config['api_url']
        response = yield from aiohttp.get(api_url, auth=auth)
        if response.status == 200:
            data = yield from response.json()
            for queue_info in data:
                if queue_info['name'] in monitored_queues:
                    print(queue_info)
                    points = [{
                        'measurement': 'queues',
                            'tags': {
                                'name': queue_info['name']
                            },
                            'fields': {
                                'memory': queue_info['memory']
                            }}]
                    yield from agent.async_push(points, db_config['name'])
    logger.info('get_rabbitmq_queues_stats terminated')
示例#3
0
def bing(arg, lines, send):
    print('bing')

    arg.update({
        'n': arg['n'] or '1',
        'url': 'https://api.datamarket.azure.com/Bing/Search/v1/Composite',
        'xpath': '//d/results/item/Web/item',
    })
    params = {
        '$format': 'json',
        #'Sources': "'web+image+video+news+spell'",
        'Sources': "'web'",
        'Adult': "'Off'",
        'Market': "'en-US'",
        'Query': "'{0}'".format(' '.join(lines) or arg['query'] or ''),
    }
    key = arg['meta']['bot'].key['microsoft']
    auth = BasicAuth(key, key)
    field = [
        ('./Title', 'text', '{}'),
        ('./Url', 'text', '[\\x0302 {} \\x0f]'),
        ('./Description', 'text', '{}'),
    ]

    return (yield from jsonxml(arg, [], send, params=params, auth=auth, field=field))
示例#4
0
 def __get_auth(self):
     request = CrequestMiddleware.get_request()
     if request is not None and hasattr(request, "user") and request.user is not None and\
             not request.user.is_anonymous():
         return BasicAuth(login=request.user.username,
                          password=request.session.get("password"))
     else:
         return None
 async def webhook_handler(request: web.Request):
     payload = await request.json()
     auth = BasicAuth.decode(request.headers["Authorization"])
     return web.json_response({
         "username": auth.login,
         "password": auth.password,
         "payload": payload
     })
示例#6
0
async def test_verify_client(hass, aiohttp_client, mock_view):
    """Test that verify client can extract client auth from a request."""
    http_client = await async_setup_auth(hass, aiohttp_client)
    client = await hass.auth.async_create_client('Hello')

    resp = await http_client.get('/', auth=BasicAuth(client.id, client.secret))
    assert resp.status == 200
    assert mock_view == [client.id]
示例#7
0
async def test_verify_client_invalid_client_id(hass, aiohttp_client,
                                               mock_view):
    """Test that verify client will decline unknown client id."""
    http_client = await async_setup_auth(hass, aiohttp_client)
    client = await hass.auth.async_create_client('Hello')

    resp = await http_client.get('/', auth=BasicAuth('invalid', client.secret))
    assert resp.status == 401
    assert mock_view == []
示例#8
0
async def test_verify_client_invalid_client_secret(hass, aiohttp_client,
                                                   mock_view):
    """Test that verify client will decline incorrect client secret."""
    http_client = await async_setup_auth(hass, aiohttp_client)
    client = await hass.auth.async_create_client('Hello')

    resp = await http_client.get('/', auth=BasicAuth(client.id, 'invalid'))
    assert resp.status == 401
    assert mock_view == []
示例#9
0
    async def upload(self, stream, path, **kwargs):
        """Zips the given stream then uploads to Dataverse.
        This will delete existing draft files with the same name.

        :param waterbutler.core.streams.RequestWrapper stream: The stream to put to Dataverse
        :param str path: The filename prepended with '/'

        :rtype: dict, bool
        """

        stream.add_writer('md5', streams.HashStreamWriter(hashlib.md5))

        zip_stream = streams.ZipStreamReader(
            AsyncIterator([(path.name, stream)]))

        # Write stream to disk (Necessary to find zip file size)
        f = tempfile.TemporaryFile()
        chunk = await zip_stream.read()
        while chunk:
            f.write(chunk)
            chunk = await zip_stream.read()
        file_stream = streams.FileStreamReader(f)

        dv_headers = {
            "Content-Disposition": "filename=temp.zip",
            "Content-Type": "application/zip",
            "Packaging": "http://purl.org/net/sword/package/SimpleZip",
            "Content-Length": str(file_stream.size),
        }

        # Delete old file if it exists
        if path.identifier:
            await self.delete(path)

        resp = await self.make_request('POST',
                                       self.build_url(
                                           pd_settings.EDIT_MEDIA_BASE_URL,
                                           'study', self.doi),
                                       headers=dv_headers,
                                       auth=BasicAuth(self.token),
                                       data=file_stream,
                                       expects=(201, ),
                                       throws=exceptions.UploadError)
        await resp.release()

        # Find appropriate version of file
        metadata = await self._get_data('latest')
        files = metadata if isinstance(metadata, list) else []
        file_metadata = next(file for file in files if file.name == path.name)

        if stream.writers['md5'].hexdigest != file_metadata.extra['hashes'][
                'md5']:
            raise exceptions.UploadChecksumMismatchError()

        return file_metadata, path.identifier is None
示例#10
0
 def setup_sessions(self):
     for i in range(self.config.max_connections):
         if self.config.auth:
             auth = BasicAuth(login=self.config.auth[0],
                              password=self.config.auth[1])
         else:
             auth = None
         session = ClientSession(
             connector=self.conn,
             headers={'User-Agent': self.config.user_agent},
             timeout=ClientTimeout(total=self.config.timeout),
             skip_auto_headers=['Accept-Encoding'],
             connector_owner=False,
             auth=auth)
         self.sessions.append(session)
示例#11
0
    async def get_access_token(self):
        """Asynchronously attempts to get an access token from the homee host using username and password."""

        # Check if current token is still valid
        if self.token != None and self.expires > datetime.now().timestamp():
            return self.token

        client = aiohttp.ClientSession()
        auth = BasicAuth(
            self.user, hashlib.sha512(self.password.encode("utf-8")).hexdigest()
        )
        url = f"{self.url}/access_token"
        headers = {"Content-Type": "application/x-www-form-urlencoded"}
        data = {
            "device_name": self.device,
            "device_hardware_id": self.deviceId,
            "device_os": DeviceOS.LINUX,
            "device_type": DeviceType.NONE,
            "device_app": DeviceApp.HOMEE,
        }

        try:
            req = await client.post(
                url, auth=auth, data=data, headers=headers, timeout=5
            )
        except Exception as e:
            await client.close()
            raise e

        try:
            reqText = await req.text()
            regex = r"^access_token=([0-z]+)&.*&expires=(\d+)$"
            matches = re.match(regex, reqText)

            self.token = matches[1]
            self.expires = datetime.now().timestamp() + int(matches[2])

            self.retries = 0

        except:
            await client.close()
            raise AuthenticationFailedException

        await client.close()
        return self.token
示例#12
0
    def auth_get_async(self, url, cache_timeout=None, model=None, **kwargs):
        response = get_cached_response(url, model=model)
        if response is not None:
            return response

        kwargs = self.prepare_request_kwargs(kwargs)
        auth = kwargs["auth"]
        if isinstance(auth, tuple):
            kwargs["auth"] = BasicAuth(login=auth[0], password=auth[1])

        with aiohttp.ClientSession() as session:
            response = yield from session.get(url, **kwargs)
            js = yield from response.json()
            response = FakeResponse(js, status_code=response.status)

        if response.status_code == codes.ok:
            cache_response(url, response, timeout=cache_timeout, model=model)
        return response
示例#13
0
 async def login(self):
     """Login to the api and store the context."""
     response = await self._request(
         method=Methods.POST,
         path="authenticate",
         data=DEFAULT_BODY,
         headers=DEFAULT_HEADER,
         auth=BasicAuth(self._username, self._password),
         timeout=30,
     )
     self._login(response)
     if response is not None:
         _LOGGER.debug("Logged in")
         if not self._serial:
             list_of_mowers = await self.get("alms")
             self._serial = list_of_mowers[0].get("alm_sn")
             _LOGGER.debug("Serial added")
         return True
     return False
示例#14
0
    async def delete(self, path, **kwargs):
        """Deletes the key at the specified path

        :param str path: The path of the key to delete
        """
        # Can only delete files in draft
        path = await self.validate_path('/' + path.identifier,
                                        version='latest',
                                        throw=True)

        resp = await self.make_request(
            'DELETE',
            self.build_url(pd_settings.EDIT_MEDIA_BASE_URL, 'file',
                           path.identifier),
            auth=BasicAuth(self.token),
            expects=(204, ),
            throws=exceptions.DeleteError,
        )
        await resp.release()
 async def create(cls,
                  epsagon_token,
                  retry_attempts=DEFAULT_RETRY_ATTEMPTS):
     """
     Creates a new EpsagonClient instance
     :param epsagon_token: used for authorization
     """
     self = cls()
     if not epsagon_token:
         raise ValueError("Epsagon token must be given")
     self.epsagon_token = epsagon_token
     retry_options = ExponentialRetry(attempts=retry_attempts,
                                      exceptions=(ClientError, ))
     self.client = RetryClient(auth=BasicAuth(login=self.epsagon_token),
                               headers={
                                   "Content-Type": "application/json",
                               },
                               retry_options=retry_options,
                               raise_for_status=True)
     return self
示例#16
0
    async def handle_request(request):
        path = request.match_info['path']
        target_url = proxy_cfg['url']
        if not target_url.endswith('/'):
            target_url += '/'
        target_url += path

        headers = {}  # use multidict?
        for header_name, header_value in request.headers.items():
            if header_name.lower() not in REMOVE_HEADERS:
                headers[header_name] = header_value
        auth = None
        if 'auth' in proxy_cfg and proxy_cfg['auth'].get('type') == 'basic':
            auth = BasicAuth(proxy_cfg['auth']['user'],
                             proxy_cfg['auth']['password'])
        # TODO set Forwarded header?

        mgr = request.app['manager']
        async with mgr.executor.http as session:
            data = await session.request(request.method,
                                         target_url,
                                         auth=auth,
                                         headers=headers,
                                         params=request.query,
                                         data=await request.content.read())

            response = web.StreamResponse(status=data.status,
                                          reason=data.reason,
                                          headers=data.headers)
            await response.prepare(request)

            while True:
                chunk = await data.content.read(4096)
                if not chunk:
                    break
                await response.write(chunk)
                #await response.drain()

        return response
示例#17
0
 def _get_auth(self):
     if self.username and self.password:
         return BasicAuth(self.username, self.password)
     return None
示例#18
0
async def test_fetch_auth_providers_require_valid_client(hass, aiohttp_client):
    """Test fetching auth providers."""
    client = await async_setup_auth(hass, aiohttp_client)
    resp = await client.get('/auth/providers',
                            auth=BasicAuth('invalid', 'bla'))
    assert resp.status == 401
示例#19
0
                    async def func() -> web.Response:
                        if request.transport:
                            peername = request.transport.get_extra_info('peername')
                            request_ip = None
                            if peername:
                                request_ip, _ = peername
                            if request.headers.get(real_ip_header) and request_ip and len(real_ip_from):
                                if any([ipaddress.ip_address(request_ip) in ipaddress.ip_network(cidr) for cidr in real_ip_from]):
                                    request_ip = request.headers.get(real_ip_header).split(',')[0].strip().split(' ')[0].strip()
                            request.request_ip = request_ip

                        request.auth = None
                        request.is_websocket = False
                        if request.headers.get('Authorization'):
                            try:
                                request.auth = BasicAuth.decode(request.headers.get('Authorization'))
                            except ValueError:
                                pass

                        if access_log:
                            timer = time.time()
                        response = None
                        try:
                            response = await handler(request)
                            response.headers[hdrs.SERVER] = server_header or ''
                        except web.HTTPException as e:
                            error_handler = context.get('_http_error_handler', {}).get(e.status, None)
                            if error_handler:
                                response = await error_handler(request)
                                response.headers[hdrs.SERVER] = server_header or ''
                            else:
                                response = e
                                response.headers[hdrs.SERVER] = server_header or ''
                                response.body = str(e).encode('utf-8')
                        except Exception as e:
                            error_handler = context.get('_http_error_handler', {}).get(500, None)
                            if not context.get('log_level') or context.get('log_level') in ['DEBUG']:
                                traceback.print_exception(e.__class__, e, e.__traceback__)
                            if error_handler:
                                response = await error_handler(request)
                                response.headers[hdrs.SERVER] = server_header or ''
                            else:
                                response = web.HTTPInternalServerError()
                                response.headers[hdrs.SERVER] = server_header or ''
                                response.body = b''
                        finally:
                            if not request.transport:
                                response = web.Response(status=499)
                                response._eof_sent = True

                            if access_log:
                                request_time = time.time() - timer
                                version_string = None
                                if isinstance(request.version, HttpVersion):
                                    version_string = 'HTTP/{}.{}'.format(request.version.major, request.version.minor)

                                if not request.is_websocket:
                                    logging.getLogger('transport.http').info('[http] [{}] {} {} "{} {}{}{}" {} {} "{}" {}'.format(
                                        response.status if response is not None else 500,
                                        request.request_ip,
                                        '"{}"'.format(request.auth.login.replace('"', '')) if request.auth and getattr(request.auth, 'login', None) else '-',
                                        request.method,
                                        request.path,
                                        '?{}'.format(request.query_string) if request.query_string else '',
                                        ' {}'.format(version_string) if version_string else '',
                                        response.content_length if response is not None and response.content_length is not None else '-',
                                        request.content_length if request.content_length is not None else '-',
                                        request.headers.get('User-Agent', '').replace('"', ''),
                                        '{0:.5f}s'.format(round(request_time, 5))
                                    ))
                                else:
                                    logging.getLogger('transport.http').info('[websocket] {} {} "CLOSE {}{}" {} "{}" {}'.format(
                                        request.request_ip,
                                        '"{}"'.format(request.auth.login.replace('"', '')) if request.auth and getattr(request.auth, 'login', None) else '-',
                                        request.path,
                                        '?{}'.format(request.query_string) if request.query_string else '',
                                        request.websocket_uuid,
                                        request.headers.get('User-Agent', '').replace('"', ''),
                                        '{0:.5f}s'.format(round(request_time, 5))
                                    ))

                            return response
示例#20
0
 async def get_access_token(self):
     r = await self.session.post(self.bliz_url + 'oauth/token',
                                 auth=BasicAuth(OAUTH_CLIENT_ID,
                                                OAUTH_CLIENT_SECRET),
                                 data={'grant_type': 'client_credentials'})
     return (await r.json())['access_token']
示例#21
0
from tests.common import ensure_auth_manager_loaded

BASE_CONFIG = [{
    'name':
    'Example',
    'type':
    'insecure_example',
    'users': [{
        'username': '******',
        'password': '******',
        'name': 'Test Name'
    }]
}]
CLIENT_ID = 'test-id'
CLIENT_SECRET = 'test-secret'
CLIENT_AUTH = BasicAuth(CLIENT_ID, CLIENT_SECRET)


async def async_setup_auth(hass,
                           aiohttp_client,
                           provider_configs=BASE_CONFIG,
                           setup_api=False):
    """Helper to setup authentication and create a HTTP client."""
    hass.auth = await auth.auth_manager_from_config(hass, provider_configs)
    ensure_auth_manager_loaded(hass.auth)
    await async_setup_component(hass, 'auth',
                                {'http': {
                                    'api_password': '******'
                                }})
    client = auth.Client('Test Client', CLIENT_ID, CLIENT_SECRET)
    hass.auth._store.clients[client.id] = client
示例#22
0
                async def func() -> web.Response:
                    request_ip = RequestHandler.get_request_ip(
                        request, context)
                    if request.headers.get('Authorization'):
                        try:
                            request._cache['auth'] = BasicAuth.decode(
                                request.headers.get('Authorization'))
                        except ValueError:
                            pass

                    if access_log:
                        timer = time.time()
                    response = web.Response(
                        status=503,  # type: ignore
                        headers={})  # type: web.Response
                    try:
                        response = await handler(request)
                        response.headers[hdrs.SERVER] = server_header or ''
                    except web.HTTPException as e:
                        error_handler = context.get('_http_error_handler',
                                                    {}).get(e.status, None)
                        if error_handler:
                            response = await error_handler(request)
                            response.headers[hdrs.SERVER] = server_header or ''
                        else:
                            response = e
                            response.headers[hdrs.SERVER] = server_header or ''
                            response.body = str(e).encode('utf-8')
                    except Exception as e:
                        error_handler = context.get('_http_error_handler',
                                                    {}).get(500, None)
                        logging.getLogger('exception').exception(
                            'Uncaught exception: {}'.format(str(e)))
                        if error_handler:
                            response = await error_handler(request)
                            response.headers[hdrs.SERVER] = server_header or ''
                        else:
                            response = web.HTTPInternalServerError(
                            )  # type: ignore
                            response.headers[hdrs.SERVER] = server_header or ''
                            response.body = b''
                    finally:
                        if not request.transport:
                            response = web.Response(
                                status=499,  # type: ignore
                                headers={})  # type: web.Response
                            response._eof_sent = True

                        if access_log:
                            request_time = time.time() - timer
                            version_string = None
                            if isinstance(request.version, HttpVersion):
                                version_string = 'HTTP/{}.{}'.format(
                                    request.version.major,
                                    request.version.minor)

                            if not request._cache.get('is_websocket'):
                                status_code = response.status if response is not None else 500
                                ignore_logging = getattr(
                                    handler, 'ignore_logging', False)
                                if ignore_logging is True:
                                    pass
                                elif isinstance(
                                        ignore_logging,
                                    (list,
                                     tuple)) and status_code in ignore_logging:
                                    pass
                                else:
                                    logging.getLogger('transport.http').info(
                                        '[{}] [{}] {} {} "{} {}{}{}" {} {} "{}" {}'
                                        .format(
                                            RequestHandler.colorize_status(
                                                'http', status_code),
                                            RequestHandler.colorize_status(
                                                status_code), request_ip,
                                            '"{}"'.format(
                                                request._cache['auth'].login.
                                                replace('"', ''))
                                            if request._cache.get('auth') and
                                            getattr(request._cache.get('auth'),
                                                    'login', None) else '-',
                                            request.method, request.path,
                                            '?{}'.format(request.query_string)
                                            if request.query_string else '',
                                            ' {}'.format(version_string)
                                            if version_string else '',
                                            response.content_length
                                            if response is not None and
                                            response.content_length is not None
                                            else '-', request.content_length
                                            if request.content_length
                                            is not None else '-',
                                            request.headers.get(
                                                'User-Agent',
                                                '').replace('"', ''),
                                            '{0:.5f}s'.format(
                                                round(request_time, 5))))
                            else:
                                logging.getLogger('transport.http').info(
                                    '[{}] {} {} "CLOSE {}{}" {} "{}" {}'.
                                    format(
                                        RequestHandler.colorize_status(
                                            'websocket', 101), request_ip,
                                        '"{}"'.format(request._cache['auth'].
                                                      login.replace('"', ''))
                                        if request._cache.get('auth')
                                        and getattr(request._cache.get('auth'),
                                                    'login', None) else '-',
                                        request.path,
                                        '?{}'.format(request.query_string)
                                        if request.query_string else '',
                                        request._cache.get(
                                            'websocket_uuid', ''),
                                        request.headers.get('User-Agent',
                                                            '').replace(
                                                                '"', ''),
                                        '{0:.5f}s'.format(
                                            round(request_time, 5))))

                        if isinstance(
                                response,
                            (web.HTTPException, web.HTTPInternalServerError)):
                            raise response

                        return response