codex/windows-native-runtime-compat #28
3 changed files with 101 additions and 6 deletions
|
|
@ -1,9 +1,12 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import io
|
||||
import os
|
||||
import secrets
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
|
||||
from PIL import Image, ImageDraw, ImageFont, ImageFilter
|
||||
from sqlalchemy.orm import Session
|
||||
|
|
@ -12,11 +15,44 @@ from adapters.db.models.captcha import Captcha
|
|||
from app.core.settings import get_settings
|
||||
from app.services.auth_security_event_service import log_auth_security_event
|
||||
from app.services.system_settings_service import get_captcha_auth_security_effective
|
||||
import hashlib
|
||||
|
||||
|
||||
|
||||
_ALPHANUM_CHARSET = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"
|
||||
|
||||
_API_ROOT = Path(__file__).resolve().parents[2]
|
||||
_BUNDLED_CAPTCHA_FONT = _API_ROOT / "assets" / "fonts" / "Vazirmatn-Bold.ttf"
|
||||
|
||||
|
||||
def _captcha_font_candidates() -> tuple[Path, ...]:
|
||||
candidates = [
|
||||
Path("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"),
|
||||
Path("/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf"),
|
||||
]
|
||||
|
||||
windows_dir = os.environ.get("WINDIR")
|
||||
if windows_dir:
|
||||
windows_fonts = Path(windows_dir) / "Fonts"
|
||||
candidates.extend(
|
||||
[
|
||||
windows_fonts / "arialbd.ttf",
|
||||
windows_fonts / "segoeuib.ttf",
|
||||
]
|
||||
)
|
||||
|
||||
candidates.append(_BUNDLED_CAPTCHA_FONT)
|
||||
return tuple(candidates)
|
||||
|
||||
|
||||
def _load_captcha_font(size: int):
|
||||
for font_path in _captcha_font_candidates():
|
||||
try:
|
||||
if font_path.is_file():
|
||||
return ImageFont.truetype(str(font_path), size)
|
||||
except (OSError, ValueError):
|
||||
continue
|
||||
|
||||
return ImageFont.load_default()
|
||||
|
||||
def _generate_numeric_code(length: int) -> str:
|
||||
return "".join(str(secrets.randbelow(10)) for _ in range(length))
|
||||
|
|
@ -46,11 +82,8 @@ def _render_image(code: str, strong: bool) -> Image.Image:
|
|||
bg2 = 242 + secrets.randbelow(8)
|
||||
img = Image.new("RGB", (width, height), (bg0, bg1, bg2))
|
||||
draw = ImageDraw.Draw(img)
|
||||
try:
|
||||
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
||||
font = ImageFont.truetype(font_path, 26)
|
||||
except Exception:
|
||||
font = ImageFont.load_default()
|
||||
font = _load_captcha_font(26)
|
||||
|
||||
for _ in range(8 if strong else 3):
|
||||
xy = [(secrets.randbelow(width), secrets.randbelow(height)) for _ in range(2)]
|
||||
fill = (180 + secrets.randbelow(40), 185 + secrets.randbelow(40), 192 + secrets.randbelow(40))
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ dependencies = [
|
|||
"simpleeval>=0.9.14",
|
||||
"croniter>=2.0.0",
|
||||
"pytz>=2024.1",
|
||||
"tzdata>=2025.1; sys_platform == 'win32'",
|
||||
"paramiko>=3.4.0",
|
||||
"qrcode>=7.4.2",
|
||||
"basalam-sdk>=0.1.0",
|
||||
|
|
|
|||
61
hesabixAPI/tests/test_captcha_font.py
Normal file
61
hesabixAPI/tests/test_captcha_font.py
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
from pathlib import Path
|
||||
|
||||
from app.services import captcha_service
|
||||
|
||||
|
||||
def test_captcha_font_candidates_keep_linux_font_first(
|
||||
monkeypatch,
|
||||
) -> None:
|
||||
monkeypatch.delenv("WINDIR", raising=False)
|
||||
|
||||
candidates = captcha_service._captcha_font_candidates()
|
||||
|
||||
assert candidates[0] == Path(
|
||||
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
||||
)
|
||||
assert candidates[-1] == captcha_service._BUNDLED_CAPTCHA_FONT
|
||||
|
||||
|
||||
def test_captcha_font_candidates_include_windows_fonts(
|
||||
monkeypatch,
|
||||
) -> None:
|
||||
windows_dir = r"C:\Windows"
|
||||
monkeypatch.setenv("WINDIR", windows_dir)
|
||||
|
||||
candidates = captcha_service._captcha_font_candidates()
|
||||
fonts_dir = Path(windows_dir) / "Fonts"
|
||||
|
||||
assert fonts_dir / "arialbd.ttf" in candidates
|
||||
assert fonts_dir / "segoeuib.ttf" in candidates
|
||||
|
||||
|
||||
def test_load_captcha_font_skips_missing_candidate(
|
||||
monkeypatch,
|
||||
tmp_path,
|
||||
) -> None:
|
||||
missing_font = tmp_path / "missing.ttf"
|
||||
available_font = tmp_path / "available.ttf"
|
||||
available_font.write_bytes(b"test-font")
|
||||
|
||||
expected_font = object()
|
||||
loaded: list[tuple[Path, int]] = []
|
||||
|
||||
def fake_truetype(path: str, size: int):
|
||||
loaded.append((Path(path), size))
|
||||
return expected_font
|
||||
|
||||
monkeypatch.setattr(
|
||||
captcha_service,
|
||||
"_captcha_font_candidates",
|
||||
lambda: (missing_font, available_font),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
captcha_service.ImageFont,
|
||||
"truetype",
|
||||
fake_truetype,
|
||||
)
|
||||
|
||||
font = captcha_service._load_captcha_font(26)
|
||||
|
||||
assert font is expected_font
|
||||
assert loaded == [(available_font, 26)]
|
||||
Loading…
Reference in a new issue