-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add back custom auth #322
Merged
Merged
Add back custom auth #322
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6a9e458
Add back custom auth
jamesls a820d1f
Add auth objects for configuring authorizers
jamesls d51825e
Add pending deprecation warning
jamesls eb80746
Update README with new interface
jamesls ac9ee17
Add authorizers as top level import
jamesls 8039255
Incorporate review feedback
jamesls aae3fb2
Add upgrading notes
jamesls 79dd752
Add missing typedef
jamesls 1b223a7
Ignore not implementederror in coverage
jamesls 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 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import json | ||
import traceback | ||
import decimal | ||
import warnings | ||
from collections import Mapping | ||
|
||
# Implementation note: This file is intended to be a standalone file | ||
|
@@ -100,6 +101,59 @@ def __repr__(self): | |
return 'CaseInsensitiveMapping(%s)' % repr(self._dict) | ||
|
||
|
||
class Authorizer(object): | ||
name = '' | ||
|
||
def to_swagger(self): | ||
raise NotImplementedError("to_swagger") | ||
|
||
|
||
class CognitoUserPoolAuthorizer(object): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing you meant to inherit from |
||
|
||
_AUTH_TYPE = 'cognito_user_pools' | ||
|
||
def __init__(self, name, header, provider_arns): | ||
self.name = name | ||
self._header = header | ||
self._provider_arns = provider_arns | ||
|
||
def to_swagger(self): | ||
return { | ||
'in': 'header', | ||
'type': 'apiKey', | ||
'name': self._header, | ||
'x-amazon-apigateway-authtype': self._AUTH_TYPE, | ||
'x-amazon-apigateway-authorizer': { | ||
'type': self._AUTH_TYPE, | ||
'providerARNs': self._provider_arns, | ||
} | ||
} | ||
|
||
|
||
class CustomAuthorizer(object): | ||
|
||
_AUTH_TYPE = 'custom' | ||
|
||
def __init__(self, name, header, authorizer_uri, ttl_seconds=300): | ||
self.name = name | ||
self._header = header | ||
self._authorizer_uri = authorizer_uri | ||
self._ttl_seconds = ttl_seconds | ||
|
||
def to_swagger(self): | ||
return { | ||
'in': 'header', | ||
'type': 'apiKey', | ||
'name': self._header, | ||
'x-amazon-apigateway-authtype': self._AUTH_TYPE, | ||
'x-amazon-apigateway-authorizer': { | ||
'type': 'token', | ||
'authorizerUri': self._authorizer_uri, | ||
'authorizerResultTtlInSeconds': self._ttl_seconds, | ||
} | ||
} | ||
|
||
|
||
class CORSConfig(object): | ||
"""A cors configuration to attach to a route.""" | ||
|
||
|
@@ -204,7 +258,7 @@ class RouteEntry(object): | |
def __init__(self, view_function, view_name, path, methods, | ||
authorizer_name=None, | ||
api_key_required=None, content_types=None, | ||
cors=False): | ||
cors=False, authorizer=None): | ||
self.view_function = view_function | ||
self.view_name = view_name | ||
self.uri_pattern = path | ||
|
@@ -225,6 +279,7 @@ def __init__(self, view_function, view_name, path, methods, | |
elif cors is False: | ||
cors = None | ||
self.cors = cors | ||
self.authorizer = authorizer | ||
|
||
def _parse_view_args(self): | ||
if '{' not in self.uri_pattern: | ||
|
@@ -284,12 +339,14 @@ def authorizers(self): | |
return self._authorizers.copy() | ||
|
||
def define_authorizer(self, name, header, auth_type, provider_arns=None): | ||
# TODO: double check remaining authorizers. This only handles | ||
# cognito_user_pools. | ||
warnings.warn( | ||
"define_authorizer() is deprecated and will be removed in future " | ||
"versions of chalice. Please use CognitoUserPoolAuthorizer(...) " | ||
"instead", PendingDeprecationWarning) | ||
self._authorizers[name] = { | ||
'header': header, | ||
'auth_type': auth_type, | ||
'provider_arns': provider_arns | ||
'provider_arns': provider_arns, | ||
} | ||
|
||
def route(self, path, **kwargs): | ||
|
@@ -302,6 +359,7 @@ def _add_route(self, path, view_func, **kwargs): | |
name = kwargs.pop('name', view_func.__name__) | ||
methods = kwargs.pop('methods', ['GET']) | ||
authorizer_name = kwargs.pop('authorizer_name', None) | ||
authorizer = kwargs.pop('authorizer', None) | ||
api_key_required = kwargs.pop('api_key_required', None) | ||
content_types = kwargs.pop('content_types', ['application/json']) | ||
cors = kwargs.pop('cors', False) | ||
|
@@ -319,7 +377,7 @@ def _add_route(self, path, view_func, **kwargs): | |
"URL paths must be unique." % path) | ||
entry = RouteEntry(view_func, name, path, methods, | ||
authorizer_name, api_key_required, | ||
content_types, cors) | ||
content_types, cors, authorizer) | ||
self.routes[path] = entry | ||
|
||
def __call__(self, event, context): | ||
|
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need a different route name here since the other example was for user pools