-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #503 from geoadmin/develop
New Release v1.31.0 - #minor
- Loading branch information
Showing
95 changed files
with
2,197 additions
and
1,498 deletions.
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
REMOTE_USER_HEADER = "HTTP_GEOADMIN_USERNAME" | ||
|
||
|
||
def validate_username_header(request): | ||
"""Drop the Geoadmin-Username header if it's invalid. | ||
This should be called before making any decision based on the value of the | ||
Geoadmin-Username header. | ||
API Gateway always sends the Geoadmin-Username header regardless of | ||
whether it was able to authenticate the user. If it could not | ||
authenticate the user, the value of the header as seen on the wire is a | ||
single whitespace. An hexdump looks like this: | ||
47 65 6f 61 64 6d 69 6e 5f 75 73 65 72 6e 61 6d 65 3a 20 0d 0a | ||
Geoadmin-Username:... | ||
This doesn't seem possible to reproduce with curl. It is possible to | ||
reproduce with wget. It is unclear whether that technically counts as an | ||
empty value or a whitespace. It is also possible that AWS change their | ||
implementation later to send something slightly different. Regardless, | ||
we already have a separate signal to tell us whether that value is | ||
valid: Geoadmin-Authenticated. So we only consider Geoadmin-Username if | ||
Geoadmin-Authenticated is set to "true". | ||
Based on discussion in https://code.djangoproject.com/ticket/35971 | ||
""" | ||
apigw_auth = request.META.get("HTTP_GEOADMIN_AUTHENTICATED", "false").lower() == "true" | ||
if not apigw_auth and REMOTE_USER_HEADER in request.META: | ||
del request.META[REMOTE_USER_HEADER] |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from middleware import api_gateway | ||
|
||
from django.conf import settings | ||
|
||
from rest_framework.authentication import RemoteUserAuthentication | ||
|
||
|
||
class ApiGatewayAuthentication(RemoteUserAuthentication): | ||
header = api_gateway.REMOTE_USER_HEADER | ||
|
||
def authenticate(self, request): | ||
if not settings.FEATURE_AUTH_ENABLE_APIGW: | ||
return None | ||
|
||
api_gateway.validate_username_header(request) | ||
return super().authenticate(request) | ||
|
||
def authenticate_header(self, request): | ||
# For this authentication method, users send a "Bearer" token via the | ||
# Authorization header. API Gateway looks up that token in Cognito and | ||
# sets the Geoadmin-Username and Geoadmin-Authenticated headers. In this | ||
# module we only care about the Geoadmin-* headers. But when | ||
# authentication fails with a 401 error we need to hint at the correct | ||
# authentication method from the point of view of the user, which is the | ||
# Authorization/Bearer scheme. | ||
return 'Bearer' |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from middleware import api_gateway | ||
|
||
from django.conf import settings | ||
from django.contrib.auth.backends import RemoteUserBackend | ||
from django.contrib.auth.middleware import PersistentRemoteUserMiddleware | ||
|
||
|
||
class ApiGatewayMiddleware(PersistentRemoteUserMiddleware): | ||
"""Persist user authentication based on the API Gateway headers.""" | ||
header = api_gateway.REMOTE_USER_HEADER | ||
|
||
def process_request(self, request): | ||
if not settings.FEATURE_AUTH_ENABLE_APIGW: | ||
return None | ||
|
||
api_gateway.validate_username_header(request) | ||
return super().process_request(request) | ||
|
||
|
||
class ApiGatewayUserBackend(RemoteUserBackend): | ||
""" This backend is to be used in conjunction with the ``ApiGatewayMiddleware`. | ||
It is probably not needed to provide a custom remote user backend as our custom remote user | ||
middleware will never call authenticate if the feature is not enabled. But better be safe than | ||
sorry. | ||
""" | ||
|
||
def authenticate(self, request, remote_user): | ||
if not settings.FEATURE_AUTH_ENABLE_APIGW: | ||
return None | ||
|
||
return super().authenticate(request, remote_user) |
This file was deleted.
Oops, something went wrong.
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
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
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
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.