o
    PDi                     @   sx   d dl Z d dlZd dlmZ d dlmZ dddZdd Zd	d
 Zdd Z	dd Z
dd ZdejfddZee dS )    N)rsa)serializationFc                 C   sT   |  dpd}| }|dkrt| S |dkrt| S |dkr#t| S td| )zF
    Generates an OCI access token based on provided credentials.
    	auth_type configfileauthenticationsimpleauthenticationinstanceprincipalz-Unrecognized auth_type authentication method )getlower!_config_file_based_authentication_simple_authentication"_instance_principal_authentication
ValueError)token_auth_configrefreshuser_auth_typer    r   T/var/www/Datamplify/venv/lib/python3.10/site-packages/oracledb/plugins/oci_tokens.pygenerate_token%   s   r   c                  C   s~   t jddd} | jtjjtjjt d	d}| 
 jtjjtjjd	d}t s:ddd	 | D }|}||d
S )zF
    Generates a public-private key pair for proof of possession.
    i  i   )public_exponentkey_size)encodingformatencryption_algorithmzutf-8)r   r   r   c                 s   s,    | ]}| d s| ds| V  qdS )z
-----BEGINz-----ENDN)
startswithstrip).0liner   r   r   	<genexpr>O   s    
z _get_key_pair.<locals>.<genexpr>)private_key
public_key)r   generate_private_keyprivate_bytesr   EncodingPEMPrivateFormatPKCS8NoEncryptiondecoder    public_bytesPublicFormatSubjectPublicKeyInfooracledbis_thin_modejoin
splitlines)r   private_key_pempublic_key_pemp_keyr   r   r   _get_key_pair7   s0   	

r3   c                 C   sD   t  }|dd}tjjj||d d}| j|d}|jj|d fS )z@
    Token generation logic used by authentication methods.
    scopezurn:oracle:db::id::*r    )r4   r    )$generate_scoped_access_token_detailsr   )	r3   r	   ociidentity_data_planemodels GenerateScopedAccessTokenDetailsgenerate_scoped_access_tokendatatoken)clientr   key_pairr4   detailsresponser   r   r   _generate_access_token[   s   rA   c                 C   sP   |  dtjj}|  dtjj}tj||}tj| tj|}t	|| S )zg
    Config file base authentication implementation: config parameters
    are provided in a file.
    file_locationprofile)
r	   r6   configDEFAULT_LOCATIONDEFAULT_PROFILE	from_filevalidate_configr7   DataplaneClientrA   )r   rB   rC   rD   r=   r   r   r   r   l   s   
r   c                 C   sL   | d | d | d | d | d | d d}t j| t j|}t|| S )zK
    Simple authentication: config parameters are passed as parameters
    userkey_filefingerprinttenancyregionrC   )rJ   rK   rL   rM   rN   rC   )r6   rD   rH   r7   rI   rA   )r   rD   r=   r   r   r   r      s   
r   c                 C   s&   t jj }t jji |d}t|| S )za
    Instance principal authentication: for compute instances
    with dynamic group access.
    )rD   signer)r6   authsigners%InstancePrincipalsSecurityTokenSignerr7   rI   rA   )r   rO   r=   r   r   r   r      s   
r   paramsc                    s*    j dur fdd} j|d dS dS )z3
    OCI-specific hook for generating a token.
    Nc                    s   t  j| S )N)r   extra_auth_params)r   rS   r   r   token_callback   s   z&oci_token_hook.<locals>.token_callback)access_token)rT   set)rS   rV   r   rU   r   oci_token_hook   s   
rY   )F)r6   r,   )cryptography.hazmat.primitives.asymmetricr   cryptography.hazmat.primitivesr   r   r3   rA   r   r   r   ConnectParamsrY   register_params_hookr   r   r   r   <module>   s   
$
