Created
August 10, 2015 09:45
-
-
Save rombr/3ea7cee9ab73bd8426cc to your computer and use it in GitHub Desktop.
Simple key authentication for Django REST framework
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
# -*- coding:utf-8 -*- | |
from django.conf import settings | |
from rest_framework import authentication | |
from rest_framework import exceptions | |
class ApiKeyAuthentication(authentication.BaseAuthentication): | |
''' | |
Аутентификация по задаанным в настройках ключам | |
''' | |
def authenticate(self, request): | |
api_key = request.GET.get('api_key') | |
if not api_key: | |
# return None for skip to next backend | |
raise exceptions.AuthenticationFailed('Invalid API key') | |
if api_key in settings.API_KEYS: | |
user = None | |
else: | |
raise exceptions.AuthenticationFailed('Invalid API key') | |
return (user, None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment