Adapters

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)[source]

Adapter for the Django framework.

Parameters:
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)[source]

Must write specified value to response.

Parameters:value (str) – String to be written to response.
set_header(key, value)[source]

Must set response headers to Key: value.

Parameters:
  • key (str) – Header name.
  • value (str) – Header value.
set_status(status)[source]

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

Parameters:status (str) – The HTTP response status.
class authomatic.adapters.Webapp2Adapter(handler)[source]

Adapter for the Webapp2 framework.

Inherits from the WebObAdapter.

Parameters:handler – A webapp2.RequestHandler instance.
class authomatic.adapters.WebObAdapter(request, response)[source]

Adapter for the WebOb package.

Parameters:
  • request – A WebOb Request instance.
  • response – A WebOb Response instance.
url

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

Returns:str
params

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

Returns:dict
cookies

Must return cookies as a dict.

Returns:dict
write(value)[source]

Must write specified value to response.

Parameters:value (str) – String to be written to response.
set_header(key, value)[source]

Must set response headers to Key: value.

Parameters:
  • key (str) – Header name.
  • value (str) – Header value.
set_status(status)[source]

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

Parameters:status (str) – The HTTP response status.
class authomatic.adapters.WerkzeugAdapter(request, response)[source]

Adapter for Flask and other Werkzeug based frameworks.

Thanks to Mark Steve Samson.

Parameters:
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)[source]

Must write specified value to response.

Parameters:value (str) – String to be written to response.
set_header(key, value)[source]

Must set response headers to Key: value.

Parameters:
  • key (str) – Header name.
  • value (str) – Header value.
set_status(status)[source]

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

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

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[source]

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)[source]

Must write specified value to response.

Parameters:value (str) – String to be written to response.
set_header(key, value)[source]

Must set response headers to Key: value.

Parameters:
  • key (str) – Header name.
  • value (str) – Header value.
set_status(status)[source]

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

Parameters:status (str) – The HTTP response status.
Fork me on GitHub