o
    >e                     @   s  d dl Z d dlmZ d dlmZmZ d dlZd dlZd dlmZ d dl	m
Z
 d dlmZmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZmZ d dlmZmZ d dlmZ dedee de fddZ!dededefddZ"dedej#fddZ$dS )    N)datetime)ListOptional)pem)
CLIContext)get_text_paramsopen_for_signing)SigningCommandPlugin)pyhanko_exception_manager)readable_filewritable_file)
PdfHandler)	PdfSignerfields)PdfCMSSignedAttributesselect_suitable_signing_md)HTTPTimeStamperhandlernamewill_createc           
      C   s   d}z-t tj| |d\}}}tj| d}t|\}}	|r"|	s-t| d| dW d S  tyS   |s@t| d|sPt| d| d| dY d S w )	Nz3You seem to be trying to create a visible signature)	with_name)	sig_fieldz, but the field 'ze' in the PDF is not a visible one. Please specify another field name if you need a visible signature.z, but the PDF did not contain any signature fields, and you did not specify a bounding box. Please specify the field as --field "PAGE/X1,Y1,X2,Y2/NAME" to create a visible signature field at the coordinates provided.z' does not exist in the PDF file, and you did not specify a bounding box. Please specify the field as --field "PAGE/X1,Y1,X2,Y2/zB" to create a visible signature field at the coordinates provided.)	nextr   enumerate_sig_fieldsget_sig_field_annot
get_objectannot_width_heightclickClickExceptionStopIteration)
r   r   r   prefixfq_name_	field_ref	sig_annotwh r'   d/var/www/html/humari/django-venv/lib/python3.10/site-packages/pyhanko/cli/commands/signing/plugin.py_ensure_field_visible   s2   
r)   plugininfileoutfilec              
   K   s  t  }|j}|j}|d urt|}nd }| j|fi |}|j}	|	d u r|d ur-d }
ntjt	
 d}
t|d%}|j}|d usDJ |j|t|j|t|
dd}t|}W d    n1 sbw   Y  | }|jrttd|}t|d}|| W d    n1 sw   Y  nct||jdL}|jjrt||	j|jd u t|	|||j |jdj!||j"t#|d	}t|d}|$ }|| |%  W d    n1 sw   Y  W d    n1 sw   Y  W d    d S W d    d S W d    d S 1 sw   Y  d S )
N)tzrb)signing_time)timestampersigned_attr_settingsPKCS7wb)infile_pathlenient)signerr0   stamp_stylenew_field_spec)existing_fields_onlyappearance_text_params)&r   get_current_contextobjtimestamp_urlr   create_signersig_settingsr   nowtzlocalget_localzoneopensigning_certasync_sign_general_datar   
public_keyr   asynciorundump
detach_pemr   armorwriter   r5   uxvisible_signature_desiredr)   
field_namer8   r   r7   sign_pdfr9   r   	getbufferrelease)r*   r+   r,   kwargsctxcli_ctxr=   r0   r6   pdf_sig_settingstimestamp_attrinfcertsignature_job	signatureoutput_bytesoutr%   resultoutfbufr'   r'   r(   _callback_logic;   s   


  $ra   returnc                    sd   dt dt f fdd}tjdtdtjdtdg}|   |   tj j	| j
|dS )	Nr+   r,   c                    s@   t   t | |fi | W d    d S 1 sw   Y  d S )N)r
   ra   )r+   r,   rS   r*   r'   r(   	_callback   s   "z&command_from_plugin.<locals>._callback)r+   )type)r,   )r   callbackhelpparams)strr   Argumentr   r   extendclick_extra_argumentsclick_optionsCommandsubcommand_namehelp_summary)r*   rd   rh   r'   rc   r(   command_from_plugin   s   rq   )%rG   r   typingr   r   r   rA   
asn1cryptor   pyhanko.cli._ctxr   "pyhanko.cli.commands.signing.utilsr   r   pyhanko.cli.plugin_apir	   pyhanko.cli.runtimer
   pyhanko.cli.utilsr   r   pyhanko.pdf_utils.rw_commonr   pyhanko.signr   r   pyhanko.sign.signers.pdf_cmsr   r   pyhanko.sign.timestampsr   ri   boolr)   ra   rn   rq   r'   r'   r'   r(   <module>   s<    
$
F