Flask-LDAPConn


NameFlask-LDAPConn JSON
Version 0.10.1 PyPI version JSON
download
home_pagehttp://github.com/rroemhild/flask-ldapconn
SummaryPure python, LDAP connection and ORM for Flask Applications
upload_time2019-12-23 18:04:41
maintainer
docs_urlNone
authorRafael Römhild
requires_python
licenseBSD
keywords flask ldap ldap3 orm
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            Flask-LDAPConn
==============

.. image:: https://travis-ci.org/rroemhild/flask-ldapconn.svg?branch=master
    :target: https://travis-ci.org/rroemhild/flask-ldapconn

.. image:: https://badge.fury.io/py/Flask-LDAPConn.svg
    :target: https://badge.fury.io/py/Flask-LDAPConn

Flask-LDAPConn is a Flask extension providing `ldap3 <https://github.com/cannatag/ldap3>`_ (an LDAP V3 pure Python client) connection for accessing LDAP servers.

To abstract access to LDAP data this extension provides a simple ORM model.


Installation
------------

.. code-block:: shell

    pip install flask-ldapconn


Configuration
-------------

Your configuration should be declared within your Flask config. Sample configuration:

.. code-block:: python

    import ssl

    LDAP_SERVER = 'localhost'
    LDAP_PORT = 389
    LDAP_BINDDN = 'cn=admin,dc=example,dc=com'
    LDAP_SECRET = 'forty-two'
    LDAP_CONNECT_TIMEOUT = 10  # Honored when the TCP connection is being established
    LDAP_USE_TLS = True  # default
    LDAP_REQUIRE_CERT = ssl.CERT_NONE  # default: CERT_REQUIRED
    LDAP_TLS_VERSION = ssl.PROTOCOL_TLSv1_2  # default: PROTOCOL_TLSv1
    LDAP_CERT_PATH = '/etc/openldap/certs'

If you want to always get any entry attribute value as a list, instead of a string if only one item is in the attribute list, then set:

.. code-block:: python

    FORCE_ATTRIBUTE_VALUE_AS_LIST = True

Default is ``False`` and will return a string if only one item is in the attribute list.


Setup
-----

Create the LDAP instance in your application.

.. code-block:: python

    from flask import Flask
    from flask_ldapconn import LDAPConn

    app = Flask(__name__)
    ldap = LDAPConn(app)


Client sample
-------------

.. code-block:: python

    from flask import Flask
    from flask_ldapconn import LDAPConn
    from ldap3 import SUBTREE

    app = Flask(__name__)
    ldap = LDAPConn(app)

    @app.route('/')
    def index():
        ldapc = ldap.connection
        basedn = 'ou=people,dc=example,dc=com'
        search_filter = '(objectClass=posixAccount)'
        attributes = ['sn', 'givenName', 'uid', 'mail']
        ldapc.search(basedn, search_filter, SUBTREE,
                     attributes=attributes)
        response = ldapc.response


User model samples
------------------

.. code-block:: python

    from flask import Flask
    from flask_ldapconn import LDAPConn

    app = Flask(__name__)
    ldap = LDAPConn(app)

    class User(ldap.Entry):

        base_dn = 'ou=people,dc=example,dc=com'
        object_classes = ['inetOrgPerson']

        name = ldap.Attribute('cn')
        email = ldap.Attribute('mail')
        userid = ldap.Attribute('uid')
        surname = ldap.Attribute('sn')
        givenname = ldap.Attribute('givenName')

    with app.app_context():

        # get a list of entries
        entries = User.query.filter('email: *@example.com').all()
        for entry in entries:
            print u'Name: {}'.format(entry.name)

        # get the first entry
        user = User.query.filter('userid: user1').first()

        # new entry
        new_user = User(
            name='User Three',
            email='user3@example.com',
            userid='user3',
            surname='Three',
            givenname='User'
        )
        new_user.save()

        # modify entry
        mod_user = User.query.filter('userid: user1').first()
        mod_user.name = 'User Number Three'
        mod_user.email.append.('u.three@example.com')
        mod_user.givenname.delete()
        mod_user.save()

        # remove entry
        rm_user = User.query.filter('userid: user1').first()
        rm_user.delete()

        # authenticate user
        auth_user = User.query.filter('userid: user1').first()
        if auth_user:
            if auth_user.authenticate('password1234'):
                print('Authenticated')
            else:
                print('Wrong password')


Authenticate with Client
------------------------

.. code-block:: python

    from flask import Flask
    from flask_ldapconn import LDAPConn

    app = Flask(__name__)
    ldap = LDAPConn(app)

    username = 'user1'
    password = 'userpass'
    attribute = 'uid'
    search_filter = ('(active=1)')

    with app.app_context():
        retval = ldap.authenticate(username, password, attribute,
                                   basedn, search_filter)
        if not retval:
            return 'Invalid credentials.'
        return 'Welcome %s.' % username


Bind as user
------------

To bind as user for the current request instance a new connection from ``flask.g.ldap_conn``:

.. code-block:: python

    g.ldap_conn = ldap.connect(userdn, password)
    user = User.query.get(userdn)


Unit Test
---------

I use a simple Docker image to run the tests on localhost. The test file ``test_flask_ldapconn.py`` tries to handle ``start`` and ``stop`` of the docker container:

.. code-block:: shell

    pip install docker-py
    docker pull rroemhild/test-openldap
    python test_flask_ldapconn.py

Run the docker container manual:

.. code-block:: shell

    docker run --privileged -d -p 389:389 --name flask_ldapconn rroemhild/test-openldap
    DOCKER_RUN=False python test_flask_ldapconn.py

Unit test with your own settings from a file:

.. code-block:: shell

    LDAP_SETTINGS=my_settings.py python test_flask_ldapconn.py


Contribute
----------

#. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.
#. Fork `the repository`_ on Github to start making your changes.
#. Write a test which shows that the bug was fixed or that the feature works as expected.
#. Send a pull request and bug the maintainer until it gets merged and published.

.. _`the repository`: http://github.com/rroemhild/flask-ldapconn



            

Raw data

            {
    "_id": null,
    "home_page": "http://github.com/rroemhild/flask-ldapconn",
    "name": "Flask-LDAPConn",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "flask ldap ldap3 orm",
    "author": "Rafael R\u00f6mhild",
    "author_email": "rafael@roemhild.de",
    "download_url": "https://files.pythonhosted.org/packages/c3/4e/5a357ffdcbe9f1545ad498cabb3d02af9035810732b2a208b04d654da765/Flask-LDAPConn-0.10.1.tar.gz",
    "platform": "any",
    "description": "Flask-LDAPConn\n==============\n\n.. image:: https://travis-ci.org/rroemhild/flask-ldapconn.svg?branch=master\n    :target: https://travis-ci.org/rroemhild/flask-ldapconn\n\n.. image:: https://badge.fury.io/py/Flask-LDAPConn.svg\n    :target: https://badge.fury.io/py/Flask-LDAPConn\n\nFlask-LDAPConn is a Flask extension providing `ldap3 <https://github.com/cannatag/ldap3>`_ (an LDAP V3 pure Python client) connection for accessing LDAP servers.\n\nTo abstract access to LDAP data this extension provides a simple ORM model.\n\n\nInstallation\n------------\n\n.. code-block:: shell\n\n    pip install flask-ldapconn\n\n\nConfiguration\n-------------\n\nYour configuration should be declared within your Flask config. Sample configuration:\n\n.. code-block:: python\n\n    import ssl\n\n    LDAP_SERVER = 'localhost'\n    LDAP_PORT = 389\n    LDAP_BINDDN = 'cn=admin,dc=example,dc=com'\n    LDAP_SECRET = 'forty-two'\n    LDAP_CONNECT_TIMEOUT = 10  # Honored when the TCP connection is being established\n    LDAP_USE_TLS = True  # default\n    LDAP_REQUIRE_CERT = ssl.CERT_NONE  # default: CERT_REQUIRED\n    LDAP_TLS_VERSION = ssl.PROTOCOL_TLSv1_2  # default: PROTOCOL_TLSv1\n    LDAP_CERT_PATH = '/etc/openldap/certs'\n\nIf you want to always get any entry attribute value as a list, instead of a string if only one item is in the attribute list, then set:\n\n.. code-block:: python\n\n    FORCE_ATTRIBUTE_VALUE_AS_LIST = True\n\nDefault is ``False`` and will return a string if only one item is in the attribute list.\n\n\nSetup\n-----\n\nCreate the LDAP instance in your application.\n\n.. code-block:: python\n\n    from flask import Flask\n    from flask_ldapconn import LDAPConn\n\n    app = Flask(__name__)\n    ldap = LDAPConn(app)\n\n\nClient sample\n-------------\n\n.. code-block:: python\n\n    from flask import Flask\n    from flask_ldapconn import LDAPConn\n    from ldap3 import SUBTREE\n\n    app = Flask(__name__)\n    ldap = LDAPConn(app)\n\n    @app.route('/')\n    def index():\n        ldapc = ldap.connection\n        basedn = 'ou=people,dc=example,dc=com'\n        search_filter = '(objectClass=posixAccount)'\n        attributes = ['sn', 'givenName', 'uid', 'mail']\n        ldapc.search(basedn, search_filter, SUBTREE,\n                     attributes=attributes)\n        response = ldapc.response\n\n\nUser model samples\n------------------\n\n.. code-block:: python\n\n    from flask import Flask\n    from flask_ldapconn import LDAPConn\n\n    app = Flask(__name__)\n    ldap = LDAPConn(app)\n\n    class User(ldap.Entry):\n\n        base_dn = 'ou=people,dc=example,dc=com'\n        object_classes = ['inetOrgPerson']\n\n        name = ldap.Attribute('cn')\n        email = ldap.Attribute('mail')\n        userid = ldap.Attribute('uid')\n        surname = ldap.Attribute('sn')\n        givenname = ldap.Attribute('givenName')\n\n    with app.app_context():\n\n        # get a list of entries\n        entries = User.query.filter('email: *@example.com').all()\n        for entry in entries:\n            print u'Name: {}'.format(entry.name)\n\n        # get the first entry\n        user = User.query.filter('userid: user1').first()\n\n        # new entry\n        new_user = User(\n            name='User Three',\n            email='user3@example.com',\n            userid='user3',\n            surname='Three',\n            givenname='User'\n        )\n        new_user.save()\n\n        # modify entry\n        mod_user = User.query.filter('userid: user1').first()\n        mod_user.name = 'User Number Three'\n        mod_user.email.append.('u.three@example.com')\n        mod_user.givenname.delete()\n        mod_user.save()\n\n        # remove entry\n        rm_user = User.query.filter('userid: user1').first()\n        rm_user.delete()\n\n        # authenticate user\n        auth_user = User.query.filter('userid: user1').first()\n        if auth_user:\n            if auth_user.authenticate('password1234'):\n                print('Authenticated')\n            else:\n                print('Wrong password')\n\n\nAuthenticate with Client\n------------------------\n\n.. code-block:: python\n\n    from flask import Flask\n    from flask_ldapconn import LDAPConn\n\n    app = Flask(__name__)\n    ldap = LDAPConn(app)\n\n    username = 'user1'\n    password = 'userpass'\n    attribute = 'uid'\n    search_filter = ('(active=1)')\n\n    with app.app_context():\n        retval = ldap.authenticate(username, password, attribute,\n                                   basedn, search_filter)\n        if not retval:\n            return 'Invalid credentials.'\n        return 'Welcome %s.' % username\n\n\nBind as user\n------------\n\nTo bind as user for the current request instance a new connection from ``flask.g.ldap_conn``:\n\n.. code-block:: python\n\n    g.ldap_conn = ldap.connect(userdn, password)\n    user = User.query.get(userdn)\n\n\nUnit Test\n---------\n\nI use a simple Docker image to run the tests on localhost. The test file ``test_flask_ldapconn.py`` tries to handle ``start`` and ``stop`` of the docker container:\n\n.. code-block:: shell\n\n    pip install docker-py\n    docker pull rroemhild/test-openldap\n    python test_flask_ldapconn.py\n\nRun the docker container manual:\n\n.. code-block:: shell\n\n    docker run --privileged -d -p 389:389 --name flask_ldapconn rroemhild/test-openldap\n    DOCKER_RUN=False python test_flask_ldapconn.py\n\nUnit test with your own settings from a file:\n\n.. code-block:: shell\n\n    LDAP_SETTINGS=my_settings.py python test_flask_ldapconn.py\n\n\nContribute\n----------\n\n#. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.\n#. Fork `the repository`_ on Github to start making your changes.\n#. Write a test which shows that the bug was fixed or that the feature works as expected.\n#. Send a pull request and bug the maintainer until it gets merged and published.\n\n.. _`the repository`: http://github.com/rroemhild/flask-ldapconn\n\n\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "Pure python, LDAP connection and ORM for Flask Applications",
    "version": "0.10.1",
    "split_keywords": [
        "flask",
        "ldap",
        "ldap3",
        "orm"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "ae4aa4ebf2017c50df8642f192765267",
                "sha256": "82d457d9fd7ba2b800449f9f57673c92286e12a6e5a0bb7eb76f468fa0182546"
            },
            "downloads": -1,
            "filename": "Flask_LDAPConn-0.10.1-py3-none-any.whl",
            "has_sig": true,
            "md5_digest": "ae4aa4ebf2017c50df8642f192765267",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 10436,
            "upload_time": "2019-12-23T18:04:34",
            "upload_time_iso_8601": "2019-12-23T18:04:34.850998Z",
            "url": "https://files.pythonhosted.org/packages/b9/60/ad47d61c967eec0eb8cac704f7c3f26fc7a5beef7c6021326e140818d089/Flask_LDAPConn-0.10.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "f3c94c88d95c1e3b4282221980da29dc",
                "sha256": "82fdb95537bb2c7218c4208cfe3a01654b4a7a224861fb9d6a5ef268d8e7e3e2"
            },
            "downloads": -1,
            "filename": "Flask-LDAPConn-0.10.1.tar.gz",
            "has_sig": true,
            "md5_digest": "f3c94c88d95c1e3b4282221980da29dc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 10510,
            "upload_time": "2019-12-23T18:04:41",
            "upload_time_iso_8601": "2019-12-23T18:04:41.606516Z",
            "url": "https://files.pythonhosted.org/packages/c3/4e/5a357ffdcbe9f1545ad498cabb3d02af9035810732b2a208b04d654da765/Flask-LDAPConn-0.10.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2019-12-23 18:04:41",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "rroemhild",
    "github_project": "flask-ldapconn",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "flask-ldapconn"
}
        
Elapsed time: 0.01562s