forked from MirrorHub/synapse
Merge pull request #701 from DoubleMalt/ldap-auth
Add LDAP authentication
This commit is contained in:
commit
f942980c0b
4 changed files with 134 additions and 10 deletions
|
@ -57,3 +57,6 @@ Florent Violleau <floviolleau at gmail dot com>
|
||||||
|
|
||||||
Niklas Riekenbrauck <nikriek at gmail dot.com>
|
Niklas Riekenbrauck <nikriek at gmail dot.com>
|
||||||
* Add JWT support for registration and login
|
* Add JWT support for registration and login
|
||||||
|
|
||||||
|
Christoph Witzany <christoph at web.crofting.com>
|
||||||
|
* Add LDAP support for authentication
|
||||||
|
|
|
@ -30,13 +30,14 @@ from .saml2 import SAML2Config
|
||||||
from .cas import CasConfig
|
from .cas import CasConfig
|
||||||
from .password import PasswordConfig
|
from .password import PasswordConfig
|
||||||
from .jwt import JWTConfig
|
from .jwt import JWTConfig
|
||||||
|
from .ldap import LDAPConfig
|
||||||
|
|
||||||
|
|
||||||
class HomeServerConfig(TlsConfig, ServerConfig, DatabaseConfig, LoggingConfig,
|
class HomeServerConfig(TlsConfig, ServerConfig, DatabaseConfig, LoggingConfig,
|
||||||
RatelimitConfig, ContentRepositoryConfig, CaptchaConfig,
|
RatelimitConfig, ContentRepositoryConfig, CaptchaConfig,
|
||||||
VoipConfig, RegistrationConfig, MetricsConfig, ApiConfig,
|
VoipConfig, RegistrationConfig, MetricsConfig, ApiConfig,
|
||||||
AppServiceConfig, KeyConfig, SAML2Config, CasConfig,
|
AppServiceConfig, KeyConfig, SAML2Config, CasConfig,
|
||||||
JWTConfig, PasswordConfig,):
|
JWTConfig, LDAPConfig, PasswordConfig,):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
52
synapse/config/ldap.py
Normal file
52
synapse/config/ldap.py
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Copyright 2015 Niklas Riekenbrauck
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
from ._base import Config
|
||||||
|
|
||||||
|
|
||||||
|
class LDAPConfig(Config):
|
||||||
|
def read_config(self, config):
|
||||||
|
ldap_config = config.get("ldap_config", None)
|
||||||
|
if ldap_config:
|
||||||
|
self.ldap_enabled = ldap_config.get("enabled", False)
|
||||||
|
self.ldap_server = ldap_config["server"]
|
||||||
|
self.ldap_port = ldap_config["port"]
|
||||||
|
self.ldap_tls = ldap_config.get("tls", False)
|
||||||
|
self.ldap_search_base = ldap_config["search_base"]
|
||||||
|
self.ldap_search_property = ldap_config["search_property"]
|
||||||
|
self.ldap_email_property = ldap_config["email_property"]
|
||||||
|
self.ldap_full_name_property = ldap_config["full_name_property"]
|
||||||
|
else:
|
||||||
|
self.ldap_enabled = False
|
||||||
|
self.ldap_server = None
|
||||||
|
self.ldap_port = None
|
||||||
|
self.ldap_tls = False
|
||||||
|
self.ldap_search_base = None
|
||||||
|
self.ldap_search_property = None
|
||||||
|
self.ldap_email_property = None
|
||||||
|
self.ldap_full_name_property = None
|
||||||
|
|
||||||
|
def default_config(self, **kwargs):
|
||||||
|
return """\
|
||||||
|
# ldap_config:
|
||||||
|
# enabled: true
|
||||||
|
# server: "ldap://localhost"
|
||||||
|
# port: 389
|
||||||
|
# tls: false
|
||||||
|
# search_base: "ou=Users,dc=example,dc=com"
|
||||||
|
# search_property: "cn"
|
||||||
|
# email_property: "email"
|
||||||
|
# full_name_property: "givenName"
|
||||||
|
"""
|
|
@ -49,6 +49,21 @@ class AuthHandler(BaseHandler):
|
||||||
self.sessions = {}
|
self.sessions = {}
|
||||||
self.INVALID_TOKEN_HTTP_STATUS = 401
|
self.INVALID_TOKEN_HTTP_STATUS = 401
|
||||||
|
|
||||||
|
self.ldap_enabled = hs.config.ldap_enabled
|
||||||
|
self.ldap_server = hs.config.ldap_server
|
||||||
|
self.ldap_port = hs.config.ldap_port
|
||||||
|
self.ldap_tls = hs.config.ldap_tls
|
||||||
|
self.ldap_search_base = hs.config.ldap_search_base
|
||||||
|
self.ldap_search_property = hs.config.ldap_search_property
|
||||||
|
self.ldap_email_property = hs.config.ldap_email_property
|
||||||
|
self.ldap_full_name_property = hs.config.ldap_full_name_property
|
||||||
|
|
||||||
|
if self.ldap_enabled is True:
|
||||||
|
import ldap
|
||||||
|
logger.info("Import ldap version: %s", ldap.__version__)
|
||||||
|
|
||||||
|
self.hs = hs # FIXME better possibility to access registrationHandler later?
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def check_auth(self, flows, clientdict, clientip):
|
def check_auth(self, flows, clientdict, clientip):
|
||||||
"""
|
"""
|
||||||
|
@ -215,8 +230,10 @@ class AuthHandler(BaseHandler):
|
||||||
if not user_id.startswith('@'):
|
if not user_id.startswith('@'):
|
||||||
user_id = UserID.create(user_id, self.hs.hostname).to_string()
|
user_id = UserID.create(user_id, self.hs.hostname).to_string()
|
||||||
|
|
||||||
user_id, password_hash = yield self._find_user_id_and_pwd_hash(user_id)
|
if not (yield self._check_password(user_id, password)):
|
||||||
self._check_password(user_id, password, password_hash)
|
logger.warn("Failed password login for user %s", user_id)
|
||||||
|
raise LoginError(403, "", errcode=Codes.FORBIDDEN)
|
||||||
|
|
||||||
defer.returnValue(user_id)
|
defer.returnValue(user_id)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
|
@ -340,8 +357,10 @@ class AuthHandler(BaseHandler):
|
||||||
StoreError if there was a problem storing the token.
|
StoreError if there was a problem storing the token.
|
||||||
LoginError if there was an authentication problem.
|
LoginError if there was an authentication problem.
|
||||||
"""
|
"""
|
||||||
user_id, password_hash = yield self._find_user_id_and_pwd_hash(user_id)
|
|
||||||
self._check_password(user_id, password, password_hash)
|
if not (yield self._check_password(user_id, password)):
|
||||||
|
logger.warn("Failed password login for user %s", user_id)
|
||||||
|
raise LoginError(403, "", errcode=Codes.FORBIDDEN)
|
||||||
|
|
||||||
logger.info("Logging in user %s", user_id)
|
logger.info("Logging in user %s", user_id)
|
||||||
access_token = yield self.issue_access_token(user_id)
|
access_token = yield self.issue_access_token(user_id)
|
||||||
|
@ -407,11 +426,60 @@ class AuthHandler(BaseHandler):
|
||||||
else:
|
else:
|
||||||
defer.returnValue(user_infos.popitem())
|
defer.returnValue(user_infos.popitem())
|
||||||
|
|
||||||
def _check_password(self, user_id, password, stored_hash):
|
@defer.inlineCallbacks
|
||||||
"""Checks that user_id has passed password, raises LoginError if not."""
|
def _check_password(self, user_id, password):
|
||||||
if not self.validate_hash(password, stored_hash):
|
defer.returnValue(
|
||||||
logger.warn("Failed password login for user %s", user_id)
|
not (
|
||||||
raise LoginError(403, "", errcode=Codes.FORBIDDEN)
|
(yield self._check_ldap_password(user_id, password))
|
||||||
|
or
|
||||||
|
(yield self._check_local_password(user_id, password))
|
||||||
|
))
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
|
def _check_local_password(self, user_id, password):
|
||||||
|
try:
|
||||||
|
user_id, password_hash = yield self._find_user_id_and_pwd_hash(user_id)
|
||||||
|
defer.returnValue(not self.validate_hash(password, password_hash))
|
||||||
|
except LoginError:
|
||||||
|
defer.returnValue(False)
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
|
def _check_ldap_password(self, user_id, password):
|
||||||
|
if self.ldap_enabled is not True:
|
||||||
|
logger.debug("LDAP not configured")
|
||||||
|
defer.returnValue(False)
|
||||||
|
|
||||||
|
import ldap
|
||||||
|
|
||||||
|
logger.info("Authenticating %s with LDAP" % user_id)
|
||||||
|
try:
|
||||||
|
ldap_url = "%s:%s" % (self.ldap_server, self.ldap_port)
|
||||||
|
logger.debug("Connecting LDAP server at %s" % ldap_url)
|
||||||
|
l = ldap.initialize(ldap_url)
|
||||||
|
if self.ldap_tls:
|
||||||
|
logger.debug("Initiating TLS")
|
||||||
|
self._connection.start_tls_s()
|
||||||
|
|
||||||
|
local_name = UserID.from_string(user_id).localpart
|
||||||
|
|
||||||
|
dn = "%s=%s, %s" % (
|
||||||
|
self.ldap_search_property,
|
||||||
|
local_name,
|
||||||
|
self.ldap_search_base)
|
||||||
|
logger.debug("DN for LDAP authentication: %s" % dn)
|
||||||
|
|
||||||
|
l.simple_bind_s(dn.encode('utf-8'), password.encode('utf-8'))
|
||||||
|
|
||||||
|
if not (yield self.does_user_exist(user_id)):
|
||||||
|
handler = self.hs.get_handlers().registration_handler
|
||||||
|
user_id, access_token = (
|
||||||
|
yield handler.register(localpart=local_name)
|
||||||
|
)
|
||||||
|
|
||||||
|
defer.returnValue(True)
|
||||||
|
except ldap.LDAPError, e:
|
||||||
|
logger.warn("LDAP error: %s", e)
|
||||||
|
defer.returnValue(False)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def issue_access_token(self, user_id):
|
def issue_access_token(self, user_id):
|
||||||
|
|
Loading…
Reference in a new issue