2016-10-03 4 views
0

にrequest.METAに追加されていない私の設定でdocsカスタムヘッダはDjangoの残りのフレームワーク

# custom_permissions.py 

from rest_framework import authentication 
from rest_framework import exceptions 

class KeyAuthentication(authentication.BaseAuthentication): 
    def authenticate(self, request): 
     key = request.META.get('Authorization') 
     print(key) 
     if not key: 
      raise exceptions.AuthenticationFailed('Authentication failed.') 

     try: 
      key = ApiKey.objects.get(key=key) 
     except ApiKey.DoesNotExist: 
      raise exceptions.AuthenticationFailed('Authentication failed.') 

    return (key, None) 

で説明したように、私は、カスタム認証を実装:

# settings.py 

REST_FRAMEWORK = { 
    'DEFAULT_AUTHENTICATION_CLASSES': (
     'api_server.apps.api_v1.custom_permissions.KeyAuthentication', 
    ), 
    'DEFAULT_PERMISSION_CLASSES': (
     'rest_framework.permissions.AllowAny', 
    ), 
} 

テスト中に期待どおりに動作します:

def test_1(self): 
    client = APIClient() 
    client.credentials(X_SECRET_KEY='INVALID_KEY') 
    response = client.get('/v1/resource/') 
    self.assertEqual(response.status_code, 403) 
    self.assertEqual(response.data, {'detail': 'Authentication failed.'}) 

def test_2(self): 
    client = APIClient() 
    client.credentials(X_SECRET_KEY='FEJ5UI') 
    response = client.get('/v1/resource/') 
    self.assertEqual(response.status_code, 200) 

しかし、私がcurlとローカルにrでテストするとrequest.METAにはX_SECRET_KEYヘッダーがありません。端末にはNoneが印刷されていますが、受信した鍵が必要です。

$ curl -X GET localhost:8080/v1/resource/ -H "X_SECRET_KEY=FEJ5UI" 
{'detail': 'Authentication failed.'} 

何か問題がありますか?

答えて

関連する問題