Source code for runway.cfngin.hooks.staticsite.auth_at_edge.callback_url_retriever
"""Callback URL Retriever.Dependency pre-hook responsible for ensuring correctcallback urls are retrieved or a temporary one is used in it's place."""from__future__importannotationsimportloggingfromtypingimportTYPE_CHECKING,Anyfrom...baseimportHookArgsBaseModelifTYPE_CHECKING:from.....contextimportCfnginContextLOGGER=logging.getLogger(__name__)
[docs]classHookArgs(HookArgsBaseModel):"""Hook arguments."""stack_name:str"""The name of the stack to check against."""user_pool_arn:str|None=None"""The ARN of the User Pool to check for a client."""
[docs]defget(context:CfnginContext,*_args:Any,**kwargs:Any)->dict[str,Any]:"""Retrieve the callback URLs for User Pool Client Creation. When the User Pool is created a Callback URL is required. During a post hook entitled ``client_updater`` these Callback URLs are updated to that of the Distribution. Before then we need to ensure that if a Client already exists that the URLs for that client are used to prevent any interruption of service during deploy. Arguments parsed by :class:`~runway.cfngin.hooks.staticsite.auth_at_edge.callback_url_retriever.HookArgs`. Args: context: The context instance. **kwargs: Arbitrary keyword arguments. """args=HookArgs.model_validate(kwargs)session=context.get_session()cloudformation_client=session.client("cloudformation")cognito_client=session.client("cognito-idp")context_dict={"callback_urls":["https://example.org"]}try:# Return the current stack if one existsstack_desc=cloudformation_client.describe_stacks(StackName=args.stack_name)# Get the client_id from the outputsoutputs=stack_desc["Stacks"][0].get("Outputs",[])ifargs.user_pool_arn:user_pool_id=args.user_pool_arn.split("/")[-1:][0]else:user_pool_id=next(o["OutputValue"]foroinoutputsif("OutputKey"inoand"OutputValue"ino)ando["OutputKey"]=="AuthAtEdgeUserPoolId")client_id=next(o["OutputValue"]foroinoutputsif("OutputKey"inoand"OutputValue"ino)ando["OutputKey"]=="AuthAtEdgeClient")# Poll the user pool client informationresp=cognito_client.describe_user_pool_client(UserPoolId=user_pool_id,ClientId=client_id)# Retrieve the callbackscallbacks=resp["UserPoolClient"].get("CallbackURLs")ifcallbacks:context_dict["callback_urls"]=callbacksreturncontext_dictexceptException:# noqa: BLE001returncontext_dict