Small changes in webhook handler.

This commit is contained in:
Alex Root Junior 2017-11-27 17:29:20 +02:00
parent 68e016ecab
commit ff2b7f9682

View file

@ -109,11 +109,7 @@ class WebhookRequestHandler(web.View):
:return: :class:`aiohttp.web.Response`
"""
if self.request.app.get('_check_ip', False):
ip_address, accept = self.check_ip()
if not accept:
raise web.HTTPUnauthorized()
context.set_value('TELEGRAM_IP', ip_address)
self.validate_ip()
context.update_state({'CALLER': WEBHOOK,
WEBHOOK_CONNECTION: True,
@ -129,6 +125,14 @@ class WebhookRequestHandler(web.View):
return response.get_web_response()
return web.Response(text='ok')
async def get(self):
self.validate_ip()
return web.Response(text='')
async def head(self):
self.validate_ip()
return web.Response(text='')
async def process_update(self, update):
"""
Need respond in less than 60 seconds in to webhook.
@ -220,6 +224,16 @@ class WebhookRequestHandler(web.View):
# Not allowed and can't get client IP
return None, False
def validate_ip(self):
"""
Check ip if that is needed. Raise web.HTTPUnauthorized for not allowed hosts.
"""
if self.request.app.get('_check_ip', False):
ip_address, accept = self.check_ip()
if not accept:
raise web.HTTPUnauthorized()
context.set_value('TELEGRAM_IP', ip_address)
def configure_app(dispatcher, app: web.Application, path=DEFAULT_WEB_PATH):
"""