o
    i#                  
   @   s  d Z ddlZddlZejZG dd deZdd Ze	edZ
e	edZejZejfe_e
jZeejfe_eje_e
jZejeejfe_ee_e
jZeejfe_ee_e
jZejejfe_ee_e
jZejejejejfe_ee_e
jZejejejejejeejeejeejfe_ee_e
j Z!ejejejejejejejeejfe!_ee!_G d	d
 d
e"Z#dS )zLImplements a macOS specific TokenCache, and provides auxiliary helper types.    Nc                       s0   e Zd ZdZdZdZdZdZ fddZ  Z	S )KeychainErrorzRThe RuntimeError that will be run when a function interacting with Keychain fails.ii2i%i,c                    s&   t t|   || _d| j| _d S )NzW{} see https://opensource.apple.com/source/CarbonHeaders/CarbonHeaders-18.1/MacErrors.h)superr   __init__exit_statusformatmessage)selfr   	__class__ J/home/app/Keep/.python/lib/python3.10/site-packages/msal_extensions/osx.pyr      s
   zKeychainError.__init__)
__name__
__module____qualname____doc__ACCESS_DENIEDZNO_SUCH_KEYCHAIN
NO_DEFAULTITEM_NOT_FOUNDr   __classcell__r   r   r	   r   r      s    r   c                 C   s
   d | S )z
    Fetches the location of a native MacOS library.
    :param name: The name of the library to be loaded.
    :return: The location of the library on a MacOS filesystem.
    z,/System/Library/Frameworks/{0}.framework/{0})r   )namer   r   r   _get_native_location   s   
r   ZSecurityZCoreFoundationc                   @   sJ   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )Keychainz?Encapsulates the interactions with a particular MacOS Keychain.Nc                 C   s4   t  | _|rtj|}|d| _d S d | _d S )Nutf-8)_ctypesc_void_p_refospath
expanduserencode	_filename)r   filenamer   r   r   r      s
   

zKeychain.__init__c                 C   s0   | j rt| j | j}nt| j}|rt|| S N)r    _SECURITY_KEYCHAIN_OPENr   _SECURITY_KEYCHAIN_COPY_DEFAULTOSError)r   statusr   r   r   	__enter__   s   
zKeychain.__enter__c                 G   s   | j r
t| j  d S d S r"   )r   _CORE_RELEASE)r   argsr   r   r   __exit__   s   zKeychain.__exit__c              	   C   s   | d}| d}t }t }t| jt||t||||d}|r)t|dt|j	}t
||j	|j	 td| |jdS )aG  Fetch the password associated with a particular service and account.

        :param service: The service that this password is associated with.
        :param account_name: The account that this password is associated with.
        :return: The value of the password associated with the specified service and account.
        r   Nr   )r   r   c_uint32r   (_SECURITY_KEYCHAIN_FIND_GENERIC_PASSWORDr   lenr   Zcreate_string_buffervalueZmemmove$_SECURITY_KEYCHAIN_ITEM_FREE_CONTENTrawdecode)r   serviceaccount_namelengthcontentsr   r/   r   r   r   get_generic_password   s(   



zKeychain.get_generic_passwordc              	   C   s   | d}| d}| d}t }t| jt||t||dd|}|s7t|dt||}|r5t|ddS |tjkrWt	| jt||t||t||d}|rUt|ddS t|d)a  Associate a password with a given service and account.

        :param service: The service to associate this password with.
        :param account_name: The account to associate this password with.
        :param value: The string that should be used as the password.
        r   Nr+   )
r   r   r   r-   r   r.   2_SECURITY_KEYCHAIN_ITEM_MODIFY_ATTRIBUTES_AND_DATAr   r   '_SECURITY_KEYCHAIN_ADD_GENERIC_PASSWORD)r   r3   r4   r/   entryZfind_exit_statusZmodify_exit_statusZadd_exit_statusr   r   r   set_generic_password   sN   






zKeychain.set_generic_passwordc                 C      t  )ac   Fetches a password associated with a domain and username.
        NOTE: THIS IS NOT YET IMPLEMENTED
        :param service: The website/service that this password is associated with.
        :param username: The account that this password is associated with.
        :return: The password that was associated with the given service and username.
        NotImplementedError)r   r3   usernamer   r   r   get_internet_password      zKeychain.get_internet_passwordc                 C   r<   )al  Sets a password associated with a domain and a username.
        NOTE: THIS IS NOT YET IMPLEMENTED
        :param service: The website/service that this password is associated with.
        :param username: The account that this password is associated with.
        :param value: The password that should be associated with the given service and username.
        r=   )r   r3   r?   r/   r   r   r   set_internet_password   rA   zKeychain.set_internet_passwordr"   )r   r   r   r   r   r'   r*   r7   r;   r@   rB   r   r   r   r   r   }   s    


 3
r   )$r   r   ctypesr   Zc_int32Z	OS_RESULTr%   r   r   CDLLZ	_SECURITYZ_COREZ	CFReleaser(   r   argtypesZSecCopyErrorMessageStringZ#_SECURITY_COPY_ERROR_MESSAGE_STRINGc_char_prestypeZSecKeychainOpenr#   ZPOINTERZSecKeychainCopyDefaultr$   ZSecKeychainItemFreeContentr0   Z&SecKeychainItemModifyAttributesAndDatar8   r,   ZSecKeychainFindGenericPasswordr-   ZSecKeychainAddGenericPasswordr9   objectr   r   r   r   r   <module>   sz   







