Files
spencermandClaude Fable 5.1 97b0973130 Phase 1: identity, sessions, audit, frontend shell
AD sign-in over LDAPS (search with the service account, bind as the user,
nested groups through LDAP_MATCHING_RULE_IN_CHAIN, disabled accounts refused),
roles as the union of PS-* groups mapped by a table derived from the seeded
departments. Server-side sessions with idle, lock and absolute timeouts, Origin
and X-Requested-With check, step-up unlock. Optional Argon2id PIN set with the
domain password; PIN sign-in by employee ID on registered devices only; five
wrong attempts disable the PIN, three wrong at unlock or signature end the
session. Rate limits per account and per address before any bind. Devices,
signatures table and sign helper, audit rows for every identity event, SSE over
LISTEN/NOTIFY with presence. Account settings and Admin APIs and screens
(AD view, devices, PIN clearing, group map with missing-group flags, refresh
from AD now). CLI ad-groups --list and ad-refresh. Migration 0002.

Frontend: real sign-in (password tab, PIN keypad on registered devices), lock
screen, role switcher, Account settings, Admin shell, screen placeholders.

CI: Samba AD DC container (PSTEST.LOCAL) with a generated CA, 49 of the 50
groups plus one extra, 17 test users; wired into the CI compose overlay and
the Gitea workflow; Playwright sign-in scenario.

Verified on docker-1: 171 server tests green inside the CI stack against
PostgreSQL and the Samba DC, 48 frontend tests, 8 Playwright scenarios against
the live stack (password and PIN sign-in, lock and unlock, Admin group map).
Findings recorded in the docs: the DC needs CAP_SYS_ADMIN, a hidden tab locks
only after a minute as the old build did,
and the app uses the alias ps-app for Playwright because "app" is on
Chromium's HSTS preload list.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-10-16 07:40:29 -06:00

88 lines
3.3 KiB
Python

"""LDAP helpers and the fake directory; no domain controller needed."""
from __future__ import annotations
import uuid
from pathlib import Path
import pytest
from fakedir import FAKE_PASSWORD, FakeDirectory
from pshelpers import make_settings
from packetstation.auth import groups
from packetstation.auth.ldap import Directory, LdapUnavailable, guid_filter, parse_guid, user_from_entry
GUID = uuid.UUID("01234567-89ab-cdef-0123-456789abcdef")
def test_parse_guid_bytes_and_string() -> None:
raw = GUID.bytes_le
assert parse_guid(raw) == GUID
assert parse_guid([raw]) == GUID
assert parse_guid("{01234567-89ab-cdef-0123-456789abcdef}") == GUID
assert parse_guid("01234567-89AB-CDEF-0123-456789ABCDEF") == GUID
with pytest.raises(ValueError):
parse_guid(b"short")
def test_guid_filter_escapes_every_byte() -> None:
flt = guid_filter(GUID)
assert flt.startswith("(objectGUID=\\67\\45\\23\\01") and flt.endswith(")")
assert flt.count("\\") == 16
def test_user_from_entry() -> None:
entry = {
"dn": "CN=Andy,DC=x",
"raw_attributes": {"objectGUID": [GUID.bytes_le]},
"attributes": {
"sAMAccountName": ["weld.andy"],
"userPrincipalName": ["[email protected]"],
"displayName": ["Andy Welder"],
"employeeID": ["0303"],
"mail": [],
"userAccountControl": ["514"],
"distinguishedName": ["CN=Andy,DC=x"],
},
}
u = user_from_entry(entry)
assert u.guid == GUID and u.sam_account == "weld.andy" and u.display_name == "Andy Welder"
assert u.emp_no == "0303" # text, leading zero kept
assert u.email is None and u.disabled is True and u.dn == "CN=Andy,DC=x"
def test_plain_ldap_refused_without_starttls(tmp_path: Path) -> None:
pw = tmp_path / "pw"
pw.write_text("x")
s = make_settings(tmp_path, ldap_url="ldap://dc.example:389", ldap_bind_dn="CN=svc", ldap_password_file=pw)
with pytest.raises(LdapUnavailable, match="plain LDAP"):
Directory(s).find_user("someone")
def test_empty_password_never_binds(tmp_path: Path) -> None:
s = make_settings(tmp_path, ldap_url="ldaps://dc.invalid:636")
assert Directory(s).verify_password("CN=x,DC=y", "") is False
def test_missing_service_secret_is_unavailable(tmp_path: Path) -> None:
s = make_settings(
tmp_path, ldap_url="ldaps://dc.invalid:636", ldap_bind_dn="CN=svc", ldap_password_file=tmp_path / "none"
)
with pytest.raises(LdapUnavailable, match="LDAP_PASSWORD_FILE"):
Directory(s).list_groups()
def test_fake_directory_nested_and_ou() -> None:
d = FakeDirectory()
ned = d.find_user("NESTED.NED")
assert ned is not None
assert groups.roles_from_groups(d.groups_of(ned.dn)) == ["shop:10:worker"]
bo = d.find_user("[email protected]")
assert bo is not None
assert groups.roles_from_groups(d.groups_of(bo.dn)) == ["shop:03:supervisor"]
assert "PS-14-Electrical-Lead" not in d.list_groups() and "PS-Extra-Legacy" in d.list_groups()
sams = {u.sam_account for u in d.members_of("PS-03-WeldParts-Worker")}
assert {"weld.andy", "both.bo", "dup.dave", "disabled.dan"} <= sams
assert d.verify_password(bo.dn, FAKE_PASSWORD) and not d.verify_password(bo.dn, "nope")
assert d.verify_password(bo.dn, "") is False