|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +from auth_token import Token |
| 5 | +from constant import DEFAULT_NETWORK_ID |
| 6 | + |
| 7 | +def request_handler(event, context): |
| 8 | + print(event) |
| 9 | + if 'path' not in event: |
| 10 | + return get_response("400", {"status": "failed", "error": "Bad Request"}) |
| 11 | + try: |
| 12 | + path = event['path'].lower() |
| 13 | + data = None |
| 14 | + if "/register" == path: |
| 15 | + payload = event['body'] |
| 16 | + payload_dict = payload_check(payload=payload, path=path) |
| 17 | + net_id = DEFAULT_NETWORK_ID |
| 18 | + token_instance = Token(net_id) |
| 19 | + data = token_instance.process_token(daemon_id=payload_dict['daemonId']) |
| 20 | + if data is None: |
| 21 | + response = get_response("400", {"status": "failed", "error": "Bad Request"}) |
| 22 | + else: |
| 23 | + if data.get('error', '') == '': |
| 24 | + print(data) |
| 25 | + response = get_response("200", {"status": "success", "data": data}) |
| 26 | + else: |
| 27 | + error = data['error'] |
| 28 | + data.pop('error') |
| 29 | + response = get_response("200", {"status": "failed", "data": data, "error": error}) |
| 30 | + elif "/event" == path: |
| 31 | + try: |
| 32 | + payload_dict = event['headers'] |
| 33 | + print("Processing [" + str(path) + "] with body [" + str(payload_dict) + "]") |
| 34 | + net_id = 42 |
| 35 | + token_instance = Token(net_id) |
| 36 | + data = token_instance.validate_token(daemon_id=payload_dict['x-daemonid'], token=payload_dict['x-token']) |
| 37 | + response = get_lambda_authorizer_response_format(event=event, allow=data['validated']) |
| 38 | + except Exception as e: |
| 39 | + print(repr(e)) |
| 40 | + response = get_lambda_authorizer_response_format(event=event, allow=False) |
| 41 | + |
| 42 | + except Exception as e: |
| 43 | + response = get_response(500, {"status": "failed", |
| 44 | + "error": repr(e)}) |
| 45 | + |
| 46 | + return response |
| 47 | + |
| 48 | + |
| 49 | +def payload_check(payload, path): |
| 50 | + payload_dict = None |
| 51 | + if payload is not None and len(payload) > 0: |
| 52 | + payload_dict = json.loads(payload) |
| 53 | + print("Processing [" + str(path) + "] with body [" + str(payload) + "]") |
| 54 | + return payload_dict |
| 55 | + |
| 56 | + |
| 57 | +# Generate response JSON that API gateway expects from the lambda function |
| 58 | +def get_response(status_code, message): |
| 59 | + return { |
| 60 | + 'statusCode': status_code, |
| 61 | + 'body': json.dumps(message), |
| 62 | + 'headers': { |
| 63 | + 'Content-Type': 'application/json', |
| 64 | + "X-Requested-With": '*', |
| 65 | + "Access-Control-Allow-Headers": 'Access-Control-Allow-Origin, Content-Type,X-Amz-Date,Authorization,X-Api-Key,x-requested-with', |
| 66 | + "Access-Control-Allow-Origin": '*', |
| 67 | + "Access-Control-Allow-Methods": 'GET,OPTIONS,POST' |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | +def get_lambda_authorizer_response_format(event, allow): |
| 73 | + print(allow) |
| 74 | + response = { |
| 75 | + "principalId": os.environ['principalId'], |
| 76 | + "policyDocument": { |
| 77 | + "Version": '2012-10-17', |
| 78 | + "Statement": [ |
| 79 | + { |
| 80 | + "Action": 'execute-api:Invoke', |
| 81 | + "Resource": event['methodArn'] |
| 82 | + } |
| 83 | + ] |
| 84 | + } |
| 85 | + } |
| 86 | + if allow: |
| 87 | + response["policyDocument"]["Statement"][0]["Effect"] = 'Allow' |
| 88 | + else: |
| 89 | + response["policyDocument"]["Statement"][0]["Effect"] = 'Deny' |
| 90 | + |
| 91 | + print(response) |
| 92 | + return response |
0 commit comments