o
    eOj3
                     @   s  U d dl Z d dlmZ d dlmZmZmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d d	lmZ e ZeeZeed
< dZdZdedefddZdefddZeddefddZ e!dededfdede"defddZ#eddd Z$dS )    N)defaultdict)	APIRouterRequestForm)RedirectResponseJSONResponse)create_access_tokenrender)settings)AsyncSessionFactory)get_admin_by_telegram_id_login_attempts   i  ipreturnc                    s6   t    t|  } fdd|D t| < tt|  tk S )z9Returns True if request is allowed, False if rate limitedc                    s   g | ]
} | t k r|qS  )LOCKOUT_SECONDS).0tnowr   ?/var/www/www-root/data/www/ai-bot.pdev.uz/admin/routers/auth.py
<listcomp>   s    z%_check_rate_limit.<locals>.<listcomp>)timer   lenMAX_ATTEMPTS)r   attemptsr   r   r   _check_rate_limit   s   r   c                 C   s   t |  t  d S )N)r   appendr   )r   r   r   r   _record_failed_attempt   s   r    z/loginrequestc                    s   t | dS )N
login.htmlr	   )r!   r   r   r   
login_page"   s   
r#   .telegram_idpasswordc              	      s  | j j}t|st| dddS |tjkr t| t| dddS t 4 I d H }t||I d H }W d   I d H  n1 I d H s@w   Y  |tj	v }|sY|sYt| t| dddS |r]dn|j
j}|rg|jjnd| }t|||tj}	tdd	d
}
|
jd|	dddtjd |
S )Nr"   z;Juda ko'p urinish. 15 daqiqadan so'ng qayta urinib ko'ring.)errorzNoto'g'ri parolzSiz admin emassizsuper_adminzSuperAdmin #z
/dashboard.  urlstatus_codeadmin_tokenTlaxiQ )keyvaluehttponlysamesitemax_agesecure)clienthostr   r
   r   admin_web_passwordr    r   r   super_admin_listroler/   user	full_namer   admin_secret_keyr   
set_cookieadmin_cookie_secure)r!   r$   r%   	client_ipsessionadminis_superr8   nametokenresponser   r   r   login'   s>   
(
rE   z/logoutc                     s   t ddd} | d | S )Nz/auth/loginr(   r)   r,   )r   delete_cookie)rD   r   r   r   logoutR   s   
rG   )%r   collectionsr   fastapir   r   r   fastapi.responsesr   r   
admin.authr   admin.responsesr
   
bot.configr   bot.database.connectionr   bot.database.crud.adminr   routerlistr   dict__annotations__r   r   strboolr   r    getr#   postintrE   rG   r   r   r   r   <module>   s:   
 	*