【发布时间】:2013-05-01 15:27:19
【问题描述】:
我已经在Django(使用Mongo DB)中实现了基本身份验证,并且是其中的新手。我正在尝试使用我在 iOS 和 android 应用程序中使用它制作的一些 Web 服务。现在,Django 后端在 Postman(REST 客户端)中返回所需的响应,但不在 iOS 应用程序中(通过身份验证挑战处理基本身份验证请求并在幕后工作),所以我希望确保 Django 实现是正确(以便我将注意力转移到iOS方面)。在 iOS 中,该服务返回一个 401 Unauthorized 状态和一个空响应。此外,不会收到身份验证质询(由操作系统自动调用)。我还尝试过表现类似的第三方网络库!所有这一切让我相信Django 的实现有问题。那么如何验证特定的 Web 服务(不仅使用 Django 构建)是否根据基本身份验证协议工作?请提示和技巧。
这是验证码:
import base64
from tastypie.authentication import Authentication
from .backend import CustomMongoEngineBackend
class MongoBasicAuthentication(Authentication):
"""
Customizing basic authentication to make it compatible with mongo
"""
def is_authenticated(self, request, **kwargs):
"""
Checks a user's nasic auth credentials against the current Mongo Auth Backend.
"""
if not request.META.get('HTTP_AUTHORIZATION'):
return False
try:
(auth_type, data) = request.META.get('HTTP_AUTHORIZATION').split()
if auth_type.lower() != 'basic':
return False
user_pass = base64.b64decode(data)
except:
return False
bits = user_pass.split(':', 1)
if len(bits) != 2:
return False
backend = CustomMongoEngineBackend()
user = backend.authenticate(email=bits[0], password=bits[1])
if user:
request.user = user
else:
return False
return True
def get_identifier(self, request):
return request.META.get('REMOTE_USER', 'nouser')
【问题讨论】:
标签: ios django mongodb basic-authentication tastypie