2013-06-13 2 views
0

Tastypie가 포함 된 Django 1.4.3을 기반으로 API를 만들고 있습니다. 나는 ApiKey를 사용하여 사용자를 인증합니다. 기본적으로 ApiKey는 만료되지 않습니다. 그러나 datetime이 apikey 테이블에있는 열 created이 있습니다. 2010 년으로 변경하더라도 키는 여전히 유효합니다.Tastypie 자동 로그 아웃

제 질문은 어떻게하면 created 열을 유용하게 만들 수 있으며, 가장 쉬운 방법으로 24 시간을 말하는 것보다 오래된 키에 대한 액세스를 금지하고 의미가 있습니까?

현재로서는 어떻게 달성 할 수 있을지 전혀 모르겠다.

나는 해결책을 기대하지 않습니다. 유용한 힌트.

답변

2

ApiKeyAuthentication에서 get_key 메서드를 재정 의하여 솔루션을 찾았습니다.

class MyApiKeyAuthentication(ApiKeyAuthentication): 
    def get_key(self, user, api_key): 
     """ 
     Attempts to find the API key for the user. Uses ``ApiKey`` by default 
     but can be overridden. 
     """ 
     from tastypie.models import ApiKey 

     try: 
      api_key = ApiKey.objects.get(user=user, key=api_key) 
      current_time = datetime.utcnow() 
      current_time = current_time.replace(tzinfo=pytz.utc) 

      week = timedelta(7) 

      if not (current_time - api_key.created) < week: 
       api_key.delete() 
       return self._unauthorized() 
      else: 
       api_key.created = current_time 
       api_key.save() 

     except ApiKey.DoesNotExist: 
      return self._unauthorized() 

     return True