|
| 1 | +from typing import Any, Dict, List, Optional |
| 2 | + |
| 3 | +from aws_lambda_powertools.utilities.data_classes.common import DictWrapper |
| 4 | + |
| 5 | + |
| 6 | +class AppSyncAuthorizerEventRequestContext(DictWrapper): |
| 7 | + """Request context""" |
| 8 | + |
| 9 | + @property |
| 10 | + def api_id(self) -> str: |
| 11 | + """AppSync API ID""" |
| 12 | + return self["requestContext"]["apiId"] |
| 13 | + |
| 14 | + @property |
| 15 | + def account_id(self) -> str: |
| 16 | + """AWS Account ID""" |
| 17 | + return self["requestContext"]["accountId"] |
| 18 | + |
| 19 | + @property |
| 20 | + def request_id(self) -> str: |
| 21 | + """Requestt ID""" |
| 22 | + return self["requestContext"]["requestId"] |
| 23 | + |
| 24 | + @property |
| 25 | + def query_string(self) -> str: |
| 26 | + """GraphQL query string""" |
| 27 | + return self["requestContext"]["queryString"] |
| 28 | + |
| 29 | + @property |
| 30 | + def operation_name(self) -> Optional[str]: |
| 31 | + """GraphQL operation name, optional""" |
| 32 | + return self["requestContext"].get("operationName") |
| 33 | + |
| 34 | + @property |
| 35 | + def variables(self) -> Dict: |
| 36 | + """GraphQL variables""" |
| 37 | + return self["requestContext"]["variables"] |
| 38 | + |
| 39 | + |
| 40 | +class AppSyncAuthorizerEvent(DictWrapper): |
| 41 | + """AppSync lambda authorizer event |
| 42 | +
|
| 43 | + Documentation: |
| 44 | + ------------- |
| 45 | + - https://aws.amazon.com/blogs/mobile/appsync-lambda-auth/ |
| 46 | + - https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html#aws-lambda-authorization |
| 47 | + - https://docs.amplify.aws/lib/graphqlapi/authz/q/platform/js#aws-lambda |
| 48 | + """ |
| 49 | + |
| 50 | + @property |
| 51 | + def authorization_token(self) -> str: |
| 52 | + """Authorization token""" |
| 53 | + return self["authorizationToken"] |
| 54 | + |
| 55 | + @property |
| 56 | + def request_context(self) -> AppSyncAuthorizerEventRequestContext: |
| 57 | + """Request context""" |
| 58 | + return AppSyncAuthorizerEventRequestContext(self._data) |
| 59 | + |
| 60 | + |
| 61 | +class AppSyncAuthorizerResponse: |
| 62 | + """AppSync Lambda authorizer response helper |
| 63 | +
|
| 64 | + Parameters |
| 65 | + ---------- |
| 66 | + authorize: bool |
| 67 | + authorize is a boolean value indicating if the value in authorizationToken |
| 68 | + is authorized to make calls to the GraphQL API. If this value is |
| 69 | + true, execution of the GraphQL API continues. If this value is false, |
| 70 | + an UnauthorizedException is raised |
| 71 | + max_age: Optional[int] |
| 72 | + Set the ttlOverride. The number of seconds that the response should be |
| 73 | + cached for. If no value is returned, the value from the API (if configured) |
| 74 | + or the default of 300 seconds (five minutes) is used. If this is 0, the response |
| 75 | + is not cached. |
| 76 | + resolver_context: Optional[Dict[str, Any]] |
| 77 | + A JSON object visible as `$ctx.identity.resolverContext` in resolver templates |
| 78 | +
|
| 79 | + The resolverContext object only supports key-value pairs. Nested keys are not supported. |
| 80 | +
|
| 81 | + Warning: The total size of this JSON object must not exceed 5MB. |
| 82 | + deny_fields: Optional[List[str]] |
| 83 | + A list of fields that will be set to `null` regardless of the resolver's return. |
| 84 | +
|
| 85 | + A field is either `TypeName.FieldName`, or an ARN such as |
| 86 | + `arn:aws:appsync:us-east-1:111122223333:apis/GraphQLApiId/types/TypeName/fields/FieldName` |
| 87 | +
|
| 88 | + Use the full ARN for correctness when sharing a Lambda function authorizer between APIs. |
| 89 | + """ |
| 90 | + |
| 91 | + def __init__( |
| 92 | + self, |
| 93 | + authorize: bool = False, |
| 94 | + max_age: Optional[int] = None, |
| 95 | + resolver_context: Optional[Dict[str, Any]] = None, |
| 96 | + deny_fields: Optional[List[str]] = None, |
| 97 | + ): |
| 98 | + self.authorize = authorize |
| 99 | + self.max_age = max_age |
| 100 | + self.deny_fields = deny_fields |
| 101 | + self.resolver_context = resolver_context |
| 102 | + |
| 103 | + def asdict(self) -> dict: |
| 104 | + """Return the response as a dict""" |
| 105 | + response: Dict = {"isAuthorized": self.authorize} |
| 106 | + |
| 107 | + if self.max_age is not None: |
| 108 | + response["ttlOverride"] = self.max_age |
| 109 | + |
| 110 | + if self.deny_fields: |
| 111 | + response["deniedFields"] = self.deny_fields |
| 112 | + |
| 113 | + if self.resolver_context: |
| 114 | + response["resolverContext"] = self.resolver_context |
| 115 | + |
| 116 | + return response |
0 commit comments