2019-03-04 10:46:53 +03:00
|
|
|
"""
|
|
|
|
Sample auth webhook to receive a cookie and respond
|
|
|
|
"""
|
|
|
|
from http import HTTPStatus
|
2022-09-28 12:19:47 +03:00
|
|
|
import http.server
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
|
|
|
|
def create_server(host='127.0.0.1', port=9876):
|
2022-09-28 12:19:47 +03:00
|
|
|
return http.server.HTTPServer((host, port), handlers)
|
2019-03-04 10:46:53 +03:00
|
|
|
|
|
|
|
def stop_server(server):
|
|
|
|
server.shutdown()
|
|
|
|
server.server_close()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
s = create_server(host='0.0.0.0')
|
|
|
|
s.serve_forever()
|