forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_webhook_server.py
57 lines (44 loc) · 1.68 KB
/
auth_webhook_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Sample auth webhook to receive a cookie and respond
"""
from http import HTTPStatus
from context import ThreadedHTTPServer
from webserver import MkHandlers, RequestHandler, 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(server_address):
return ThreadedHTTPServer(server_address, handlers)
def stop_server(server):
server.shutdown()
server.server_close()