Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit e805b12

Browse files
author
Daniel Berteaud
committed
Add LDAP auth support
1 parent cd43bc1 commit e805b12

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

docs/setup.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,13 @@ writing. Windows is not and will never be supported.
281281
* ``libpq-dev`` for PostgreSQL
282282
* ``libmagic-dev`` for mime type detection
283283
* ``mime-support`` for mime type detection
284+
* ``libldap2-dev`` for LDAP auth support
284285

285286
Use this list for your preferred package management:
286287

287288
.. code::
288289
289-
python3 python3-pip python3-dev imagemagick fonts-liberation optipng gnupg libpq-dev libmagic-dev mime-support
290+
python3 python3-pip python3-dev imagemagick fonts-liberation optipng gnupg libpq-dev libmagic-dev mime-support libldap2-dev
290291
291292
These dependencies are required for OCRmyPDF, which is used for text recognition.
292293

paperless.conf.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@
3434
#PAPERLESS_COOKIE_PREFIX=
3535
#PAPERLESS_ENABLE_HTTP_REMOTE_USER=false
3636

37+
# LDAP Auth settings
38+
#PAPERLESS_ENABLE_LDAP_AUTH=True
39+
#PAPERLESS_LDAP_URI=ldap://ldap.example.com
40+
#PAPERLESS_LDAP_BIND_DN=CN=Paperless NG,OU=Apps,DC=domain,DC=com
41+
#PAPERLESS_LDAP_BIND_PASSWORD=p@ssw0rd
42+
#PAPERLESS_LDAP_USER_BASE=OU=People,DC=example,DC=com
43+
#PAPERLESS_LDAP_USER_FILTER=(sAMAccountName=%(user)s)
44+
#PAPERLESS_LDAP_FIRSTNAME_ATTR=givenName
45+
#PAPERLESS_LDAP_LASTNAME_ATTR=sn
46+
#PAPERLESS_LDAP_EMAIL_ATTR=mail
47+
3748
# OCR settings
3849

3950
#PAPERLESS_OCR_LANGUAGE=eng

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dateparser==1.0.0
3232
django-cors-headers==3.8.0
3333
django-extensions==3.1.3
3434
django-filter==2.4.0
35+
django-auth-ldap
3536
django-picklefield==3.0.1; python_version >= '3'
3637
django-q==1.3.9
3738
django==3.2.6

src/paperless/settings.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ def __get_boolean(key, default="NO"):
183183
# Security #
184184
###############################################################################
185185

186+
AUTHENTICATION_BACKENDS = []
187+
186188
AUTO_LOGIN_USERNAME = os.getenv("PAPERLESS_AUTO_LOGIN_USERNAME")
187189

188190
if AUTO_LOGIN_USERNAME:
@@ -198,14 +200,39 @@ def __get_boolean(key, default="NO"):
198200
MIDDLEWARE.append(
199201
'paperless.auth.HttpRemoteUserMiddleware'
200202
)
201-
AUTHENTICATION_BACKENDS = [
202-
'django.contrib.auth.backends.RemoteUserBackend',
203-
'django.contrib.auth.backends.ModelBackend'
204-
]
203+
AUTHENTICATION_BACKENDS.append(
204+
'django.contrib.auth.backends.RemoteUserBackend'
205+
)
205206
REST_FRAMEWORK['DEFAULT_AUTHENTICATION_CLASSES'].append(
206207
'rest_framework.authentication.RemoteUserAuthentication'
207208
)
208209

210+
ENABLE_LDAP_AUTH = __get_boolean("PAPERLESS_ENABLE_LDAP_AUTH")
211+
212+
if ENABLE_LDAP_AUTH:
213+
import ldap
214+
from django_auth_ldap.config import LDAPSearch
215+
AUTHENTICATION_BACKENDS.append(
216+
'django_auth_ldap.backend.LDAPBackend'
217+
)
218+
AUTH_LDAP_SERVER_URI = os.getenv("PAPERLESS_LDAP_URI", "ldap://localhost")
219+
AUTH_LDAP_BIND_DN = os.getenv("PAPERLESS_LDAP_BIND_DN", "")
220+
AUTH_LDAP_BIND_PASSWORD = os.getenv("PAPERLESS_LDAP_BIND_PASSWORD", "")
221+
AUTH_LDAP_USER_SEARCH = LDAPSearch(
222+
os.getenv("PAPERLESS_LDAP_USER_BASE", "ou=users,dc=example,dc=com"),
223+
ldap.SCOPE_SUBTREE, os.getenv("PAPERLESS_LDAP_USER_FILTER", "(uid=%(user)s)")
224+
)
225+
AUTH_LDAP_START_TLS = os.getenv("PAPERLESS_LDAP_START_TLS", True)
226+
AUTH_LDAP_USER_ATTR_MAP = {
227+
"first_name": os.getenv("PAPERLESS_LDAP_FIRSTNAME_ATTR", "givenName"),
228+
"last_name": os.getenv("PAPERLESS_LDAP_LASTNAME_ATTR", "sn"),
229+
"email": os.getenv("PAPERLESS_LDAP_EMAIL_ATTR", "mail")
230+
}
231+
232+
AUTHENTICATION_BACKENDS.append(
233+
'django.contrib.auth.backends.ModelBackend'
234+
)
235+
209236
# X-Frame options for embedded PDF display:
210237
if DEBUG:
211238
X_FRAME_OPTIONS = 'ANY'

0 commit comments

Comments
 (0)