我想在我的自定义身份验证来覆盖is_authenticated。 我有一个像这样简单的东西(先从):
class MyAuthentication(BasicAuthentication):
def __init__(self, *args, **kwargs):
super(MyAuthentication, self).__init__(*args, **kwargs)
def is_authenticated(self, request, **kwargs):
return True
然后在我的ModelResource我有
class LoginUserResource(ModelResource):
class Meta:
resource_name = 'login'
queryset = User.objects.all()
excludes = ['id', 'email', 'password', 'is_staff', 'is_superuser']
list_allowed_methods = ['post']
authentication = MyAuthentication()
authorization = DjangoAuthorization()
我不断收到一个500错误回来"error_message": "column username is not unique"
。 我只有在数据库中一个用户名,它是我想对用户进行认证。
任何想法,为什么它返回这个错误? 我怎么会允许一个API客户端登录?
谢谢您的帮助。
你的方法将尝试创建与您正在使用验证用户名新用户。 在DB层这将泡了,因为你已经注意到,这样的用户已经存在。
你需要的是建立一个UserResource
,就可以添加一个方法,用户可以张贴和数据传递的用户名/密码登录。
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from tastypie.http import HttpUnauthorized, HttpForbidden
from django.conf.urls import url
from tastypie.utils import trailing_slash
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
fields = ['first_name', 'last_name', 'email']
allowed_methods = ['get', 'post']
resource_name = 'user'
def override_urls(self):
return [
url(r"^(?P<resource_name>%s)/login%s$" %
(self._meta.resource_name, trailing_slash()),
self.wrap_view('login'), name="api_login"),
url(r'^(?P<resource_name>%s)/logout%s$' %
(self._meta.resource_name, trailing_slash()),
self.wrap_view('logout'), name='api_logout'),
]
def login(self, request, **kwargs):
self.method_check(request, allowed=['post'])
data = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))
username = data.get('username', '')
password = data.get('password', '')
user = authenticate(username=username, password=password)
if user:
if user.is_active:
login(request, user)
return self.create_response(request, {
'success': True
})
else:
return self.create_response(request, {
'success': False,
'reason': 'disabled',
}, HttpForbidden )
else:
return self.create_response(request, {
'success': False,
'reason': 'incorrect',
}, HttpUnauthorized )
def logout(self, request, **kwargs):
self.method_check(request, allowed=['get'])
if request.user and request.user.is_authenticated():
logout(request)
return self.create_response(request, { 'success': True })
else:
return self.create_response(request, { 'success': False }, HttpUnauthorized)
现在你可以做发个帖子http://hostname/api/user/login
与数据{ 'username' : 'me', 'password' : 'l33t' }
此更新的GET方法去除安全问题。 适用于Django的1.5.4。
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
allowed_methods = ['post']
def prepend_urls(self):
return [
url(r"^user/login/$", self.wrap_view('login'), name="api_login"),
url(r"^user/logout/$", self.wrap_view('logout'), name='api_logout'),
]
def login(self, request, **kwargs):
self.method_check(request, allowed=['post'])
data = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))
username = data.get('username', '')
password = data.get('password', '')
user = authenticate(username=username, password=password)
if user:
if user.is_active:
login(request, user)
return self.create_response(request, {
'success': True
})
else:
return self.create_response(request, {
'success': False,
'reason': 'disabled',
}, HttpForbidden )
else:
return self.create_response(request, {
'success': False,
'reason': 'incorrect',
}, HttpUnauthorized )
def logout(self, request, **kwargs):
self.method_check(request, allowed=['post'])
if request.user and request.user.is_authenticated():
logout(request)
return self.create_response(request, { 'success': True })
else:
return self.create_response(request, { 'success': False }, HttpUnauthorized)