From 66cac2e20df6f3edab37e663646f282d47e3968c Mon Sep 17 00:00:00 2001 From: rkhudov Date: Mon, 14 Feb 2022 02:26:43 +0100 Subject: [PATCH 1/2] add authorizer --- .project-version | 2 +- serverless.yml | 5 +++++ src/functions/auth.py | 37 +++++++++++++++++++++++++++++++++++++ src/utils/__init__.py | 0 src/utils/auth.py | 3 +++ 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/functions/auth.py create mode 100644 src/utils/__init__.py create mode 100644 src/utils/auth.py diff --git a/.project-version b/.project-version index 8acdd82..4e379d2 100644 --- a/.project-version +++ b/.project-version @@ -1 +1 @@ -0.0.1 +0.0.2 diff --git a/serverless.yml b/serverless.yml index 1203bd2..e7fb20c 100644 --- a/serverless.yml +++ b/serverless.yml @@ -11,6 +11,9 @@ provider: runtime: python3.8 functions: + auth: + handler: src/functions/auth.handler + get: handler: src/functions/hello_world.handler events: @@ -18,6 +21,8 @@ functions: path: hello-world method: get cors: true + authorizer: + name: auth post: handler: src/functions/add.handler diff --git a/src/functions/auth.py b/src/functions/auth.py new file mode 100644 index 0000000..d38c7b7 --- /dev/null +++ b/src/functions/auth.py @@ -0,0 +1,37 @@ +""" +Provide handler for the auth method. +""" +from typing import Any, Dict + +from aws_lambda_powertools.utilities.typing import LambdaContext + + +def handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any]: + """ + Lambda function handler. + + Arguments: + event (dict): the event with data to process. + context (LambdaContext): the information about the invocation, function, and runtime environment. + + Returns: + The response as dict. + """ + # auth_token = ... + resource = event.get('methodArn') + principal_id = None + effect = 'Allow' + response = { + 'principalId': principal_id, + 'policyDocument': { + 'Version': '2022-02-14', + 'Statement': [ + { + 'Action': 'execute-api:Invoke', + 'Effect': effect, + 'Resource': resource, + }, + ], + }, + } + return response diff --git a/src/utils/__init__.py b/src/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/auth.py b/src/utils/auth.py new file mode 100644 index 0000000..363eb22 --- /dev/null +++ b/src/utils/auth.py @@ -0,0 +1,3 @@ +""" +Provide utils for authentication. +""" From 7720efe494b717b4a132b7c5d0341618966599a5 Mon Sep 17 00:00:00 2001 From: rkhudov Date: Mon, 14 Feb 2022 02:28:41 +0100 Subject: [PATCH 2/2] add type of auth func --- serverless.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/serverless.yml b/serverless.yml index e7fb20c..4b2162c 100644 --- a/serverless.yml +++ b/serverless.yml @@ -23,6 +23,7 @@ functions: cors: true authorizer: name: auth + type: request post: handler: src/functions/add.handler