Skip to content

Commit 8beeb49

Browse files
author
Marco Paolini
committed
Periodically check auth status
Closes #4
1 parent 934de40 commit 8beeb49

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

pushpull/config.py

+4
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ def get_cors_allow_credentials():
5353

5454
def get_ws_autoping_timeout():
5555
return 15
56+
57+
58+
def get_periodic_auth_check_timeout():
59+
return 150

pushpull/websocket/gateway.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
async def websocket_rabbitmq_gateway(request):
2020
authorization = decode_auth_querystring_param(request.GET)
2121
try:
22-
# TODO: reuse amqp channel: here we open one, we close it and then we reopen another one with the
23-
# Exchanger a few lines down
22+
# TODO: reuse amqp channel and maybe share it with the Exchanger
2423
user_info = await auth.get_user_info(authorization)
2524
except auth.NotAuthorized as exc:
2625
raise aiohttp.web_exceptions.HTTPUnauthorized() from exc
@@ -29,8 +28,7 @@ async def websocket_rabbitmq_gateway(request):
2928
except auth.AuthTimeout as exc:
3029
logger.warning('auth backend timeout')
3130
raise aiohttp.web_exceptions.HTTPInternalServerError(text='auth backend timeout') from exc
32-
else:
33-
name = user_info.id
31+
name = user_info.id
3432
ws = aiohttp.web.WebSocketResponse()
3533
client_id = request.GET.get('client-id')
3634
if not client_id:
@@ -45,7 +43,8 @@ async def websocket_rabbitmq_gateway(request):
4543
asyncio.ensure_future(coro) for coro in [
4644
send_from_amqp_to_websocket(amqp_receiver, ws),
4745
send_from_websocket_to_amqp(ws, amqp_sender),
48-
send_ping_to_websocket(ws, config.get_ws_autoping_timeout())
46+
send_ping_to_websocket(ws, config.get_ws_autoping_timeout()),
47+
check_auth_periodic(authorization, delay=config.get_periodic_auth_check_timeout())
4948
]
5049
]
5150
try:
@@ -61,9 +60,9 @@ async def websocket_rabbitmq_gateway(request):
6160
except Exception as exc:
6261
logger.exception('client id %s coroutine %r done due to exception: %r', client_id, coro, exc)
6362
else:
64-
logger.info('client id %s coroutine %r done, result: %r', client_id, coro, result)
63+
logger.debug('client id %s coroutine %r done, result: %r', client_id, coro, result)
6564
for coro in pending:
66-
logger.info('client id %s cancelling pending coroutine %r', client_id, coro)
65+
logger.debug('client id %s cancelling pending coroutine %r', client_id, coro)
6766
coro.cancel()
6867
await asyncio.sleep(0)
6968
except Exception:
@@ -100,6 +99,18 @@ async def send_ping_to_websocket(ws, timeout):
10099
await asyncio.sleep(timeout)
101100

102101

102+
async def check_auth_periodic(authorization, delay):
103+
while True:
104+
await asyncio.sleep(delay)
105+
logger.debug('periodic auth check start for %s', authorization)
106+
try:
107+
# TODO: reuse amqp channel and maybe share it with the Exchanger
108+
await auth.get_user_info(authorization)
109+
except auth.AuthTimeout as exc:
110+
logger.warning('periodic auth check timeout for %s', authorization)
111+
logger.debug('periodic auth check OK for %s', authorization)
112+
113+
103114
async def echo_websocket(ws):
104115
"""For testing only """
105116
async for msg in ws:

0 commit comments

Comments
 (0)