How to sign-in? Django TastyPie with ApiKeyAuthentication actual authentication Process

前提是你 提交于 2019-12-03 16:09:52

I'm using BasicAuth so it may be slightly different. But my solution is basicaly an empty resource that requires authentication. If the authentication is a success the service returns response code 200 and the authenticated user, I override obj_get_list and stuff the authenticated user in there. If the credentials are wrong the service returns response code 401.

 class LoginResource(ModelResource):
        class Meta:
            allowed_methods = ['get']
            resource_name = 'login'
            include_resource_uri = False
            object_class = User
            authentication = BasicAuthentication()
            authorization = DjangoAuthorization()

        def obj_get_list(self, bundle, **kwargs):
            return [bundle.request.user]

Okay I'll try to explain my point of view on the topic:

First the UserResource example on the tastypie page for me has one significant issue: The User Objects should not be presented at any time to the single User, they should be able to see they're own "profile" or whatever but never browse and see others. Of course that can be done and with UserResource by clearing the main "list view" of that resource and applying the APIKeyAuth to the individual profiles, but still I don't like the idea of UserResource.

Second in the form when you are developing an API(such as tastypie usage) the APIKey is the actual "password" so what should be send on request is not the username and password but the username and APIKey, which is obtained in other manners(normally an e-mail or some kind of website based UI). Than they are recommended to be send via Authorization Header and not in GET parameters.

Third when we are talking about API there is no such thing as sign-in - at least not in the RESTFULL APIs - it is in some sense connectionless, so you actually going to send the Authorization header with each request. As to the question yes you can overwrite the data. Look at the hydrate/dehydrate cycle in the Tastypie docs to understand how does it renders the content and if you have more question go ahead and ask.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!