Skip to content

Commit

Permalink
Fix login
Browse files Browse the repository at this point in the history
  • Loading branch information
dnknth committed Oct 30, 2024
1 parent c89d7dc commit e2ea997
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
37 changes: 22 additions & 15 deletions backend/ldap_ui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import contextlib
import logging
import sys
from http import HTTPStatus
from typing import AsyncGenerator

import ldap
Expand Down Expand Up @@ -49,8 +50,8 @@

# Force authentication
UNAUTHORIZED = Response(
"Invalid credentials",
status_code=401,
HTTPStatus.UNAUTHORIZED.phrase,
status_code=HTTPStatus.UNAUTHORIZED.value,
headers={"WWW-Authenticate": 'Basic realm="Please log in", charset="UTF-8"'},
)

Expand All @@ -71,17 +72,20 @@ async def dispatch(

# Search for basic auth user
if type(request.user) is LdapUser:
password = request.user.password
dn = settings.GET_BIND_PATTERN(request.user.username)
if dn is None:
dn, _attrs = await unique(
connection,
connection.search(
settings.BASE_DN,
ldap.SCOPE_SUBTREE,
settings.GET_BIND_DN_FILTER(request.user.username),
),
)
password = request.user.password
try:
dn, _attrs = await unique(
connection,
connection.search(
settings.BASE_DN,
ldap.SCOPE_SUBTREE,
settings.GET_BIND_DN_FILTER(request.user.username),
),
)
except HTTPException:
pass

# Hard-wired credentials
if dn is None:
Expand All @@ -104,7 +108,7 @@ async def dispatch(
LOG.error(msg)
return PlainTextResponse(
msg,
status_code=500,
status_code=HTTPStatus.INTERNAL_SERVER_ERROR.value,
)


Expand Down Expand Up @@ -174,13 +178,16 @@ async def http_exception(_request: Request, exc: HTTPException) -> Response:

async def forbidden(_request: Request, exc: ldap.LDAPError) -> Response:
"HTTP 403 Forbidden"
return PlainTextResponse(ldap_exception_message(exc), status_code=403)
return PlainTextResponse(
ldap_exception_message(exc),
status_code=HTTPStatus.FORBIDDEN.value,
)


async def http_422(_request: Request, e: ValidationError) -> Response:
"HTTP 422 Unprocessable Entity"
LOG.warn("Invalid request body", exc_info=e)
return Response(repr(e), status_code=422)
return Response(repr(e), status_code=HTTPStatus.UNPROCESSABLE_ENTITY.value)


@contextlib.asynccontextmanager
Expand Down Expand Up @@ -215,7 +222,7 @@ async def lifespan(app):
Middleware(GZipMiddleware, minimum_size=512, compresslevel=6),
),
routes=[
Mount("/api", routes=api.routes),
Mount("/api", app=api),
Mount("/", StaticFiles(packages=["ldap_ui"], html=True)),
],
)
3 changes: 2 additions & 1 deletion backend/ldap_ui/ldap_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import base64
import io
from http import HTTPStatus
from typing import Any, Optional, Tuple, Union

import ldap
Expand Down Expand Up @@ -37,7 +38,7 @@
__all__ = ("api",)


NO_CONTENT = Response(status_code=204)
NO_CONTENT = Response(status_code=HTTPStatus.NO_CONTENT.value)

# Special fields
PHOTOS = ("jpegPhoto", "thumbnailPhoto")
Expand Down

0 comments on commit e2ea997

Please sign in to comment.