
    AHj#              
       |   d Z ddlZddlZej                  Z G d de      Zd Z ej                   ed            Z
 ej                   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j,                  ej                        fe_        ee_        e
j.                  Z ej,                  ej                        fe_        ee_        e
j2                  Zej                  ej                  fe_        ee_        e
j6                  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j,                  ej:                         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j:                  ej$                   ej,                  ej                        fe!_        ee!_         G d d	e"      Z#y)
zLImplements a macOS specific TokenCache, and provides auxiliary helper types.    Nc                   2     e Zd ZdZdZdZdZdZ fdZ xZ	S )KeychainErrorzRThe RuntimeError that will be run when a function interacting with Keychain fails.ii2i%i,c                 x    t         t        |           || _        dj	                  | j                        | _        y )NzW{} see https://opensource.apple.com/source/CarbonHeaders/CarbonHeaders-18.1/MacErrors.h)superr   __init__exit_statusformatmessage)selfr   	__class__s     K/root/tools/cai/cai_env/lib/python3.12/site-packages/msal_extensions/osx.pyr   zKeychainError.__init__   s5    mT+-& fVD$$% 	    )
__name__
__module____qualname____doc__ACCESS_DENIEDNO_SUCH_KEYCHAIN
NO_DEFAULTITEM_NOT_FOUNDr   __classcell__)r   s   @r   r   r      s%    \MJN' 'r   r   c                 $    dj                  |       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	   )names    r   _get_native_locationr      s     :@@FFr   SecurityCoreFoundationc                   <    e Zd ZdZd
dZd Zd Zd Zd Zd Z	d	 Z
y)Keychainz?Encapsulates the interactions with a particular MacOS Keychain.Nc                     t        j                         | _        |r6t        j                  j                  |      }|j                  d      | _        y d | _        y )Nutf-8)_ctypesc_void_p_refospath
expanduserencode	_filename)r   filenames     r   r   zKeychain.__init__   s@    $$&	ww))(3H%__W5DN!DNr   c                     | j                   r!t        | j                   | j                        }nt        | j                        }|rt	        |      | S N)r(   _SECURITY_KEYCHAIN_OPENr#   _SECURITY_KEYCHAIN_COPY_DEFAULTOSError)r   statuss     r   	__enter__zKeychain.__enter__   s=    >>,T^^TYYGF4TYY?F&/!r   c                 H    | j                   rt        | j                          y y r+   )r#   _CORE_RELEASE)r   argss     r   __exit__zKeychain.__exit__   s    99$))$ r   c           
         |j                  d      }|j                  d      }t        j                         }t        j                         }t	        | j
                  t        |      |t        |      |||d      }|rt        |      t        j                  |j                        }t        j                  ||j                  |j                         t        d|       |j                  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   create_string_buffervaluememmove$_SECURITY_KEYCHAIN_ITEM_FREE_CONTENTrawdecode)r   serviceaccount_namelengthcontentsr   r;   s          r   get_generic_passwordzKeychain.get_generic_password   s     ..)#**73!!###%>IIL	
 K88,,V\\:x~~v||<,T8<yy((r   c           
         |j                  d      }|j                  d      }|j                  d      }t        j                         }t        | j                  t        |      |t        |      |dd|      }|s&t        |dt        |      |      }|rt        |      y|t        j                  k(  rFt        | j                  t        |      |t        |      |t        |      |d      }|rt        |      yt        |      )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    Nr6   )
r'   r!   r"   r8   r#   r9   2_SECURITY_KEYCHAIN_ITEM_MODIFY_ATTRIBUTES_AND_DATAr   r   '_SECURITY_KEYCHAIN_ADD_GENERIC_PASSWORD)r   r@   rA   r;   entryfind_exit_statusmodify_exit_statusadd_exit_statuss           r   set_generic_passwordzKeychain.set_generic_password   s
    ..)#**73W%  "CIIL	
  !SE
	" "#0BCC " !=!==E		GL!E
	O #@@   ,<==r   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   r@   usernames      r   get_internet_passwordzKeychain.get_internet_password        "##r   c                     t               )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.
        rN   )r   r@   rP   r;   s       r   set_internet_passwordzKeychain.set_internet_password   rR   r   r+   )r   r   r   r   r   r0   r4   rD   rL   rQ   rT    r   r   r   r   }   s*    I"%)@1>f$$r   r   )$r   r$   ctypesr!   c_int32	OS_RESULTr.   r   r   CDLL	_SECURITY_CORE	CFReleaser2   r"   argtypesSecCopyErrorMessageString#_SECURITY_COPY_ERROR_MESSAGE_STRINGc_char_prestypeSecKeychainOpenr,   POINTERSecKeychainCopyDefaultr-   SecKeychainItemFreeContentr=   &SecKeychainItemModifyAttributesAndDatarF   r7   SecKeychainFindGenericPasswordr8   SecKeychainAddGenericPasswordrG   objectr   rU   r   r   <module>rj      s   S 	 OO	'G '"G GLL-j9:	)*:;<   '0&I&I #0 # , /6.>.> # + $33 GOOG$$%$    #,   #,"B"B GOOG$$%,  ( +4  ' (1'K'K $1 $ - 09 $ , 44 3 	? 2 ; >G 2 : ,5+S+S (GOOG$$%GOOG$$%GOOG$$%	5 ( 1 4= ( 0 +4*Q*Q 'GOOG$$%	4 ' 0 3< ' /$v $r   