Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 60a51917cb
Fetching contributors…

Cannot retrieve contributors at this time

52 lines (39 sloc) 1.497 kb
"""An incredibly simple HTTP basic auth implementation."""
from base64 import b64decode, b64encode
from urllib import quote, unquote
class DecodeError(Exception):
pass
def encode(username, password):
"""Returns an HTTP basic authentication encrypted string given a valid
username and password.
"""
return 'Basic ' + b64encode('%s:%s' % (quote(username), quote(password)))
def decode(encoded_str):
"""Decode an encrypted HTTP basic authentication string. Returns a tuple of
the form (username, password), and raises a DecodeError exception if
nothing could be decoded.
"""
split = encoded_str.strip().split(' ')
# If split is only one element, try to decode the username and password
# directly.
if len(split) == 1:
try:
username, password = b64decode(split[0]).split(':')
except:
raise DecodeError
# If there are only two elements, check the first and ensure it says
# 'basic' so that we know we're about to decode the right thing. If not,
# bail out.
elif len(split) == 2:
if split[0].strip().lower() == 'basic':
try:
username, password = b64decode(split[1]).split(':')
except:
raise DecodeError
else:
raise DecodeError
# If there are more than 2 elements, something crazy must be happening.
# Bail.
else:
raise DecodeError
return unquote(username), unquote(password)
Jump to Line
Something went wrong with that request. Please try again.