0

Tastypie を使用して Django 1.4.3 に基づいて API を作成しています。ApiKey を使用してユーザーを認証します。デフォルトでは、ApiKey は期限切れにできません。しかしcreated、apikey テーブルに datetime の列があります。年に変更しても2010キーは有効です。

私の質問は、列をcreated便利にし、たとえば 24 時間以上経過したキーへのアクセスを禁止する最も簡単な方法と、それが理にかなっている 方法を教えてください。

現時点では、どうすればそれを達成できるかさえわかりません。

すぐに解決できるとは思っていません。いくつかの役立つヒント。

4

1 に答える 1

2

get_keyApiKeyAuthentication でメソッドをオーバーライドすることで解決策を見つけました。

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
于 2013-06-13T18:05:20.883 に答える