|
| 1 | +from typing import Any, Dict, List, Optional, Union |
| 2 | + |
| 3 | +from aws_lambda_powertools.utilities.data_classes.common import DictWrapper, get_header_value |
| 4 | + |
| 5 | + |
| 6 | +def get_identity_object(identity: Optional[dict]) -> Any: |
| 7 | + """Get the identity object based on the best detected type""" |
| 8 | + # API_KEY authorization |
| 9 | + if identity is None: |
| 10 | + return None |
| 11 | + |
| 12 | + # AMAZON_COGNITO_USER_POOLS authorization |
| 13 | + if "sub" in identity: |
| 14 | + return AppSyncIdentityCognito(identity) |
| 15 | + |
| 16 | + # AWS_IAM authorization |
| 17 | + return AppSyncIdentityIAM(identity) |
| 18 | + |
| 19 | + |
| 20 | +class AppSyncIdentityIAM(DictWrapper): |
| 21 | + """AWS_IAM authorization""" |
| 22 | + |
| 23 | + @property |
| 24 | + def source_ip(self) -> List[str]: |
| 25 | + """The source IP address of the caller received by AWS AppSync. """ |
| 26 | + return self["sourceIp"] |
| 27 | + |
| 28 | + @property |
| 29 | + def username(self) -> str: |
| 30 | + """The user name of the authenticated user. IAM user principal""" |
| 31 | + return self["username"] |
| 32 | + |
| 33 | + @property |
| 34 | + def account_id(self) -> str: |
| 35 | + """The AWS account ID of the caller.""" |
| 36 | + return self["accountId"] |
| 37 | + |
| 38 | + @property |
| 39 | + def cognito_identity_pool_id(self) -> str: |
| 40 | + """The Amazon Cognito identity pool ID associated with the caller.""" |
| 41 | + return self["cognitoIdentityPoolId"] |
| 42 | + |
| 43 | + @property |
| 44 | + def cognito_identity_id(self) -> str: |
| 45 | + """The Amazon Cognito identity ID of the caller.""" |
| 46 | + return self["cognitoIdentityId"] |
| 47 | + |
| 48 | + @property |
| 49 | + def user_arn(self) -> str: |
| 50 | + """The ARN of the IAM user.""" |
| 51 | + return self["userArn"] |
| 52 | + |
| 53 | + @property |
| 54 | + def cognito_identity_auth_type(self) -> str: |
| 55 | + """Either authenticated or unauthenticated based on the identity type.""" |
| 56 | + return self["cognitoIdentityAuthType"] |
| 57 | + |
| 58 | + @property |
| 59 | + def cognito_identity_auth_provider(self) -> str: |
| 60 | + """A comma separated list of external identity provider information used in obtaining the |
| 61 | + credentials used to sign the request.""" |
| 62 | + return self["cognitoIdentityAuthProvider"] |
| 63 | + |
| 64 | + |
| 65 | +class AppSyncIdentityCognito(DictWrapper): |
| 66 | + """AMAZON_COGNITO_USER_POOLS authorization""" |
| 67 | + |
| 68 | + @property |
| 69 | + def source_ip(self) -> List[str]: |
| 70 | + """The source IP address of the caller received by AWS AppSync. """ |
| 71 | + return self["sourceIp"] |
| 72 | + |
| 73 | + @property |
| 74 | + def username(self) -> str: |
| 75 | + """The user name of the authenticated user.""" |
| 76 | + return self["username"] |
| 77 | + |
| 78 | + @property |
| 79 | + def sub(self) -> str: |
| 80 | + """The UUID of the authenticated user.""" |
| 81 | + return self["sub"] |
| 82 | + |
| 83 | + @property |
| 84 | + def claims(self) -> Dict[str, str]: |
| 85 | + """The claims that the user has.""" |
| 86 | + return self["claims"] |
| 87 | + |
| 88 | + @property |
| 89 | + def default_auth_strategy(self) -> str: |
| 90 | + """The default authorization strategy for this caller (ALLOW or DENY).""" |
| 91 | + return self["defaultAuthStrategy"] |
| 92 | + |
| 93 | + @property |
| 94 | + def groups(self) -> List[str]: |
| 95 | + """List of OIDC groups""" |
| 96 | + return self["groups"] |
| 97 | + |
| 98 | + @property |
| 99 | + def issuer(self) -> str: |
| 100 | + """The token issuer.""" |
| 101 | + return self["issuer"] |
| 102 | + |
| 103 | + |
| 104 | +class AppSyncResolverEventInfo(DictWrapper): |
| 105 | + """The info section contains information about the GraphQL request""" |
| 106 | + |
| 107 | + @property |
| 108 | + def field_name(self) -> str: |
| 109 | + """The name of the field that is currently being resolved.""" |
| 110 | + return self["fieldName"] |
| 111 | + |
| 112 | + @property |
| 113 | + def parent_type_name(self) -> str: |
| 114 | + """The name of the parent type for the field that is currently being resolved.""" |
| 115 | + return self["parentTypeName"] |
| 116 | + |
| 117 | + @property |
| 118 | + def variables(self) -> Dict[str, str]: |
| 119 | + """A map which holds all variables that are passed into the GraphQL request.""" |
| 120 | + return self.get("variables") |
| 121 | + |
| 122 | + @property |
| 123 | + def selection_set_list(self) -> List[str]: |
| 124 | + """A list representation of the fields in the GraphQL selection set. Fields that are aliased will |
| 125 | + only be referenced by the alias name, not the field name.""" |
| 126 | + return self.get("selectionSetList") |
| 127 | + |
| 128 | + @property |
| 129 | + def selection_set_graphql(self) -> Optional[str]: |
| 130 | + """A string representation of the selection set, formatted as GraphQL schema definition language (SDL). |
| 131 | + Although fragments are not be merged into the selection set, inline fragments are preserved.""" |
| 132 | + return self.get("selectionSetGraphQL") |
| 133 | + |
| 134 | + |
| 135 | +class AppSyncResolverEvent(DictWrapper): |
| 136 | + """AppSync resolver event |
| 137 | +
|
| 138 | + **NOTE:** AppSync Resolver Events can come in various shapes this data class |
| 139 | + supports both Amplify GraphQL directive @function and Direct Lambda Resolver |
| 140 | +
|
| 141 | + Documentation: |
| 142 | + ------------- |
| 143 | + - https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference.html |
| 144 | + - https://docs.amplify.aws/cli/graphql-transformer/function#structure-of-the-function-event |
| 145 | + """ |
| 146 | + |
| 147 | + def __init__(self, data: dict): |
| 148 | + super().__init__(data) |
| 149 | + |
| 150 | + info: dict = data.get("info") |
| 151 | + if not info: |
| 152 | + info = {"fieldName": self.get("fieldName"), "parentTypeName": self.get("typeName")} |
| 153 | + |
| 154 | + self._info = AppSyncResolverEventInfo(info) |
| 155 | + |
| 156 | + @property |
| 157 | + def type_name(self) -> str: |
| 158 | + """The name of the parent type for the field that is currently being resolved.""" |
| 159 | + return self.info.parent_type_name |
| 160 | + |
| 161 | + @property |
| 162 | + def field_name(self) -> str: |
| 163 | + """The name of the field that is currently being resolved.""" |
| 164 | + return self.info.field_name |
| 165 | + |
| 166 | + @property |
| 167 | + def arguments(self) -> Dict[str, any]: |
| 168 | + """A map that contains all GraphQL arguments for this field.""" |
| 169 | + return self["arguments"] |
| 170 | + |
| 171 | + @property |
| 172 | + def identity(self) -> Union[None, AppSyncIdentityIAM, AppSyncIdentityCognito]: |
| 173 | + """An object that contains information about the caller. |
| 174 | +
|
| 175 | + Depending of the type of identify found: |
| 176 | +
|
| 177 | + - API_KEY authorization - returns None |
| 178 | + - AWS_IAM authorization - returns AppSyncIdentityIAM |
| 179 | + - AMAZON_COGNITO_USER_POOLS authorization - returns AppSyncIdentityCognito |
| 180 | + """ |
| 181 | + return get_identity_object(self.get("identity")) |
| 182 | + |
| 183 | + @property |
| 184 | + def source(self) -> Dict[str, any]: |
| 185 | + """A map that contains the resolution of the parent field.""" |
| 186 | + return self.get("source") |
| 187 | + |
| 188 | + @property |
| 189 | + def request_headers(self) -> Dict[str, str]: |
| 190 | + """Request headers""" |
| 191 | + return self["request"]["headers"] |
| 192 | + |
| 193 | + @property |
| 194 | + def prev_result(self) -> Optional[Dict[str, any]]: |
| 195 | + """It represents the result of whatever previous operation was executed in a pipeline resolver.""" |
| 196 | + prev = self.get("prev") |
| 197 | + if not prev: |
| 198 | + return None |
| 199 | + return prev.get("result") |
| 200 | + |
| 201 | + @property |
| 202 | + def info(self) -> AppSyncResolverEventInfo: |
| 203 | + """The info section contains information about the GraphQL request.""" |
| 204 | + return self._info |
| 205 | + |
| 206 | + @property |
| 207 | + def stash(self) -> Optional[dict]: |
| 208 | + """The stash is a map that is made available inside each resolver and function mapping template. |
| 209 | + The same stash instance lives through a single resolver execution. This means that you can use the |
| 210 | + stash to pass arbitrary data across request and response mapping templates, and across functions in |
| 211 | + a pipeline resolver.""" |
| 212 | + return self.get("stash") |
| 213 | + |
| 214 | + def get_header_value( |
| 215 | + self, name: str, default_value: Optional[str] = None, case_sensitive: Optional[bool] = False |
| 216 | + ) -> Optional[str]: |
| 217 | + """Get header value by name |
| 218 | +
|
| 219 | + Parameters |
| 220 | + ---------- |
| 221 | + name: str |
| 222 | + Header name |
| 223 | + default_value: str, optional |
| 224 | + Default value if no value was found by name |
| 225 | + case_sensitive: bool |
| 226 | + Whether to use a case sensitive look up |
| 227 | + Returns |
| 228 | + ------- |
| 229 | + str, optional |
| 230 | + Header value |
| 231 | + """ |
| 232 | + return get_header_value(self.request_headers, name, default_value, case_sensitive) |
0 commit comments