-
Notifications
You must be signed in to change notification settings - Fork 432
feat(data-classes): AppSync Lambda authorizer event #610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
heitorlessa
merged 14 commits into
aws-powertools:develop
from
gyft:feat-appsync-authorizer
Aug 16, 2021
Merged
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a11eaaa
feat(event-sources): AppSync lambda authorizer event
michaelbrewer 5152ea9
feat(event-sources): AppSync lambda authorize response helpers
michaelbrewer 23e52bf
feat(event-sources): rename to to_dict
michaelbrewer 4900729
refactor: opted for optionals instead
michaelbrewer a6d6125
refactor: based on code review
michaelbrewer 2e9d1ec
refactor: based on code review
michaelbrewer 12a50f4
refactor: based on code review
michaelbrewer b8fe2df
refactor: clean up docs
michaelbrewer b698dd5
chore: add link for amplify docs
michaelbrewer 599ffa6
chore: update docs
michaelbrewer 1e59652
Merge branch 'develop' into feat-appsync-authorizer
michaelbrewer 7a225f8
docs: add sample usage for lamda authorizer
michaelbrewer 521d482
chore: bump ci
michaelbrewer 18c09aa
chore: bump ci
michaelbrewer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
aws_lambda_powertools/utilities/data_classes/appsync_authorizer_event.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
from typing import Any, Dict, List, Optional | ||
|
||
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper | ||
|
||
|
||
class AppSyncAuthorizerEventRequestContext(DictWrapper): | ||
"""Request context""" | ||
|
||
@property | ||
def api_id(self) -> str: | ||
"""AppSync api id""" | ||
return self["requestContext"]["apiId"] | ||
|
||
@property | ||
def account_id(self) -> str: | ||
"""AWS Account ID""" | ||
return self["requestContext"]["accountId"] | ||
|
||
@property | ||
def request_id(self) -> str: | ||
"""Requestt ID""" | ||
return self["requestContext"]["requestId"] | ||
|
||
@property | ||
def query_string(self) -> str: | ||
"""Graphql query string""" | ||
michaelbrewer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self["requestContext"]["queryString"] | ||
|
||
@property | ||
def operation_name(self) -> Optional[str]: | ||
"""Graphql operation name, optional""" | ||
michaelbrewer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self["requestContext"].get("operationName") | ||
|
||
@property | ||
def variables(self) -> Dict: | ||
"""Graphql variables""" | ||
michaelbrewer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self["requestContext"]["variables"] | ||
|
||
|
||
class AppSyncAuthorizerEvent(DictWrapper): | ||
"""AppSync lambda authorizer event | ||
|
||
Documentation: | ||
------------- | ||
- https://aws.amazon.com/blogs/mobile/appsync-lambda-auth/ | ||
- https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html#aws-lambda-authorization | ||
""" | ||
|
||
@property | ||
def authorization_token(self) -> str: | ||
"""Authorization token""" | ||
return self["authorizationToken"] | ||
|
||
@property | ||
def request_context(self) -> AppSyncAuthorizerEventRequestContext: | ||
"""Request context""" | ||
return AppSyncAuthorizerEventRequestContext(self._data) | ||
|
||
|
||
class AppSyncAuthorizerResponse: | ||
"""AppSync Lambda authorizer response helper | ||
|
||
Parameters | ||
---------- | ||
authorize: bool | ||
authorize is a boolean value indicating if the value in authorizationToken | ||
is authorized to make calls to the GraphQL API. If this value is | ||
true, execution of the GraphQL API continues. If this value is false, | ||
an UnauthorizedException is raised | ||
ttl: Optional[int] | ||
michaelbrewer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Set the ttlOverride. The number of seconds that the response should be | ||
cached for. If no value is returned, the value from the API (if configured) | ||
or the default of 300 seconds (five minutes) is used. If this is 0, the response | ||
is not cached. | ||
resolver_context: Optional[Dict[str, Any]] | ||
A JSON object visible as `$ctx.identity.resolverContext` in resolver templates | ||
Warning: The total size of this JSON object must not exceed 5MB. | ||
denied_fields: Optional[List[str]] | ||
michaelbrewer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
A list of which are forcibly changed to null, even if a value was returned from a resolver. | ||
Each item is either a fully qualified field ARN in the form of | ||
`arn:aws:appsync:us-east-1:111122223333:apis/GraphQLApiId/types/TypeName/fields/FieldName` | ||
or a short form of TypeName.FieldName. The full ARN form should be used when two APIs | ||
share a lambda function authorizer and there might be ambiguity between common types | ||
and fields between the two APIs. | ||
michaelbrewer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
def __init__( | ||
self, | ||
authorize: bool = False, | ||
ttl: Optional[int] = None, | ||
michaelbrewer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resolver_context: Optional[Dict[str, Any]] = None, | ||
denied_fields: Optional[List[str]] = None, | ||
): | ||
self._data: Dict = {"isAuthorized": authorize} | ||
|
||
if ttl is not None: | ||
self._data["ttlOverride"] = ttl | ||
michaelbrewer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if denied_fields: | ||
self._data["deniedFields"] = denied_fields | ||
michaelbrewer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if resolver_context: | ||
self._data["resolverContext"] = resolver_context | ||
michaelbrewer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def to_dict(self) -> dict: | ||
"""Return the response as a dict""" | ||
return self._data | ||
michaelbrewer marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"authorizationToken": "BE9DC5E3-D410-4733-AF76-70178092E681", | ||
"requestContext": { | ||
"apiId": "giy7kumfmvcqvbedntjwjvagii", | ||
"accountId": "254688921111", | ||
"requestId": "b80ed838-14c6-4500-b4c3-b694c7bef086", | ||
"queryString": "mutation MyNewTask($desc: String!) {\n createTask(description: $desc, owner: \"ccc\", taskStatus: \"cc\", title: \"ccc\") {\n id\n }\n}\n", | ||
"operationName": "MyNewTask", | ||
"variables": { | ||
"desc": "Foo" | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"isAuthorized": true, | ||
"resolverContext": { | ||
"name": "Foo Man", | ||
"balance": 100 | ||
}, | ||
"deniedFields": ["Mutation.createEvent"], | ||
"ttlOverride": 15 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.