Metadata-Version: 2.5
Name: tornado-http-auth
Version: 1.2.0
Summary: Digest and basic authentication for tornado
Project-URL: Homepage, https://github.com/gvalkov/tornado-http-auth
Project-URL: Repository, https://github.com/gvalkov/tornado-http-auth
Author-email: Georgi Valkov <georgi.t.valkov@gmail.com>
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: basic-auth,digest-auth,tornado
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.9
Requires-Dist: tornado>=4.0.0
Description-Content-Type: text/markdown

# tornado-http-auth

[![PyPI](https://img.shields.io/pypi/v/tornado-http-auth.svg?style=flat-square&label=latest%20stable%20version)](https://pypi.python.org/pypi/tornado-http-auth)
[![License](https://img.shields.io/pypi/l/tornado-http-auth.svg?style=flat-square&label=license)](https://pypi.python.org/pypi/tornado-http-auth)

Digest and basic authentication for the [Tornado](http://www.tornadoweb.org/en/stable/) web framework.
Based on code and ideas from Twisted's [cred](https://twistedmatrix.com/documents/15.4.0/core/howto/cred.html).

## Installation

The latest stable version of tornado-http-auth can be installed from pypi:

```bash
pip install tornado-http-auth
```

## Usage

```python
import tornado.ioloop
from tornado.web import RequestHandler, Application
from tornado_http_auth import DigestAuthMixin, BasicAuthMixin, auth_required

credentials = {'user1': 'pass1'}


# Example 1 (using decorator).
class MainHandler(DigestAuthMixin, RequestHandler):
    @auth_required(realm='Protected', auth_func=credentials.get)
    def get(self):
        self.write('Hello %s' % self._current_user)


# Example 2 (using prepare and get_authenticated_user).
class MainHandler(BasicAuthMixin, RequestHandler):
    def prepare(self):
        self.get_authenticated_user(check_credentials_func=credentials.get, realm='Protected')

    def get(self):
        self.write('Hello %s' % self._current_user)


app = Application([
    (r'/', MainHandler),
])

app.listen(8888)
tornado.ioloop.IOLoop.current().start()

# curl --user user1:pass1 -v http://localhost:8888  -> 200 OK
# curl --user user2:pass2 -v http://localhost:8888  -> 401 Unauthorized
# Remove or comment second class
# curl --digest --user user1:pass1 -v http://localhost:8888  -> 200 OK
# curl --digest --user user2:pass2 -v http://localhost:8888  -> 401 Unauthorized
```

## License

This project is released under the terms of the
[Apache License, Version 2.0](https://raw.github.com/gvalkov/tornado-http-auth/master/LICENSE).
