An incredibly simple HTTP basic auth implementation.
Project description
# python-basicauth
A dead simple HTTP basic auth encoder and decoder. Why? Because HTTP should be
drop dead easy. That's why.
![HTTP Basic Auth?!](https://github.com/rdegges/python-basicauth/raw/master/http_basic_auth.jpg)
## Install
To install ``python-basicauth``, simply run ``pip install basicauth`` and
you'll get the latest version installed automatically.
## Usage
To generate an encoded basic auth string:
``` python
>>> from basicauth import encode
>>> username, password = 'rdegges', 'omghax!!!'
>>> encoded_str = encode(username, password)
>>> print encoded_str
'Basic cmRlZ2dlczpvbWdoYXglMjElMjElMjE='
```
To decode an encoded basic auth string:
``` python
>>> from basicauth import decode
>>> encoded_str = 'Basic cmRlZ2dlczpvbWdoYXglMjElMjElMjE=' # From the example above.
>>> username, password = decode(encoded_str)
>>> print (username, password)
('rdegges', 'omghax!!!')
```
We can also decode the hash directly:
``` python
>>> from basicauth import decode
>>> encoded_str = 'cmRlZ2dlczpvbWdoYXglMjElMjElMjE=' # From the example above.
>>> username, password = decode(encoded_str)
>>> print (username, password)
('rdegges', 'omghax!!!')
```
And if there are errors:
``` python
>>> from basicauth import decode, DecodeError
>>> encoded_str = 'lol omg cmRlZ2dlczpvbWdoYXglMjElMjElMjE=' # Invalid hash.
>>> username, password = decode(encoded_str)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "basicauth/__init__.py", line 49, in decode
raise DecodeError
basicauth.DecodeError
```
**NOTE**: The [HTTP Basic Authentication spec](http://www.ietf.org/rfc/rfc2617.txt)
doesn **NOT** allow you to include any colon characters (`:`) in the username
field. Colons are allowed in the password field -- but that's it.
## Tests
[![Build Status](https://secure.travis-ci.org/rdegges/python-basicauth.png?branch=master)](http://travis-ci.org/rdegges/python-basicauth)
Want to run the tests? No problem:
``` bash
$ git clone git://github.com/rdegges/python-basicauth.git
$ cd python-basicauth
$ python setup.py develop
...
$ pip install -r requirements.txt # Install test dependencies.
$ nosetests
.............
----------------------------------------------------------------------
Ran 13 tests in 0.166s
OK
```
A dead simple HTTP basic auth encoder and decoder. Why? Because HTTP should be
drop dead easy. That's why.
![HTTP Basic Auth?!](https://github.com/rdegges/python-basicauth/raw/master/http_basic_auth.jpg)
## Install
To install ``python-basicauth``, simply run ``pip install basicauth`` and
you'll get the latest version installed automatically.
## Usage
To generate an encoded basic auth string:
``` python
>>> from basicauth import encode
>>> username, password = 'rdegges', 'omghax!!!'
>>> encoded_str = encode(username, password)
>>> print encoded_str
'Basic cmRlZ2dlczpvbWdoYXglMjElMjElMjE='
```
To decode an encoded basic auth string:
``` python
>>> from basicauth import decode
>>> encoded_str = 'Basic cmRlZ2dlczpvbWdoYXglMjElMjElMjE=' # From the example above.
>>> username, password = decode(encoded_str)
>>> print (username, password)
('rdegges', 'omghax!!!')
```
We can also decode the hash directly:
``` python
>>> from basicauth import decode
>>> encoded_str = 'cmRlZ2dlczpvbWdoYXglMjElMjElMjE=' # From the example above.
>>> username, password = decode(encoded_str)
>>> print (username, password)
('rdegges', 'omghax!!!')
```
And if there are errors:
``` python
>>> from basicauth import decode, DecodeError
>>> encoded_str = 'lol omg cmRlZ2dlczpvbWdoYXglMjElMjElMjE=' # Invalid hash.
>>> username, password = decode(encoded_str)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "basicauth/__init__.py", line 49, in decode
raise DecodeError
basicauth.DecodeError
```
**NOTE**: The [HTTP Basic Authentication spec](http://www.ietf.org/rfc/rfc2617.txt)
doesn **NOT** allow you to include any colon characters (`:`) in the username
field. Colons are allowed in the password field -- but that's it.
## Tests
[![Build Status](https://secure.travis-ci.org/rdegges/python-basicauth.png?branch=master)](http://travis-ci.org/rdegges/python-basicauth)
Want to run the tests? No problem:
``` bash
$ git clone git://github.com/rdegges/python-basicauth.git
$ cd python-basicauth
$ python setup.py develop
...
$ pip install -r requirements.txt # Install test dependencies.
$ nosetests
.............
----------------------------------------------------------------------
Ran 13 tests in 0.166s
OK
```
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
basicauth-0.3.tar.gz
(59.5 kB
view details)
File details
Details for the file basicauth-0.3.tar.gz
.
File metadata
- Download URL: basicauth-0.3.tar.gz
- Upload date:
- Size: 59.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1304a4063148315926ab5eb5d84e7e18d78dc9cda8299d9f4c698e6a5785b29 |
|
MD5 | 1a20ac1c6cce38a1e5caf439776c7675 |
|
BLAKE2b-256 | 08e90d1343e98853d984928c7398f9be60b8b068d70e7c82ffed6a7711aa8758 |