2019-03-04 10:46:53 +03:00
|
|
|
"""
|
|
|
|
Sample auth webhook to receive a cookie and respond
|
|
|
|
"""
|
|
|
|
from http import HTTPStatus
|
2022-10-21 20:32:58 +03:00
|
|
|
|
|
|
|
from context import ThreadedHTTPServer
|
2022-09-28 12:19:47 +03:00
|
|
|
from webserver import MkHandlers, RequestHandler, Response
|
2019-03-04 10:46:53 +03:00
|
|
|
|
|
|
|
class CookieAuth(RequestHandler):
|
|
|
|
def get(self, request):
|
2022-08-15 15:38:59 +03:00
|
|
|
print('auth GET request')
|
2019-03-04 10:46:53 +03:00
|
|
|
headers = {k.lower(): v for k, v in request.headers.items()}
|
2021-11-09 15:00:21 +03:00
|
|
|
|
2019-03-04 10:46:53 +03:00
|
|
|
print(headers)
|
2021-11-09 15:00:21 +03:00
|
|
|
cookieHdrs = []
|
2019-03-04 10:46:53 +03:00
|
|
|
if 'cookie' in headers and headers['cookie']:
|
|
|
|
res = {'x-hasura-role': 'admin'}
|
2022-09-28 12:19:47 +03:00
|
|
|
|
2021-11-09 15:00:21 +03:00
|
|
|
for k, v in headers.items():
|
|
|
|
if 'response-set-cookie' in k:
|
|
|
|
hdr = ('Set-Cookie', v)
|
|
|
|
cookieHdrs.append(hdr)
|
|
|
|
|
2022-08-15 15:38:59 +03:00
|
|
|
print('auth response: OK')
|
2021-11-09 15:00:21 +03:00
|
|
|
return Response(HTTPStatus.OK, res, cookieHdrs)
|
2022-08-15 15:38:59 +03:00
|
|
|
print('auth response: Unauthorized')
|
2019-03-04 10:46:53 +03:00
|
|
|
return Response(HTTPStatus.UNAUTHORIZED)
|
|
|
|
|
|
|
|
def post(self, request):
|
2022-08-15 15:38:59 +03:00
|
|
|
print('auth POST request')
|
2019-03-04 10:46:53 +03:00
|
|
|
headers = {k.lower(): v for k, v in request.json['headers'].items()}
|
2021-11-09 15:00:21 +03:00
|
|
|
cookieHdrs = []
|
2022-09-28 12:19:47 +03:00
|
|
|
|
2019-03-04 10:46:53 +03:00
|
|
|
if 'cookie' in headers and headers['cookie']:
|
|
|
|
res = {'x-hasura-role': 'admin'}
|
2021-11-09 15:00:21 +03:00
|
|
|
|
|
|
|
for k, v in headers.items():
|
|
|
|
if 'response-set-cookie' in k:
|
|
|
|
hdr = ('Set-Cookie', v)
|
|
|
|
cookieHdrs.append(hdr)
|
|
|
|
|
2022-08-15 15:38:59 +03:00
|
|
|
print('auth response: OK')
|
2021-11-09 15:00:21 +03:00
|
|
|
return Response(HTTPStatus.OK, res, headers)
|
2022-08-15 15:38:59 +03:00
|
|
|
print('auth response: Unauthorized')
|
2019-03-04 10:46:53 +03:00
|
|
|
return Response(HTTPStatus.UNAUTHORIZED)
|
|
|
|
|
|
|
|
|
|
|
|
handlers = MkHandlers({
|
|
|
|
'/auth': CookieAuth,
|
|
|
|
})
|
|
|
|
|
2022-10-21 20:32:58 +03:00
|
|
|
def create_server(server_address):
|
|
|
|
return ThreadedHTTPServer(server_address, handlers)
|
2019-03-04 10:46:53 +03:00
|
|
|
|
|
|
|
def stop_server(server):
|
|
|
|
server.shutdown()
|
|
|
|
server.server_close()
|