Skip to content

Commit d77c0f9

Browse files
author
Austin Morton
committed
Initial Commit
0 parents  commit d77c0f9

File tree

10 files changed

+213
-0
lines changed

10 files changed

+213
-0
lines changed

.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
*.py[cod]
2+
3+
# C extensions
4+
*.so
5+
6+
# Packages
7+
*.egg
8+
*.egg-info
9+
dist
10+
build
11+
eggs
12+
parts
13+
bin
14+
var
15+
sdist
16+
develop-eggs
17+
.installed.cfg
18+
lib
19+
lib64
20+
__pycache__
21+
22+
# Installer logs
23+
pip-log.txt
24+
25+
# Unit test / coverage reports
26+
.coverage
27+
.tox
28+
nosetests.xml
29+
30+
# Translations
31+
*.mo
32+
33+
# Mr Developer
34+
.mr.developer.cfg
35+
.project
36+
.pydevproject

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
language: pythonpython: - "2.7" - "2.6"install: - pip install -r requirements/tests.txt - pip install -e .script: - flake8 src - python setup.py nosetests

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
Django Remote Auth LDAP
2+
=========================
3+
4+
[![Build Status](https://travis-ci.org/Juvenal1228/django-remote-auth-ldap.png?branch=master)](https://travis-ci.org/Juvenal1228/django-remote-auth-ldap)
5+
6+
Purpose
7+
-------
8+
9+
This app combines [django-auth-ldap](http://pythonhosted.org/django-auth-ldap/) with django's [RemoteUserBackend](https://docs.djangoproject.com/en/dev/howto/auth-remote-user/)
10+
It allows django applications hosted in IIS to take advantage of Windows Authentication in IIS (401 Challenge) while also having the advanced features offered in `django-auth-ldap`
11+
12+
13+
Features
14+
--------
15+
16+
- [PEP 8](http://www.python.org/dev/peps/pep-0008/) compliance
17+
- [semver](http://semver.org/) compliance
18+
19+
20+
Installing
21+
----------
22+
23+
Install with pip/easy_install from the pypi
24+
25+
`pip install django-remote-auth-ldap`
26+
27+
or clone the latest source
28+
29+
git clone https://github.com/Juvenal1228/django-remote-auth-ldap.git
30+
cd django-remote-auth-ldap
31+
python setup.py install
32+
33+
34+
Using
35+
-----
36+
37+
In your django settings.py file configure django-auth-ldap normally, verify that the configuration is indeed working!
38+
39+
Add the `RemoteUserMiddleware` class after the `AuthenticationMiddleware` class
40+
```python
41+
MIDDLEWARE_CLASSES = (
42+
...
43+
'django.contrib.auth.middleware.AuthenticationMiddleware',
44+
'django_remote_auth_ldap.middleware.RemoteUserMiddleware',
45+
...
46+
)
47+
```
48+
49+
Set the RemoteUserLDAPBackend as the authentication backend
50+
```python
51+
AUTHENTICATION_BACKENDS = (
52+
'django_remote_auth_ldap.backend.RemoteUserLDAPBackend',
53+
)
54+
```
55+
56+
The application expects the remote user to be in the form `domain\user` (which is how IIS returns it)
57+
58+
Settings
59+
--------
60+
61+
There are a few settings you can use to control the behavior
62+
63+
- `DRAL_CHECK_DOMAIN` - Boolean - whether or not to check the domain against a known list - default True
64+
- `DRAL_STRIP_DOMAIN` - Boolean - whether or not to strip the domain off the username before passing to django-auth-ldap - default True
65+
- `DRAL_DOMAINS` - List - list of domains to check against, should be lowercase! - default []
66+
- `DRAL_HEADER` - String - header to check for remote user in - default REMOTE_USER
67+
68+

requirements/tests.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
nose
2+
coverage
3+
flake8
4+
Django

setup.cfg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[nosetests]
2+
with-coverage=1
3+
cover-package=django_remote_auth_ldap
4+
5+
[flake8]
6+
max-complexity=10
7+
show-source=1
8+
statistics=1

setup.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from setuptools import setup, find_packages
2+
import os
3+
4+
here = os.path.abspath(os.path.dirname(__file__))
5+
README = open(os.path.join(here, 'README.md')).read()
6+
7+
8+
version = '0.1.0'
9+
10+
install_requires = [
11+
'django-auth-ldap',
12+
'django-appconf'
13+
]
14+
15+
setup(
16+
name='django-remote-auth-ldap',
17+
version=version,
18+
description="REMOTE_USER authentication using LDAP",
19+
long_description=README,
20+
classifiers=[
21+
'Framework :: Django',
22+
'Intended Audience :: Developers',
23+
'License :: OSI Approved :: MIT License',
24+
'Programming Language :: Python',
25+
'Programming Language :: Python :: 2.6',
26+
'Programming Language :: Python :: 2.7',
27+
'Topic :: Internet :: WWW/HTTP',
28+
],
29+
keywords='',
30+
author='Austin Morton',
31+
author_email='[email protected]',
32+
url='https://github.com/Juvenal1228/django-remote-auth-ldap',
33+
license='MIT',
34+
packages=find_packages('src'),
35+
package_dir={'': 'src'},
36+
zip_safe=False,
37+
install_requires=install_requires,
38+
test_suite='nose.collector'
39+
)

src/django_remote_auth_ldap/__init__.py

Whitespace-only changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from django_auth_ldap.backend import LDAPBackend, _LDAPUser
2+
from django_remote_auth_ldap.conf import settings
3+
4+
5+
class RemoteUserLDAPBackend(LDAPBackend):
6+
def authenticate(self, remote_user):
7+
if not self.correct_domain(remote_user):
8+
return None
9+
10+
username = self.clean_username(remote_user)
11+
ldap_user = RemoteLDAPUser(self, username=username)
12+
user = ldap_user.authenticate('')
13+
14+
return user
15+
16+
def correct_domain(self, username):
17+
if not settings.CHECK_DOMAIN:
18+
return True
19+
if not '\\' in username:
20+
return False
21+
(dom, username) = username.split('\\', 1)
22+
return dom.lower() in settings.DOMAINS
23+
24+
def clean_username(self, username):
25+
if not settings.STRIP_DOMAIN:
26+
return username
27+
if not '\\' in username:
28+
return username
29+
(dom, username) = username.split('\\', 1)
30+
return username
31+
32+
33+
class RemoteLDAPUser(_LDAPUser):
34+
def _authenticate_user_dn(self, password):
35+
if self.dn is None:
36+
msg = "Failed to map the username to a DN."
37+
raise self.AuthenticationFailed(msg)

src/django_remote_auth_ldap/conf.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from django.conf import settings # noqa
2+
from appconf import AppConf
3+
4+
5+
class DjangoRemoteAuthLdapAppConf(AppConf):
6+
CHECK_DOMAIN = True
7+
STRIP_DOMAIN = True
8+
DOMAINS = []
9+
HEADER = 'REMOTE_USER'
10+
11+
class Meta:
12+
prefix = 'DRAL'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.contrib.auth import middleware
2+
from django_remote_auth_ldap.conf import settings
3+
4+
5+
class RemoteUserMiddleware(middleware.RemoteUserMiddleware):
6+
@property
7+
def HEADER(self):
8+
return settings.HEADER

0 commit comments

Comments
 (0)