mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 09:22:43 +03:00
061b2be9f1
`requests` does not set a default timeout, so tests can hang forever if you get something wrong. PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5457 GitOrigin-RevId: 379d97705d6b52b019213cd46f92f3892dc8a46b
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
"""
|
|
Sample auth webhook to receive a cookie and respond
|
|
"""
|
|
from http import HTTPStatus
|
|
from webserver import RequestHandler, WebServer, MkHandlers, Response
|
|
|
|
class CookieAuth(RequestHandler):
|
|
def get(self, request):
|
|
print('auth GET request')
|
|
headers = {k.lower(): v for k, v in request.headers.items()}
|
|
|
|
print(headers)
|
|
cookieHdrs = []
|
|
if 'cookie' in headers and headers['cookie']:
|
|
res = {'x-hasura-role': 'admin'}
|
|
|
|
for k, v in headers.items():
|
|
if 'response-set-cookie' in k:
|
|
hdr = ('Set-Cookie', v)
|
|
cookieHdrs.append(hdr)
|
|
|
|
print('auth response: OK')
|
|
return Response(HTTPStatus.OK, res, cookieHdrs)
|
|
print('auth response: Unauthorized')
|
|
return Response(HTTPStatus.UNAUTHORIZED)
|
|
|
|
def post(self, request):
|
|
print('auth POST request')
|
|
headers = {k.lower(): v for k, v in request.json['headers'].items()}
|
|
cookieHdrs = []
|
|
|
|
if 'cookie' in headers and headers['cookie']:
|
|
res = {'x-hasura-role': 'admin'}
|
|
|
|
for k, v in headers.items():
|
|
if 'response-set-cookie' in k:
|
|
hdr = ('Set-Cookie', v)
|
|
cookieHdrs.append(hdr)
|
|
|
|
print('auth response: OK')
|
|
return Response(HTTPStatus.OK, res, headers)
|
|
print('auth response: Unauthorized')
|
|
return Response(HTTPStatus.UNAUTHORIZED)
|
|
|
|
|
|
handlers = MkHandlers({
|
|
'/auth': CookieAuth,
|
|
})
|
|
|
|
def create_server(host='127.0.0.1', port=9876):
|
|
return WebServer((host, port), handlers)
|
|
|
|
def stop_server(server):
|
|
server.shutdown()
|
|
server.server_close()
|
|
|
|
if __name__ == '__main__':
|
|
s = create_server(host='0.0.0.0')
|
|
s.serve_forever()
|