Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
40 lines (26 sloc) 1.41 KB
HTTP Basic Authentication
httpauthdemo.py:
A very simple, pretty much useless example.
The idea is to show how to build a decorator to request valid credentials,
and send HTTP "401 Unauthorized" when necessary.
httpauthdemo_redis.py:
This is more of a real-world example, authenticating against a database,
in this case, redis. You can easily port it to standard RDBMs such as
MySQL, PostgreSQL and others, using twisted.enterprise.adbapi.
Again, it does so via the decorator, which extract credentials from Redis
and compare them against what people typed on their browser.
If you run this example without a redis-server, it will always return HTTP
503 "Service Unavailable" due to lack of communication with the database
backend. Once you start redis, it will automatically connect to it.
Make sure you create a user in redis to test the authentication system.
From the command line, run:
$ redis-cli set cyclone:root 123
Then point your browser to http://localhost:8888 and authenticate as root/123.
httpauthdemo_mongo.py:
Same as the above, but for the MongoDB database.
Make sure you have txredis installed, otherwise go grab it from GitHub:
https://github.com/fiorix/mongo-async-python-driver
Create a new user:
curl -D - -d "username=root&password=123" http://localhost:8888/createuser
Then point your browser to http://localhost:8888 and authenticate as root/123.
Have fun!