Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
touilleMan committed Dec 15, 2014
1 parent b941cdf commit 976e2ca
Show file tree
Hide file tree
Showing 78 changed files with 21,718 additions and 60 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# User specific environment variables
init.env
venv/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
60 changes: 0 additions & 60 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -278,63 +278,3 @@ PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.

END OF TERMS AND CONDITIONS

How to Apply These Terms to Your New Programs

If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.

To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.

{description}
Copyright (C) {year} {fullname}

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Also add information on how to contact you by electronic and paper mail.

If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:

Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.

The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.

You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:

Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.

{signature of Ty Coon}, 1 April 1989
Ty Coon, President of Vice

This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Scille-nature-api

Projet viginature du Museum national d'histoire naturelle
21 changes: 21 additions & 0 deletions authomatic/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
"""
This is the only interface that you should ever need to get a **user** logged in, get
**his/her** info and credentials, deserialize the credentials
and access **his/her protected resources**.
.. autosummary::
:nosignatures:
authomatic.setup
authomatic.login
authomatic.provider_id
authomatic.access
authomatic.async_access
authomatic.credentials
authomatic.request_elements
authomatic.backend
"""

from .core import Authomatic, setup, login, provider_id, access, async_access, credentials, request_elements, backend
272 changes: 272 additions & 0 deletions authomatic/adapters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
# -*- coding: utf-8 -*-
"""
Adapters
--------
.. contents::
:backlinks: none
The :func:`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 <https://github.com/peterhudec/authomatic/issues>`_
or consider a contribution to this module by :ref:`implementing <implement_adapters>` one by yourself.
Its very easy and shouldn't take you more than a few minutes.
.. autoclass:: DjangoAdapter
:members:
.. autoclass:: Webapp2Adapter
:members:
.. autoclass:: WebObAdapter
:members:
.. autoclass:: WerkzeugAdapter
:members:
.. _implement_adapters:
Implementing an Adapter
^^^^^^^^^^^^^^^^^^^^^^^
Implementing an adapter for a Python web framework is pretty easy.
Do it by subclassing the :class:`.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 :class:`.WebObAdapter` or :class:`.WerkzeugAdapter` respectively.
.. autoclass:: BaseAdapter
:members:
"""

import abc
from authomatic.core import Response


class BaseAdapter(object):
"""
Base class for platform adapters
Defines common interface for WSGI framework specific functionality.
"""

__metaclass__ = abc.ABCMeta

@abc.abstractproperty
def params(self):
"""
Must return a :class:`dict` of all request parameters of any HTTP method.
:returns:
:class:`dict`
"""


@abc.abstractproperty
def url(self):
"""
Must return the url of the actual request including path but without query and fragment
:returns:
:class:`str`
"""


@abc.abstractproperty
def cookies(self):
"""
Must return cookies as a :class:`dict`.
:returns:
:class:`dict`
"""


@abc.abstractmethod
def write(self, value):
"""
Must write specified value to response.
:param str value:
String to be written to response.
"""


@abc.abstractmethod
def set_header(self, key, value):
"""
Must set response headers to ``Key: value``.
:param str key:
Header name.
:param str value:
Header value.
"""


@abc.abstractmethod
def set_status(self, status):
"""
Must set the response status e.g. ``'302 Found'``.
:param str status:
The HTTP response status.
"""


class DjangoAdapter(BaseAdapter):
"""
Adapter for the |django|_ framework.
"""

def __init__(self, request, response):
"""
:param request:
An instance of the :class:`django.http.HttpRequest` class.
:param response:
An instance of the :class:`django.http.HttpResponse` class.
"""
self.request = request
self.response = response

@property
def params(self):
return dict(self.request.REQUEST)

@property
def url(self):
return self.request.build_absolute_uri(self.request.path)

@property
def cookies(self):
return dict(self.request.COOKIES)

def write(self, value):
self.response.write(value)

def set_header(self, key, value):
self.response[key] = value

def set_status(self, status):
self.response.status_code = status


class WebObAdapter(BaseAdapter):
"""Adapter for the |webob|_ package."""

def __init__(self, request, response):
"""
:param request:
A |webob|_ :class:`Request` instance.
:param response:
A |webob|_ :class:`Response` instance.
"""
self.request = request
self.response = response


#===========================================================================
# Request
#===========================================================================

@property
def url(self):
return self.request.path_url


@property
def params(self):
return dict(self.request.params)


@property
def cookies(self):
return dict(self.request.cookies)


#===========================================================================
# Response
#===========================================================================

def write(self, value):
self.response.write(value)


def set_header(self, key, value):
self.response.headers[key] = str(value)


def set_status(self, status):
self.response.status = status


class Webapp2Adapter(WebObAdapter):
"""
Adapter for the |webapp2|_ framework.
Inherits from the :class:`.WebObAdapter`.
"""

def __init__(self, handler):
"""
:param handler:
A :class:`webapp2.RequestHandler` instance.
"""
self.request = handler.request
self.response = handler.response


class WerkzeugAdapter(BaseAdapter):
"""
Adapter for |flask|_ and other |werkzeug|_ based frameworks.
Thanks to `Mark Steve Samson <http://marksteve.com>`_.
"""

@property
def params(self):
return self.request.args

@property
def url(self):
return self.request.base_url

@property
def cookies(self):
return self.request.cookies

def __init__(self, request, response):
"""
:param request:
Instance of the :class:`werkzeug.wrappers.Request` class.
:param response:
Instance of the :class:`werkzeug.wrappers.Response` class.
"""

self.request = request
self.response = response

def write(self, value):
self.response.data += value

def set_header(self, key, value):
self.response.headers[key] = value

def set_status(self, status):
self.response.status = status
Loading

0 comments on commit 976e2ca

Please sign in to comment.