Ë
    ¥0›h×  ã                  ó†   — d Z ddlmZ ddlZddlmZ ddlmZ dd„Zdd„Z	ddddd	œ	 	 	 	 	 dd
„Z
ddddœ	 	 	 	 	 dd„Zdd„Zy)aæ  
audit.py

Purpose:
  Centralized audit logging for authentication events and administrative
  actions. This module provides small, focused helpers to record security-
  relevant events into the SQLite database, so operators have traceability for
  incident response.

How it works:
  - Lazily ensures minimal schemas for two tables: `auth_log` and
    `admin_audit`.
  - Exposes `log_auth_event(...)` and `log_admin_action(...)` with simple
    parameters to record events. Timestamps are stored as UNIX epoch seconds.
  - Keeps the code path lightweight; failures to log never break requests.

Data model:
  - auth_log(id, ts, ip, user_id, username, event, detail)
  - admin_audit(id, ts, ip, admin_user_id, target_user_id, action, detail)
é    )ÚannotationsN)ÚOptionalé   )Úget_dbc                 ó<   — t        t        j                  «       «      S )N)ÚintÚtime© ó    úDC:\Users\algun\Documents\ceba web\Ceba - Github\app_modules\audit.pyÚ_nowr      s   € ÜŒty‰y‹{ÓÐr   c                 ó|   — t        «       } | j                  d«       | j                  d«       | j                  «        y )Na  
        CREATE TABLE IF NOT EXISTS auth_log (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            ts INTEGER NOT NULL,
            ip TEXT,
            user_id INTEGER,
            username TEXT,
            event TEXT NOT NULL,
            detail TEXT
        )
        a6  
        CREATE TABLE IF NOT EXISTS admin_audit (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            ts INTEGER NOT NULL,
            ip TEXT,
            admin_user_id INTEGER NOT NULL,
            target_user_id INTEGER,
            action TEXT NOT NULL,
            detail TEXT
        )
        )r   ÚexecuteÚcommit)Údbs    r   Ú_ensure_schemar   "   s8   € Ü	‹€BØ‡JJð
	ôð ‡JJð
	ôð ‡II…Kr   )ÚusernameÚuser_idÚipÚdetailc          	     ó¬   — 	 t        «        t        «       }|j                  dt        «       |||| |f«       |j	                  «        y # t
        $ r Y y w xY w)NzYINSERT INTO auth_log (ts, ip, user_id, username, event, detail) VALUES (?, ?, ?, ?, ?, ?)©r   r   r   r   r   Ú	Exception)Úeventr   r   r   r   r   s         r   Úlog_auth_eventr   A   sR   € ð
ÜÔÜ‹XˆØ

‰
ØgÜ‹VR˜ (¨E°6Ð:ô	
ð 		‰	øÜò áðúó   ‚AA Á	AÁA)Útarget_user_idr   r   c          	     ó¬   — 	 t        «        t        «       }|j                  dt        «       |||| |f«       |j	                  «        y # t
        $ r Y y w xY w)NziINSERT INTO admin_audit (ts, ip, admin_user_id, target_user_id, action, detail) VALUES (?, ?, ?, ?, ?, ?)r   )ÚactionÚadmin_user_idr   r   r   r   s         r   Úlog_admin_actionr!   P   sR   € ð	ÜÔÜ‹XˆØ

‰
ØwÜ‹VR˜¨¸ÀÐGô	
ð 		‰	øÜò Ùðúr   c                óÀ   — 	 | j                   j                  d«      xs dj                  d«      d   j                  «       }|xs | j                  S # t
        $ r Y y w xY w)NzX-Forwarded-ForÚ Ú,r   )ÚheadersÚgetÚsplitÚstripÚremote_addrr   )ÚrequestÚfwds     r   Úget_request_ipr,   ^   s]   € ðØ‰×"Ñ"Ð#4Ó5Ò;¸×BÑBÀ3ÓGÈÑJ×PÑPÓRˆØÒ)g×)Ñ)Ð)øÜò Ùðús   ‚AA Á	AÁA)Úreturnr   )r-   ÚNone)r   Ústrr   úOptional[str]r   úOptional[int]r   r0   r   r0   r-   r.   )r   r/   r    r   r   r1   r   r0   r   r0   r-   r.   )r-   r0   )Ú__doc__Ú
__future__r   r	   Útypingr   r   r   r   r   r   r!   r,   r
   r   r   Ú<module>r5      s{   ðñõ* #ã Ý å óóð> =AÐ[_Ø'+ÀTñØ$ðØ5BðØNRóð Z^Ø)-ÀtñØ&ðØ7DðØPTóôr   