
    bAj                        d Z ddlmZmZmZ ddlmZmZ ddlmZm	Z	 ddl
mZ ddlmZ  edgd	      Z e       Zd
ededefdZdedefdZ	 	 ddededee   deeeef      def
dZdededefdZdedeeef   fdZy)z6Security utilities: JWT, password hashing, encryption.    )datetime	timedeltatimezone)AnyOptional)JWTErrorjwt)CryptContext)get_settingsbcryptauto)schemes
deprecatedplain_passwordhashed_passwordreturnc                 .    t         j                  | |      S N)pwd_contextverify)r   r   s     ;C:\xampp\htdocs\tradingbinance\backend\app\core\security.pyverify_passwordr      s    no>>    passwordc                 ,    t         j                  |       S r   )r   hash)r   s    r   get_password_hashr      s    H%%r   Nsubjectroleexpires_deltaextra_claimsc                    t        j                  t        j                        |xs t	        t
        j                        z   }| ||dd}|r|j                  |       t        j                  |t
        j                  d      S )N)minutesaccesssubr   exptypeHS256	algorithm)r   nowr   utcr   settingsaccess_token_expire_minutesupdater	   encode
secret_key)r   r   r    r!   expirepayloads         r   create_access_tokenr5      sm     \\(,,'P8+O+OPF 	G |$::gx22gFFr   c                     t        j                  t        j                        t	        t
        j                        z   }| ||dd}t        j                  |t
        j                  d      S )N)daysrefreshr%   r)   r*   )
r   r,   r   r-   r   r.   refresh_token_expire_daysr	   r1   r2   )r   r   r3   r4   s       r   create_refresh_tokenr:   +   sR    \\(,,')9[9[*\\F	G ::gx22gFFr   tokenc                     	 t        j                  | t        j                  dg      S # t        $ r}t        d      |d }~ww xY w)Nr)   )
algorithmszInvalid token)r	   decoder.   r2   r   
ValueError)r;   es     r   decode_tokenrA   6   s?    1zz%!4!4'KK 1)q01s   &) 	A>A)NN)__doc__r   r   r   typingr   r   joser   r	   passlib.contextr
   app.core.configr   r   r.   strboolr   r   dictr5   r:   rA    r   r   <module>rK      s    < 2 2    ( (H:&A>?C ?# ?$ ?& & & *.-1	GG
G I&G 4S>*	G
 	G(G# GS GS G1 1S#X 1r   