
    Xj                     n    d dl mZ d dlmZ d dlmZ  ee          Z G d d          Z e            Z	dS )    )Fernet)settings)
get_loggerc                   J    e Zd Zd Zd ZdedefdZdedefdZdedefd	Z	d
S )EncryptionServicec                 <    d | _         |                                  d S )N)_fernet_initialize)selfs    ;/lsinfo/ai/hellotax_ai/base_platform/app/core/encryption.py__init__zEncryptionService.__init__   s!    &*    c                    t           j        st                              d           d S 	 t           j        }t	          |t
                    r|                                }t          |          | _        t          	                    d           d S # t          $ r2}t                              d|            t          d          |d }~ww xY w)NzCENCRYPTION_KEY not configured, API keys will be stored in plaintextz+Encryption service initialized successfullyz!Failed to initialize encryption: zInvalid ENCRYPTION_KEY)r   ENCRYPTION_KEYloggerwarning
isinstancestrencoder   r	   info	Exceptionerror
ValueError)r   keyes      r   r
   zEncryptionService._initialize   s    & 	NN`aaaF	>)C#s## #jjll!#;;DLKKEFFFFF 	> 	> 	>LL@Q@@AAA566A=	>s   A#B 
C-CC	plaintextreturnc                 f   |s|S | j         st                              d           |S 	 | j                             |                                          }t                              d           |                                S # t          $ r#}t                              d|             d }~ww xY w)Nz,Encryption not configured, storing plaintextzData encrypted successfullyzEncryption failed: )	r	   r   r   encryptr   r   decoder   r   )r   r   	encryptedr   s       r   r   zEncryptionService.encrypt   s     	| 	NNIJJJ	,,Y-=-=-?-?@@IKK5666##%%% 	 	 	LL2q22333	s   AB 
B0B++B0
ciphertextc                 r   |s|S | j         st                              d           |S 	 | j                             |                                          }t                              d           |                                S # t          $ r)}t                              d|            |cY d }~S d }~ww xY w)Nz0Encryption not configured, returning value as-iszData decrypted successfullyzDecryption failed: )r	   r   r   decryptr   r   r    r   )r   r"   	decryptedr   s       r   r$   zEncryptionService.decrypt,   s     	| 	NNMNNN	,,Z->->-@-@AAIKK5666##%%% 	 	 	NN444555	s   AB 
B6B1+B61B6valuec                     |r| j         sdS 	 | j                             |                                           dS # t          $ r Y dS w xY w)NFT)r	   r$   r   r   )r   r&   s     r   is_encryptedzEncryptionService.is_encrypted:   sb     	DL 	5	L  0004 	 	 	55	s   ,; 
A	A	N)
__name__
__module____qualname__r   r
   r   r   r$   boolr(    r   r   r   r   
   s          > > >     # #    # $      r   r   N)
cryptography.fernetr   
app.configr   common_loggingr   r)   r   r   encryption_servicer-   r   r   <module>r2      s    & & & & & &       % % % % % %	H		7 7 7 7 7 7 7 7t '&((   r   