本文整理汇总了Python中auth.models.User.get_by_username方法的典型用法代码示例。如果您正苦于以下问题:Python User.get_by_username方法的具体用法?Python User.get_by_username怎么用?Python User.get_by_username使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类auth.models.User
的用法示例。
在下文中一共展示了User.get_by_username方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_user_from_http_auth
# 需要导入模块: from auth.models import User [as 别名]
# 或者: from auth.models.User import get_by_username [as 别名]
def get_user_from_http_auth():
"""
Basic Auth with base64 encoded username/password fields
Clients should authenticate by passing the base64 encoded email and
password in the "USERNAME" and "PASSWORD" HTTP headers respectively.
For Example::
USERNAME: dGVzdHVzZXJAd2VsbGF3YXJlLnVz
PASSWORD: TGludXggaXMgZm9yIGF3ZXNvbWUgcGVvcGxlLiBNYWNzIGFuZCBQQydzIGFyZSBmb3IgaWRpb3RzLg==
NOTE: HTTP_ prefix works as well
SIMPLE TEST: fire up runserver and in seperate terminal:
for username: [email protected] and password: testuser
>>> curl -H "USERNAME: dGVzdHVzZXJAd2VsbGF3YXJlLnVz" \
... -H "PASSWORD: TGludXggaXMgZm9yIGF3ZXNvbWUgcGVvcGxlLiBNYWNzIGFuZCBQQydzIGFyZSBmb3IgaWRpb3RzLg==" \
... -d "" http://localhost:8000/api/auth/
This will create a temporary Token for authorization for the set period of
time (see login view). This Client should expect a plain-text token key as
a response to the login. The Client is expected to base64 encode that token
for future requests.
@returns: auth.models.User | None
"""
header_username_keys = ['USERNAME', 'HTTP_USERNAME']
header_password_keys = ['PASSWORD', 'HTTP_PASSWORD']
for hdr_u_key in header_username_keys:
enc_username = request.headers.get(hdr_u_key, None)
if enc_username:
break
for hdr_p_key in header_password_keys:
enc_password = request.headers.get(hdr_p_key, None)
if enc_password:
break
if (not enc_username or enc_username == 'AA==') or (not enc_password or enc_password == 'AA=='):
logger.debug('No username or password')
print "no username or password supplied"
return None
from auth.models import User
try:
username = b64decode(enc_username)
password = b64decode(enc_password)
logger.debug("Username: %s | Password: %s" % (username, password))
user = User.get_by_username(username)
print "Got User: %s" % user
if not user:
print "No such user"
return None
if not user.is_active or not user.check_password(password):
print "User is not active or password check fail"
return None
print "user is good, sending back: %s, %s, %s" % (user.username, user.email, user.first_name)
return user
except User.DoesNotExist:
logger.warn("No such user")
except TypeError as e: # base64 decoding problem
logging.exception("Error decoding username and/or password")
return None