-
-
Notifications
You must be signed in to change notification settings - Fork 591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added authorization via LDAP #1329
Open
svesh
wants to merge
1
commit into
abhinavsingh:develop
Choose a base branch
from
svesh:ldap-auth
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
proxy.py | ||
~~~~~~~~ | ||
⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on | ||
Network monitoring, controls & Application development, testing, debugging. | ||
|
||
:copyright: (c) 2013-present by Abhinav Singh and contributors. | ||
:license: BSD, see LICENSE for more details. | ||
|
||
.. spelling:: | ||
|
||
auth | ||
http | ||
ldap | ||
""" | ||
import base64 | ||
from time import time | ||
from typing import Dict, Optional | ||
|
||
import ldap | ||
|
||
from ..http import httpHeaders | ||
from ..http.proxy import HttpProxyBasePlugin | ||
from ..common.flag import flags | ||
from ..http.parser import HttpParser | ||
from ..http.exception import ProxyAuthenticationFailed | ||
|
||
|
||
DEFAULT_LDAP_SERVER = 'ldap://ldap.example.org' | ||
DEFAULT_LDAP_ROOT_DN = 'uid=Manager,ou=People,dc=example,dc=com' | ||
DEFAULT_LDAP_ROOT_PW = 'SecretPassword' | ||
DEFAULT_LDAP_BASE_DN = 'ou=People,dc=example,dc=com' | ||
DEFAULT_LDAP_USER_SEARCH = '(&(uid={user})(accountStatus=active))' | ||
DEFAULT_LDAP_AUTH_TIMEOUT = 3600 | ||
|
||
flags.add_argument( | ||
'--ldap-server', | ||
type=str, | ||
default=DEFAULT_LDAP_SERVER, | ||
help='Default: ' + DEFAULT_LDAP_SERVER + | ||
'. LDAP server address.', | ||
) | ||
|
||
flags.add_argument( | ||
'--ldap-root-dn', | ||
type=str, | ||
default=DEFAULT_LDAP_ROOT_DN, | ||
help='Default: ' + DEFAULT_LDAP_ROOT_DN + | ||
'. LDAP root dn.', | ||
) | ||
|
||
flags.add_argument( | ||
'--ldap-root-pw', | ||
type=str, | ||
default=DEFAULT_LDAP_ROOT_PW, | ||
help='Default: ' + DEFAULT_LDAP_ROOT_PW + | ||
'. LDAP root password.', | ||
) | ||
|
||
flags.add_argument( | ||
'--ldap-base-dn', | ||
type=str, | ||
default=DEFAULT_LDAP_BASE_DN, | ||
help='Default: ' + DEFAULT_LDAP_BASE_DN + | ||
'. LDAP users base DN.', | ||
) | ||
|
||
flags.add_argument( | ||
'--ldap-user-search', | ||
type=str, | ||
default=DEFAULT_LDAP_USER_SEARCH, | ||
help='Default: ' + DEFAULT_LDAP_USER_SEARCH + | ||
'. LDAP user search filter.', | ||
) | ||
|
||
flags.add_argument( | ||
'--ldap-auth-timeout', | ||
type=int, | ||
default=DEFAULT_LDAP_AUTH_TIMEOUT, | ||
help='Default: ' + str(DEFAULT_LDAP_AUTH_TIMEOUT) + | ||
'. LDAP user auth timeout.', | ||
) | ||
|
||
|
||
class LDAPAuthPlugin(HttpProxyBasePlugin): | ||
"""Performs proxy authentication through LDAP.""" | ||
|
||
__auth_pass__: Dict[bytes, float] = {} | ||
|
||
def auth_user(self, user: str, password: str) -> bool: | ||
ldap_connection = ldap.initialize(self.flags.ldap_server) | ||
ldap_connection.bind_s(self.flags.ldap_root_dn, self.flags.ldap_root_pw) | ||
search_filter = self.flags.ldap_user_search.format(user=user) | ||
search_result = ldap_connection.search_s(self.flags.ldap_base_dn, ldap.SCOPE_SUBTREE, search_filter, ['uid']) | ||
if len(search_result) != 1 and len(search_result[0]) != 2: | ||
return False | ||
try: | ||
ldap_connection.bind_s(search_result[0][0], password) | ||
except ldap.LDAPError: | ||
return False | ||
return True | ||
|
||
def before_upstream_connection( | ||
self, request: HttpParser, | ||
) -> Optional[HttpParser]: | ||
if not request.headers or httpHeaders.PROXY_AUTHORIZATION not in request.headers: | ||
raise ProxyAuthenticationFailed() | ||
parts = request.headers[httpHeaders.PROXY_AUTHORIZATION][1].split() | ||
if len(parts) != 2 or parts[0].lower() != b'basic': | ||
raise ProxyAuthenticationFailed() | ||
elif self.__auth_pass__.get(parts[1], 0) > time(): | ||
return request | ||
elif self.__auth_pass__.get(parts[1], 0) < time(): | ||
userpass = base64.b64decode(parts[1]).decode().split(':') | ||
user = userpass[0] | ||
password = userpass[-1] | ||
if self.auth_user(user, password): | ||
self.__auth_pass__[parts[1]] = time() + self.flags.ldap_auth_timeout | ||
return request | ||
raise ProxyAuthenticationFailed() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
setuptools-scm == 6.3.2 | ||
twine==3.8.0 | ||
python-ldap==3.4.3 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auth_ldap
is just an example plugin, it's upto user to install the dependency if they choose to use this pluginOtherwise, now to install
proxy.py
, users will also need to installpython-ldap
even when not using the plugin.