import requests import os import boto3 from urllib.parse import urlparse, parse_qs import base64 import requests from aws_requests_auth.aws_auth import AWSRequestsAuth import json # Config for calling AssumeRoleWithSAML idp_arn = "arn:aws:iam::0123456789:saml-provider/MyIdentityProvider" api_gw_role_arn = 'arn:aws:iam:: 0123456789:role/APIGWAccessRole' studio_api_url = "abcdef.execute-api.us-east-1.amazonaws.com" studio_api_gw_path = "https://" + studio_api_url + "/Prod " # Every customer will need to get SAML Response from the POST call def get_saml_response(event): saml_response_uri = base64.b64decode(event['body']).decode('ascii') request_body = parse_qs(saml_response_uri) print(f"b64 saml response: {request_body['SAMLResponse'][0]}") return request_body['SAMLResponse'][0] def lambda_handler(event, context): sts = boto3.client('sts') # get temporary credentials response = sts.assume_role_with_saml( RoleArn=api_gw_role_arn, PrincipalArn=durga_idp_arn, SAMLAssertion=get_saml_response(event) ) auth = AWSRequestsAuth(aws_access_key=response['Credentials']['AccessKeyId'], aws_secret_access_key=response['Credentials']['SecretAccessKey'], aws_host=studio_api_url, aws_region='us-west-2', aws_service='execute-api', aws_token=response['Credentials']['SessionToken']) presigned_response = requests.post( studio_api_gw_path, data=saml_response_data, auth=auth) return presigned_response