-
Notifications
You must be signed in to change notification settings - Fork 62
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 #4 from mwarkentin/token-auth
Token based authentication for endpoint
- Loading branch information
Showing
7 changed files
with
123 additions
and
7 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,60 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
test_django-watchman | ||
------------ | ||
Tests for `django-watchman` decorators module. | ||
""" | ||
|
||
from __future__ import unicode_literals | ||
|
||
import unittest | ||
|
||
from watchman import settings as watchman_settings | ||
from django.core.urlresolvers import reverse | ||
from django.test.client import Client | ||
|
||
|
||
class TestWatchman(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.client = Client() | ||
watchman_settings.WATCHMAN_TOKEN = 'foo' | ||
|
||
def test_200_ok_if_no_token_set(self): | ||
watchman_settings.WATCHMAN_TOKEN = None | ||
response = self.client.get(reverse('watchman.views.status')) | ||
self.assertEqual(response.status_code, 200) | ||
watchman_settings.WATCHMAN_TOKEN = 'foo' | ||
|
||
def test_200_ok_if_tokens_match(self): | ||
data = { | ||
'watchman-token': 'foo', | ||
} | ||
response = self.client.get(reverse('watchman.views.status'), data) | ||
self.assertEqual(response.status_code, 200) | ||
|
||
def test_required_token_param_can_be_renamed(self): | ||
watchman_settings.WATCHMAN_TOKEN_NAME = 'custom-token' | ||
data = { | ||
'custom-token': 'foo', | ||
} | ||
response = self.client.get(reverse('watchman.views.status'), data) | ||
self.assertEqual(response.status_code, 200) | ||
watchman_settings.WATCHMAN_TOKEN_NAME = 'watchman-token' | ||
|
||
def test_403_raised_if_missing_token(self): | ||
response = self.client.get(reverse('watchman.views.status')) | ||
self.assertEqual(response.status_code, 403) | ||
|
||
def test_403_raised_if_invalid_token(self): | ||
data = { | ||
'watchman-token': 'bar', | ||
} | ||
response = self.client.get(reverse('watchman.views.status'), data) | ||
self.assertEqual(response.status_code, 403) | ||
|
||
def tearDown(self): | ||
watchman_settings.WATCHMAN_TOKEN = None |
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,34 @@ | ||
from django.http import HttpResponseForbidden | ||
from django.views.decorators.csrf import csrf_exempt | ||
|
||
from functools import wraps | ||
|
||
from watchman import settings | ||
|
||
|
||
def token_required(view_func): | ||
""" | ||
Decorator which ensures that WATCHMAN_TOKEN is provided if set. | ||
WATCHMAN_TOKEN_NAME can also be set if the token GET parameter must be | ||
customized. | ||
""" | ||
|
||
def _validate_token(request): | ||
watchman_token = settings.WATCHMAN_TOKEN | ||
if watchman_token is None: | ||
return True | ||
|
||
watchman_token_name = settings.WATCHMAN_TOKEN_NAME | ||
return watchman_token == request.GET.get(watchman_token_name) | ||
|
||
@csrf_exempt | ||
@wraps(view_func) | ||
def _wrapped_view(request, *args, **kwargs): | ||
if _validate_token(request): | ||
return view_func(request, *args, **kwargs) | ||
|
||
return HttpResponseForbidden() | ||
|
||
return _wrapped_view |
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,5 @@ | ||
from django.conf import settings | ||
|
||
|
||
WATCHMAN_TOKEN = getattr(settings, 'WATCHMAN_TOKEN', None) | ||
WATCHMAN_TOKEN_NAME = getattr(settings, 'WATCHMAN_TOKEN_NAME', 'watchman-token') |
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