-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
305 additions
and
36 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,3 @@ | ||
from enum import Enum | ||
|
||
from .google import GoogleAuth | ||
from .github import GithubAuth | ||
|
||
|
||
class OAuth2Provider(str, Enum): | ||
GOOGLE = "google" | ||
GITHUB = "github" | ||
from .ldap import LDAPAuth |
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,113 @@ | ||
import logging | ||
from ldap3 import Server, Connection, ALL | ||
from auth_server.config import Settings | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
settings = Settings() | ||
|
||
|
||
class LDAPAuth: | ||
name: str = 'ldap' | ||
|
||
def __init__(self, | ||
url: str, | ||
username: str, | ||
password: str, | ||
user_dn_format: str, | ||
email_attr: str = 'mail', | ||
use_ssl: bool = True, | ||
): | ||
self._username = username | ||
self._password = password | ||
self._url = url | ||
self._user_dn_format = user_dn_format | ||
self._use_ssl = use_ssl | ||
self._email_attr = email_attr | ||
self._conn = None | ||
|
||
async def signin(self): | ||
return self._signin() | ||
|
||
def _signin(self): | ||
server = Server(self._url, use_ssl=self._use_ssl, get_info=ALL) | ||
user_dn = self.get_user_dn() | ||
self._conn = Connection(server, user_dn, self._password) | ||
|
||
if not self._conn.bind(): | ||
# this may happen from multiple reasons: | ||
# 1. user simply provided wrong credentials | ||
# 2. auth_server configuration issues | ||
# 3. server side problem | ||
logger.info( | ||
f"LDAP conn.bind() returned falsy value: {self._conn}" | ||
) | ||
raise Exception("LDAP conn.bind() returned falsy value") | ||
|
||
return self._conn | ||
|
||
async def user_email(self) -> str | None: | ||
if self._conn is None: | ||
self._conn = await self.signin() | ||
|
||
return self._user_email() | ||
|
||
def _user_email(self) -> str | None: | ||
user_dn = self.get_user_dn() | ||
search_filter = f'(uid={self._username})' | ||
attributes = [ | ||
'uid', self._email_attr | ||
] | ||
msg = "User entry not found:" \ | ||
f"user_dn: {user_dn} " \ | ||
f"search filter: {search_filter}" \ | ||
f"attributes: {attributes}" | ||
if self._conn.search( | ||
user_dn, | ||
search_filter, | ||
attributes=attributes | ||
): | ||
if len(self._conn.entries) == 0: | ||
logger.info(msg) | ||
return None | ||
|
||
result = self._conn.entries[0] | ||
|
||
if not result: | ||
logger.info("conn.search returned an empty entry") | ||
logger.info(msg) | ||
return None | ||
|
||
if result[self._email_attr] is None: | ||
logger.info("con.search empty email attr") | ||
logger.info(msg) | ||
return None | ||
|
||
return result[self._email_attr].value | ||
else: | ||
logger.info("conn.search returned falsy value") | ||
logger.info(msg) | ||
|
||
return None | ||
|
||
def get_user_dn(self) -> str: | ||
return self._user_dn_format.format(username=self._username) | ||
|
||
|
||
def get_client(username: str, password: str) -> LDAPAuth: | ||
return LDAPAuth( | ||
url=settings.papermerge__auth__ldap_url, | ||
username=username, | ||
password=password, | ||
user_dn_format=settings.papermerge__auth__ldap_user_dn_format, | ||
email_attr=settings.papermerge__auth__ldap_email_attr, | ||
use_ssl=settings.papermerge__auth__ldap_use_ssl | ||
) | ||
|
||
|
||
def get_default_email(username: str) -> str: | ||
domain = settings.papermerge__auth__ldap_user_email_domain_fallback | ||
return f"{username}@{domain}" | ||
|
||
|
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,43 @@ | ||
import typer | ||
from rich.console import Console | ||
from typing_extensions import Annotated | ||
from auth_server.backends import ldap | ||
|
||
|
||
app = typer.Typer() | ||
console = Console() | ||
|
||
Password = Annotated[ | ||
str, | ||
typer.Option( | ||
prompt=True, | ||
confirmation_prompt=False, | ||
hide_input=True | ||
) | ||
] | ||
|
||
|
||
@app.command() | ||
def auth(username: str, password: Password): | ||
"""Authenticates user with credentials""" | ||
client = ldap.get_client(username, password) | ||
try: | ||
client._signin() | ||
console.print("Authentication success", style="green") | ||
except Exception: | ||
console.print("Authentication failed", style="red") | ||
|
||
|
||
@app.command() | ||
def user_email(username: str, password: Password): | ||
"""Prints user email as retrieved from LDAP""" | ||
client = ldap.get_client(username, password) | ||
try: | ||
client._signin() | ||
console.print(f"User email: {client._user_email()}") | ||
except Exception: | ||
console.print("Authentication error", style="red") | ||
|
||
|
||
if __name__ == '__main__': | ||
app() |
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
Oops, something went wrong.