-
Notifications
You must be signed in to change notification settings - Fork 31
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 #2248 from WikiWatershed/arr/api-authtoken
Geoprocessing API: Add Auth Tokens
- Loading branch information
Showing
9 changed files
with
214 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
from rest_framework.permissions import BasePermission | ||
from rest_framework.authentication import TokenAuthentication | ||
|
||
from django.conf import settings | ||
|
||
|
||
class IsTokenAuthenticatedOrClientApp(BasePermission): | ||
""" | ||
Global permission check for request being | ||
(a) from a whitelisted IP | ||
ie. is this our own client web app and not the swagger docs? | ||
(b) from a user authenticated via auth token | ||
""" | ||
|
||
def has_permission(self, request, view): | ||
ip_addr = request.META['REMOTE_ADDR'] | ||
from_swagger = 'api/docs' in request.META['HTTP_REFERER'] \ | ||
if 'HTTP_REFERER' in request.META else False | ||
whitelisted = ip_addr in settings.ALLOWED_HOSTS | ||
token_authenticated = type(request.successful_authenticator) \ | ||
is TokenAuthentication | ||
return (not from_swagger and whitelisted) or token_authenticated |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models, migrations | ||
from django.conf import settings | ||
from django.contrib.auth.models import User | ||
from rest_framework.authtoken.models import Token | ||
|
||
|
||
def add_auth_tokens_to_users(apps, schema_editor): | ||
for user in User.objects.all(): | ||
Token.objects.create(user=user) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('authtoken', '0001_initial'), | ||
('user', '0001_initial') | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(add_auth_tokens_to_users) | ||
] |
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.