-
Notifications
You must be signed in to change notification settings - Fork 7
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 #1320 from unicef/staging
Staging
- Loading branch information
Showing
365 changed files
with
12,885 additions
and
3,561 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 |
---|---|---|
|
@@ -114,3 +114,5 @@ docs/_build/ | |
|
||
# custom settings files | ||
EquiTrack/EquiTrack/settings/custom.py | ||
|
||
*secrets.yml |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ exclude = | |
load_initial_data.py, | ||
publics/management/commands/xml, | ||
t2f/management/commands/et2f_init.py, | ||
.tox |
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,91 @@ | ||
import logging | ||
|
||
from django.conf import settings | ||
from django.contrib.auth import get_user_model | ||
import jwt | ||
from rest_framework.authentication import BasicAuthentication, SessionAuthentication, TokenAuthentication | ||
from rest_framework.exceptions import AuthenticationFailed, PermissionDenied | ||
from rest_framework_jwt.authentication import JSONWebTokenAuthentication | ||
from rest_framework_jwt.settings import api_settings | ||
from rest_framework_jwt.utils import jwt_payload_handler | ||
|
||
from EquiTrack.utils import set_country | ||
|
||
jwt_decode_handler = api_settings.JWT_DECODE_HANDLER | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class DRFBasicAuthMixin(BasicAuthentication): | ||
def authenticate(self, request): | ||
super_return = super(DRFBasicAuthMixin, self).authenticate(request) | ||
if not super_return: | ||
return None | ||
|
||
user, token = super_return | ||
set_country(user, request) | ||
return user, token | ||
|
||
|
||
class EtoolsTokenAuthentication(TokenAuthentication): | ||
|
||
def authenticate(self, request): | ||
super_return = super(EtoolsTokenAuthentication, self).authenticate(request) | ||
if not super_return: | ||
return None | ||
|
||
user, token = super_return | ||
set_country(user, request) | ||
return user, token | ||
|
||
|
||
class EToolsTenantJWTAuthentication(JSONWebTokenAuthentication): | ||
""" | ||
Handles setting the tenant after a JWT successful authentication | ||
""" | ||
def authenticate(self, request): | ||
|
||
jwt_value = self.get_jwt_value(request) | ||
if jwt_value is None: | ||
# no JWT token return to skip this authentication mechanism | ||
return None | ||
|
||
try: | ||
user, jwt_value = super(EToolsTenantJWTAuthentication, self).authenticate(request) | ||
except TypeError: | ||
raise PermissionDenied(detail='No valid authentication provided') | ||
except AuthenticationFailed: | ||
# Try again | ||
if getattr(settings, 'JWT_ALLOW_NON_EXISTENT_USERS', False): | ||
try: | ||
# try and see if the token is valid | ||
payload = jwt_decode_handler(jwt_value) | ||
except (jwt.ExpiredSignature, jwt.DecodeError): | ||
raise PermissionDenied(detail='Authentication Failed') | ||
else: | ||
# signature is valid user does not exist... setting default authenticated user | ||
user = get_user_model().objects.get(username=settings.DEFAULT_UNICEF_USER) | ||
setattr(user, 'jwt_payload', payload) | ||
else: | ||
raise PermissionDenied(detail='Authentication Failed') | ||
|
||
if not user.profile.country: | ||
raise PermissionDenied(detail='No country found for user') | ||
|
||
if user.profile.country_override and user.profile.country != user.profile.country_override: | ||
user.profile.country = user.profile.country_override | ||
user.profile.save() | ||
|
||
set_country(user, request) | ||
return user, jwt_value | ||
|
||
|
||
class CsrfExemptSessionAuthentication(SessionAuthentication): | ||
|
||
def enforce_csrf(self, request): | ||
return | ||
|
||
|
||
def custom_jwt_payload_handler(user): | ||
payload = jwt_payload_handler(user) | ||
payload['groups'] = list(user.groups.values_list('name', flat=True)) | ||
return payload |
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 |
---|---|---|
|
@@ -9,7 +9,8 @@ | |
"postal_code": "10017", | ||
"country": "U.S.A.", | ||
"email": "[email protected]", | ||
"phone_number": "" | ||
"phone_number": "", | ||
"unicef_users_allowed": true | ||
} | ||
}, | ||
{ | ||
|
Oops, something went wrong.