Authomatic
is an authorization / authentication
client library for Python web applications
inspired by Alex Vagin’s Simpleauth.
In fact, I almost named it Deadsimpleauth,
but that name would be too long
for a succinct library.

- Tweet

# Adapters

Contents

- Adapters
  - Available Adapters
  - Implementing an Adapter

The `authomatic.login()` function needs access to functionality like getting the URL of the handler where it is being called, getting the request params and cookies and writing the body, headers and status to the response.

Since implementation of these features varies across Python web frameworks, the Authomatic library uses adapters to unify these differences into a single interface.

## Available Adapters

If you are missing an adapter for the framework of your choice, please open an enhancement issue or consider a contribution to this module by implementing one by yourself. Its very easy and shouldn’t take you more than a few minutes.

class `authomatic.adapters.``DjangoAdapter`(request, response)

  Adapter for the Django framework.

  Parameters: | request – An instance of the `django.http.HttpRequest` class. response – An instance of the `django.http.HttpResponse` class.

class `authomatic.adapters.``Webapp2Adapter`(handler)

  Adapter for the Webapp2 framework.

  Inherits from the `WebObAdapter`.

  Parameters: | handler – A `webapp2.RequestHandler` instance.

class `authomatic.adapters.``WebObAdapter`(request, response)

  Adapter for the WebOb package.

  Parameters: | request – A WebOb `Request` instance. response – A WebOb `Response` instance.

class `authomatic.adapters.``WerkzeugAdapter`(request, response)

  Adapter for Flask and other Werkzeug based frameworks.

  Thanks to Mark Steve Samson.

  Parameters: | request – Instance of the `werkzeug.wrappers.Request` class. response – Instance of the `werkzeug.wrappers.Response` class.

## Implementing an Adapter

Implementing an adapter for a Python web framework is pretty easy.

Do it by subclassing the `BaseAdapter` abstract class. There are only six members that you need to implement.

Moreover if your framework is based on the WebOb or Werkzeug package you can subclass the `WebObAdapter` or `WerkzeugAdapter` respectively.

class `authomatic.adapters.``BaseAdapter`

  Base class for platform adapters

  Defines common interface for WSGI framework specific functionality.

  `params`

    Must return a `dict` of all request parameters of any HTTP method.

    Returns: | `dict`

  `url`

    Must return the url of the actual request including path but without query and fragment

    Returns: | `str`

  `cookies`

    Must return cookies as a `dict`.

    Returns: | `dict`

  `write`(value)

    Must write specified value to response.

    Parameters: | value (str) – String to be written to response.

  `set_header`(key, value)

    Must set response headers to `Key: value`.

    Parameters: | key (str) – Header name. value (str) – Header value.

  `set_status`(status)

    Must set the response status e.g. `'302 Found'`.

    Parameters: | status (str) – The HTTP response status.