Skip to content

Instantly share code, notes, and snippets.

@rombr
Created August 10, 2015 09:45
Show Gist options
  • Save rombr/3ea7cee9ab73bd8426cc to your computer and use it in GitHub Desktop.
Save rombr/3ea7cee9ab73bd8426cc to your computer and use it in GitHub Desktop.
Simple key authentication for Django REST framework
# -*- 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