Skip to content

Commit

Permalink
regression tests, +updates
Browse files Browse the repository at this point in the history
  • Loading branch information
fiorix committed Jan 18, 2013
1 parent 5800fea commit e8a6492
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
29 changes: 16 additions & 13 deletions demos/httpauth/httpauthdemo.py
Expand Up @@ -37,19 +37,22 @@ def __init__(self):
def HTTPBasic(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
try:
auth_type, auth_data = \
self.request.headers["Authorization"].split()
assert auth_type == "Basic"
usr, pwd = base64.b64decode(auth_data).split(":", 1)
assert usr == "root@localhost"
assert pwd == "123"
except:
# All arguments are optional. Defaults are:
# log_message=None, auth_type="Basic", realm="Restricted Access"
msg = None
if "Authorization" in self.request.headers:
auth_type, data = self.request.headers["Authorization"].split()
try:
assert auth_type == "Basic"
usr, pwd = base64.b64decode(data).split(":", 1)
assert usr == "root@localhost"
assert pwd == "123"
except AssertionError:
msg = "Authentication failed"
else:
msg = "Authentication required"

if msg:
raise cyclone.web.HTTPAuthenticationRequired(
log_message="Authentication failed!",
auth_type="Basic", realm="DEMO")
log_message=msg, auth_type="Basic", realm="DEMO")
else:
self._current_user = usr
return method(self, *args, **kwargs)
Expand All @@ -64,7 +67,7 @@ def get(self):

def main():
log.startLogging(sys.stdout)
reactor.listenTCP(8888, Application(), interface="127.0.0.1")
reactor.listenTCP(8888, Application(), interface="0.0.0.0")
reactor.run()


Expand Down
39 changes: 22 additions & 17 deletions demos/httpauth/httpauthdemo_redis.py
Expand Up @@ -42,25 +42,30 @@ def HTTPBasic(method):
@defer.inlineCallbacks
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
try:
auth_type, auth_data = \
self.request.headers["Authorization"].split()
assert auth_type == "Basic"
usr, pwd = base64.b64decode(auth_data).split(":", 1)
except:
raise cyclone.web.HTTPAuthenticationRequired

try:
redis_pwd = yield self.redisdb.get("cyclone:%s" % usr)
except Exception, e:
log.msg("Redis failed to get('cyclone:%s'): %s" % (usr, str(e)))
raise cyclone.web.HTTPError(503) # Service Unavailable

if pwd != str(redis_pwd):
raise cyclone.web.HTTPAuthenticationRequired
msg = None
if "Authorization" in self.request.headers:
auth_type, data = self.request.headers["Authorization"].split()
try:
assert auth_type == "Basic"
usr, pwd = base64.b64decode(data).split(":", 1)
redis_pwd = yield self.redisdb.get("cyclone:%s" % usr)
assert pwd == str(redis_pwd) # it may come back as an int!
except AssertionError:
msg = "Authentication failed"
except cyclone.redis.ConnectionError, e:
# There's nothing we can do here. Just wait for the
# connection to resume.
log.msg("Redis is unavailable: %s" % e)
raise cyclone.web.HTTPError(503) # Service Unavailable
else:
msg = "Authentication required"

if msg:
raise cyclone.web.HTTPAuthenticationRequired(
log_message=msg, auth_type="Basic", realm="DEMO")
else:
self._current_user = usr
defer.returnValue(method(self, *args, **kwargs))
yield defer.maybeDeferred(method, self, *args, **kwargs)
return wrapper


Expand Down

0 comments on commit e8a6492

Please sign in to comment.