0

認証が成功するとユーザー名とパスワードを返す Django-Tastypie でカスタム認証を使用しています。ModelResource インスタンスでは、認証が成功すると、ユーザー名とパスワードが利用可能になります。

class TestResource(ModelResource):

    class Meta:
        queryset = Test.remote_supported.get_all(username,password)
        resource_name = 'test'
        filtering = {
            'id' : ALL,
        }
        detail_allowed_methods = ['get', 'post', 'patch']       
        authorization = Authorization()
        authentication = MyCustomAuth()
        always_return_data = True

    def __init__(self, api_name=None):
        self.username = None
        self.password = None
        super(TestResource, self).__init__(api_name)

    def is_authenticated(self, request):
        auth_result = self._meta.authentication.is_authenticated(request)

        if isinstance(auth_result, HttpResponse):
            raise ImmediateHttpResponse(response=auth_result)

        if not auth_result is True:
            raise ImmediateHttpResponse(response=http.HttpUnauthorized())

        # this is where I receive the username and password from my custom auth
        self.username, self.password = self._meta.authentication.get_credentials()

そのユーザー名とそのパスワードはメタクラスで利用できないため、このコードは明らかに機能しません。たとえあったとしても、それを変更すると、このインスタンスだけに影響するのではなく、すべてのインスタンスに影響します。これは私の意図ではありません。パスワードは RESTful クエリごとにする必要があります。

これは私のモデルがどのように見えるかです:

class RemoteAuthSupported(models.Manager):
    def get_all(self, username, password):
        # ... here I do some custom operations with the username and password
        return super(RemoteAuthSupported, self).get_query_set()  

class Test(models.Model):
    objects = models.Manager()
    remote_supported = RemoteAuthSupported()

    # ... the field declarations follow here ... #

これを実行しようとしている理由は、Django アプリケーションで非 ORM データ ソースを使用しているためです。Django アプリケーションには独自の認証が必要ですが、ユーザー名とパスワードは同じです。Tastypie ModelResource から Django Model に渡されるこのパラメーターを処理する方法は何でしょうか? ここで、ユーザー モデルが使用されていないことを言及しておく必要があります。

4

2 に答える 2