Ë
    á„ýj
  ã                   ó\   — d dl Z d dlZd dlZd dlmZmZ 	 ddeeef   dededee	ef   fd„Z
y)	é    N)ÚAnyÚTupleÚ	auth_dataÚ	bot_tokenÚmax_age_secondsÚreturnc                 ó&  — |sy| j                  d«      }|rt        |t        «      sy| j                  d«      }|€y	 t        |«      }t        t        j                  «       «      }||dz   kD  ry||z
  |kD  rd	d
||z
  › d|› d�fS g }t        | j                  «       «      D ]#  \  }}	|dk(  s|	€Œ|j                  |› d|	› �«       Œ% dj                  |«      }
t        j                  |j                  d«      «      j                  «       }t        j                   ||
j                  d«      t        j                  «      j#                  «       }t        j$                  |j'                  «       |j'                  «       «      syy# t        t
        f$ r Y yw xY w)aí  
    Verify authentication data received from Telegram Login Widget.
    
    Telegram verification algorithm:
    1. Check `auth_date` is fresh (< max_age_seconds) and not in the future.
    2. Build data_check_string by sorting all key=value pairs alphabetically, excluding 'hash'.
    3. Generate secret_key = SHA256(bot_token.encode()).digest()
    4. Calculate HMAC-SHA256(data_check_string, secret_key).hexdigest()
    5. Compare with received `hash` using constant-time comparison.
    )Fz3Telegram bot token is not configured on the server.Úhash)Fz9Missing or invalid 'hash' in Telegram authorization data.Ú	auth_date)Fz3Missing 'auth_date' in Telegram authorization data.)FzInvalid 'auth_date' format.éx   )Fz*Authentication timestamp is in the future.Fz.Telegram authorization data has expired (age: zs > zs).ú=ú
zutf-8)Fz:Cryptographic signature verification failed: invalid hash.)Tz-Telegram authorization verified successfully.)ÚgetÚ
isinstanceÚstrÚintÚ
ValueErrorÚ	TypeErrorÚtimeÚsortedÚitemsÚappendÚjoinÚhashlibÚsha256ÚencodeÚdigestÚhmacÚnewÚ	hexdigestÚcompare_digestÚlower)r   r   r   Úreceived_hashÚauth_date_rawr   ÚnowÚcheck_pairsÚkeyÚvalÚdata_check_stringÚ
secret_keyÚexpected_hashs                ú9/root/telegram-saas/modules/identity/telegram_verifier.pyÚverify_telegram_authorizationr-      s›  € ñ ØKà—M‘M &Ó)€MÙ¤
¨=¼#Ô >ØQð —M‘M +Ó.€MØÐØKð4Ü˜Ó&ˆ	ô Œd�i‰i‹kÓ
€Cà�3˜‘9ÒØBð 	ˆi‰˜?Ò*ØÐFÀsÈYÁÐFWÐW[Ð\kÐ[lÐloÐpÐpÐpð €KÜ˜9Ÿ?™?Ó,Ó-ò +‰ˆˆSØ�&Š=˜C˜KØØ×Ñ˜c˜U ! C 5˜>Õ*ð+ð
 Ÿ	™	 +Ó.Ðô —‘ 	× 0Ñ 0°Ó 9Ó:×AÑAÓC€Jô —H‘HØØ× Ñ  Ó)Ü�‰ó÷ �iƒkð	 ô ×Ñ˜}×2Ñ2Ó4°m×6IÑ6IÓ6KÔLØRà@øôI œ	Ð"ò 4Ù3ð4ús   ½E> Å>FÆF)i€Q )r   r   r   Útypingr   r   Údictr   r   Úboolr-   © ó    r,   ú<module>r3      sZ   ðÛ Û Û ß ð !ñAAØ�C˜�H‰~ðAAàðAAð ðAAð ˆ4�ˆ9Ñô	AAr2   