Skip to content

Commit

Permalink
Merge pull request #31 from PedroHenriqueDevBR/dev
Browse files Browse the repository at this point in the history
feat: verify expirated user password
  • Loading branch information
PedroHenriqueDevBR authored Jan 19, 2024
2 parents 3908ce5 + d7341bc commit 44ae37a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
30 changes: 30 additions & 0 deletions ldap_password/apps/core/services/ldap/search_user.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from typing import Optional
from datetime import datetime, timedelta

import ldap3
from django.conf import settings
Expand Down Expand Up @@ -50,6 +51,7 @@ def _execute_search(self, user: str) -> bool:
attributes=[
"uid",
"mail",
"accountExpires",
],
)

Expand Down Expand Up @@ -123,3 +125,31 @@ def search_mail_by_username(self, username: str) -> str:
return _("Can not search user data in LDAP Server")
except LDAPSocketOpenError:
return _("Can not connect to the LDAP server")

def verify_user_expided_password_by_username(self, username: str) -> str:
try:
response = self._search(username=username)
if response is None:
raise IndexError

attrs = response["attributes"]
if "accountExpires" in attrs:
now = datetime.now()
now_timestamp = now.timestamp()
account_expires = attrs["accountExpires"][0]
diference = timedelta(1)
expire_date = datetime.fromisoformat(account_expires)
expire_date = expire_date - diference # remove 1 day from date
expire_timestamp = expire_date.timestamp()
return str(now_timestamp >= expire_timestamp)

raise AttributeError
except AttributeError:
return _("Expires date not registered to user")
except IndexError:
msg = _("Not found")
return f"{username} {msg}!"
except ConnectionError:
return _("Can not search user data in LDAP Server")
except LDAPSocketOpenError:
return _("Can not connect to the LDAP server")
6 changes: 6 additions & 0 deletions ldap_password/apps/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from apps.core.views import (
index,
success,
ValidateExpiredPasswordView,
PasswordView,
RequestMailView,
ConfirmTokenView,
Expand All @@ -16,6 +17,11 @@
path("password", PasswordView.as_view(), name="password"),
path("request/mail", RequestMailView.as_view(), name="mail"),
path("request/token", ConfirmTokenView.as_view(), name="token"),
path(
"request/expired",
ValidateExpiredPasswordView.as_view(),
name="expired",
),
path(
"request/token/password",
ChangePasswordToken.as_view(),
Expand Down
19 changes: 19 additions & 0 deletions ldap_password/apps/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.conf import settings
from django.contrib import messages
from django.http import HttpRequest, QueryDict
from django.http import JsonResponse
from django.shortcuts import redirect, render
from django.utils.translation import gettext as _
from django.views import View
Expand All @@ -28,6 +29,24 @@ def success(request: HttpRequest):
return render(request, template_name, context)


class ValidateExpiredPasswordView(View):
def post(self, request: HttpRequest):
data = request.POST
if not data.get("username"):
return JsonResponse({"return": False}, safe=True, status=400)

username = data.get("username") or ""
ldap_search = SearchLDAPUser()
response = ldap_search.verify_user_expided_password_by_username(
username=username,
)

if response == "True":
return JsonResponse({"return": True}, safe=True, status=200)

return JsonResponse({"return": False}, status=401)


class PasswordView(View):
def get(self, request: HttpRequest):
enterprise_name = settings.ENTERPRISE_NAME
Expand Down

0 comments on commit 44ae37a

Please sign in to comment.