forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapigw_auth_v2.py
30 lines (22 loc) · 910 Bytes
/
apigw_auth_v2.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
from secrets import compare_digest
from aws_lambda_powertools.utilities.data_classes import event_source
from aws_lambda_powertools.utilities.data_classes.api_gateway_authorizer_event import (
APIGatewayAuthorizerEventV2,
APIGatewayAuthorizerResponseV2,
)
def get_user_by_token(token):
if compare_digest(token, "value"):
return {"name": "Foo"}
return None
@event_source(data_class=APIGatewayAuthorizerEventV2)
def lambda_handler(event: APIGatewayAuthorizerEventV2, context):
user = get_user_by_token(event.headers.get("Authorization"))
if user is None:
# No user was found, so we return not authorized
return APIGatewayAuthorizerResponseV2(authorize=False).asdict()
# Found the user and setting the details in the context
response = APIGatewayAuthorizerResponseV2(
authorize=True,
context=user,
)
return response.asdict()