You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importbase64fromdjango.httpimportHttpResponsefromdjango.contrib.authimportauthenticate, logindefbasicauth_required(realm=""):
"Based on http://djangosnippets.org/snippets/243/"defexternal_wrapper(view):
defwrapper(request, *args, **kwargs):
if'HTTP_AUTHORIZATION'inrequest.META:
auth=request.META['HTTP_AUTHORIZATION'].split()
iflen(auth) ==2:
ifauth[0].lower() =="basic":
uname, passwd=base64.b64decode(auth[1]).split(':')
user=authenticate(username=uname, password=passwd)
ifuserisnotNone:
ifuser.is_active:
login(request, user)
request.user=userreturnview(request, *args, **kwargs)
# the authorization attempt failed. Send a 401# back to them to ask them to authenticate.#response=HttpResponse()
response.status_code=401response['WWW-Authenticate'] ='Basic realm="%s"'%realmreturnresponsereturnwrapperreturnexternal_wrapper
The text was updated successfully, but these errors were encountered:
support fo basic authentication via decorator
Implementation proposal:
The text was updated successfully, but these errors were encountered: