ó
Á£ô_c           @   s‚  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z d d l	 Z
 d d l Z d d l Z d d l Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z e j e ƒ Z d e f d „  ƒ  YZ  d e j! f d „  ƒ  YZ" d e j# f d „  ƒ  YZ$ d e j! f d „  ƒ  YZ% d S(   s!   Creates ACME accounts for server.iÿÿÿÿN(   t   serialization(   t   fields(   t   messages(   t
   ClientBase(   t   errors(   t
   interfaces(   t   util(   t	   constants(   t   ost   Accountc           B   sT   e  Z d  Z d e j f d „  ƒ  YZ d d „ Z e d „  ƒ Z	 d „  Z
 d „  Z RS(   sá   ACME protocol registration.

    :ivar .RegistrationResource regr: Registration Resource
    :ivar .JWK key: Authorized Account Key
    :ivar .Meta: Account metadata
    :ivar str id: Globally unique account identifier.

    t   Metac           B   sA   e  Z d  Z e j d ƒ Z e j d ƒ Z e j d d e	 ƒZ
 RS(   sÍ  Account metadata

        :ivar datetime.datetime creation_dt: Creation date and time (UTC).
        :ivar str creation_host: FQDN of host, where account has been created.
        :ivar str register_to_eff: If not None, Certbot will register the provided
                                        email during the account registration.

        .. note:: ``creation_dt`` and ``creation_host`` are useful in
            cross-machine migration scenarios.

        t   creation_dtt   creation_hostt   register_to_efft	   omitempty(   t   __name__t
   __module__t   __doc__t   acme_fieldst   RFC3339FieldR   t   joset   FieldR   t   TrueR   (    (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyR
   %   s   c         C   sì   | |  _  | |  _ | d  k r` |  j d t j j d t j ƒ j d d ƒ d t	 j
 ƒ  d d  ƒ n | |  _ y t j ƒ  } Wn& t k
 r¡ t j d d t ƒ} n X| j |  j  j  j ƒ  j d	 t j j d
 t j j ƒ ƒ | j ƒ  |  _ d  S(   NR   t   tzt   microsecondi    R   R   t   md5t   usedforsecurityt   encodingt   format(   t   keyt   regrt   NoneR
   t   datetimet   nowt   pytzt   UTCt   replacet   sockett   getfqdnt   metat   hashlibR   t
   ValueErrort   newt   Falset   updatet
   public_keyt   public_bytesR    t   Encodingt   PEMt   PublicFormatt   SubjectPublicKeyInfot	   hexdigestt   id(   t   selfR   R   R'   t   hasher(    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyt   __init__5   s    		Wc         C   s/   d j  t j |  j j ƒ |  j j |  j d  ƒ S(   s3   Short account identification string, useful for UI.s   {1}@{0} ({2})i   (   R   t	   pyrfc3339t   generateR'   R   R   R4   (   R5   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyt   slugP   s    c         C   s%   d j  |  j j |  j |  j |  j ƒ S(   Ns   <{0}({1}, {2}, {3})>(   R   t	   __class__R   R   R4   R'   (   R5   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyt   __repr__V   s    c         C   sF   t  | |  j ƒ oE |  j | j k oE |  j | j k oE |  j | j k S(   N(   t
   isinstanceR;   R   R   R'   (   R5   t   other(    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyt   __eq__Z   s    $N(   R   R   R   R   t   JSONObjectWithFieldsR
   R   R7   t   propertyR:   R<   R?   (    (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyR	      s   	t   AccountMemoryStoragec           B   s5   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z RS(   s   In-memory account storage.c         C   s   | d  k	 r | n i  |  _ d  S(   N(   R   t   accounts(   R5   t   initial_accounts(    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyR7   c   s    c         C   s   t  t j |  j ƒ ƒ S(   N(   t   listt   sixt
   itervaluesRC   (   R5   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyt   find_allf   s    c         C   s<   | j  |  j k r( t j d | j  ƒ n  | |  j | j  <d  S(   Ns   Overwriting account: %s(   R4   RC   t   loggert   debug(   R5   t   accountt   client(    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyt   savei   s    c         C   s6   y |  j  | SWn  t k
 r1 t j | ƒ ‚ n Xd  S(   N(   RC   t   KeyErrorR   t   AccountNotFound(   R5   t
   account_id(    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyt   loadn   s    N(   R   R   R   R   R7   RH   RM   RQ   (    (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyRB   `   s
   		t$   RegistrationResourceWithNewAuthzrURIc           B   s   e  Z d  Z e j d ƒ Z RS(   sf  A backwards-compatible RegistrationResource with a new-authz URI.

       Hack: Certbot versions pre-0.11.1 expect to load
       new_authzr_uri as part of the account. Because people
       sometimes switch between old and new versions, we will
       continue to write out this field for some time so older
       clients don't crash in that scenario.
    t   new_authzr_uri(   R   R   R   R   R   RS   (    (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyRR   t   s   t   AccountFileStoragec           B   sï   e  Z d  Z d „  Z d „  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sM   Accounts file storage.

    :ivar .IConfig config: Client configuration

    c         C   s)   | |  _  t j | j d |  j  j ƒ d  S(   NiÀ  (   t   configR   t   make_or_verify_dirt   accounts_dirt   strict_permissions(   R5   RU   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyR7   …   s    	c         C   s   |  j  | |  j j ƒ S(   N(   t!   _account_dir_path_for_server_pathRU   t   server_path(   R5   RP   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyt   _account_dir_path‰   s    c         C   s%   |  j  j | ƒ } t j j | | ƒ S(   N(   RU   t   accounts_dir_for_server_pathR   t   patht   join(   R5   RP   RZ   RW   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyRY   Œ   s    c         C   s   t  j j | d ƒ S(   Ns	   regr.json(   R   R]   R^   (   t   clst   account_dir_path(    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyt
   _regr_path   s    c         C   s   t  j j | d ƒ S(   Ns   private_key.json(   R   R]   R^   (   R_   R`   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyt	   _key_path”   s    c         C   s   t  j j | d ƒ S(   Ns	   meta.json(   R   R]   R^   (   R_   R`   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyt   _metadata_path˜   s    c         C   s	  |  j  j | ƒ } y t j | ƒ } Wn t k
 r9 g  SXg  } xU | D]M } y | j |  j | | ƒ ƒ WqG t j k
 r“ t	 j
 d d t ƒqG XqG W| r| t j k rt j | } |  j | ƒ } | rü y |  j | | ƒ Wqü t k
 rø g  SXn  | } n  | S(   Ns   Account loading problemt   exc_info(   RU   R\   R   t   listdirt   OSErrort   appendt   _load_for_server_pathR   t   AccountStorageErrorRI   RJ   R   R   t   LE_REUSE_SERVERSt   _find_all_for_server_patht   _symlink_to_accounts_dir(   R5   RZ   RW   t
   candidatesRC   RP   t   prev_server_patht   prev_accounts(    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyRk   œ   s*    	c         C   s   |  j  |  j j ƒ S(   N(   Rk   RU   RZ   (   R5   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyRH   ·   s    c         C   s8   |  j  | | ƒ } |  j  | | ƒ } t j | | ƒ d  S(   N(   RY   R   t   symlink(   R5   Rn   RZ   RP   t   prev_account_dirt   new_account_dir(    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyt   _symlink_to_account_dirº   s    c         C   sg   |  j  j | ƒ } t j j | ƒ r4 t j | ƒ n t j | ƒ |  j  j | ƒ } t j | | ƒ d  S(   N(   RU   R\   R   R]   t   islinkt   unlinkt   rmdirRp   (   R5   Rn   RZ   RW   Rq   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyRl   ¿   s    c         C   sŽ  |  j  | | ƒ } t j j | ƒ s³ | t j k r t j | } |  j | | ƒ } |  j j | ƒ } t j	 | ƒ r‰ |  j
 | | | ƒ n |  j | | ƒ | St j d | ƒ ‚ n  y¦ t |  j | ƒ ƒ  } t j j | j ƒ  ƒ } Wd  QXt |  j | ƒ ƒ  }	 t j j |	 j ƒ  ƒ }
 Wd  QXt |  j | ƒ ƒ  } t j j | j ƒ  ƒ } Wd  QXWn" t k
 r}} t j | ƒ ‚ n Xt | |
 | ƒ S(   Ns   Account at %s does not exist(   RY   R   R]   t   isdirR   Rj   Rh   RU   R\   Re   Rs   Rl   R   RO   t   openRa   R   t   RegistrationResourcet
   json_loadst   readRb   R   t   JWKRc   R	   R
   t   IOErrorRi   (   R5   RP   RZ   R`   Rn   t   prev_loaded_accountRW   t	   regr_fileR   t   key_fileR   t   metadata_fileR'   t   error(    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyRh   È   s,    "c         C   s   |  j  | |  j j ƒ S(   N(   Rh   RU   RZ   (   R5   RP   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyRQ   å   s    c         C   so   yF |  j  | ƒ } |  j | | ƒ |  j | | ƒ |  j | | | ƒ Wn" t k
 rj } t j | ƒ ‚ n Xd S(   sš   Create a new account.

        :param Account account: account to create
        :param ClientBase client: ACME client associated to the account

        N(   t   _preparet   _createt   _update_metat   _update_regrR}   R   Ri   (   R5   RK   RL   t   dir_pathR‚   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyRM   è   s    c         C   sO   y& |  j  | ƒ } |  j | | | ƒ Wn" t k
 rJ } t j | ƒ ‚ n Xd S(   s¦   Update the registration resource.

        :param Account account: account to update
        :param ClientBase client: ACME client associated to the account

        N(   Rƒ   R†   R}   R   Ri   (   R5   RK   RL   R‡   R‚   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyt   update_regrø   s
    c         C   sL   y# |  j  | ƒ } |  j | | ƒ Wn" t k
 rG } t j | ƒ ‚ n Xd S(   sV   Update the meta resource.

        :param Account account: account to update

        N(   Rƒ   R…   R}   R   Ri   (   R5   RK   R‡   R‚   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyt   update_meta  s
    c         C   s|   |  j  | ƒ } t j j | ƒ s7 t j d | ƒ ‚ n  |  j | |  j j ƒ t j	 |  j j
 ƒ sx |  j |  j j ƒ n  d S(   sn   Delete registration info from disk

        :param account_id: id of account which should be deleted

        s   Account at %s does not existN(   R[   R   R]   Rw   R   RO   t#   _delete_account_dir_for_server_pathRU   RZ   Re   RW   t$   _delete_accounts_dir_for_server_path(   R5   RP   R`   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyt   delete  s    c         C   s8   t  j |  j | ƒ } |  j | | ƒ } t j | ƒ d  S(   N(   t	   functoolst   partialRY   t!   _delete_links_and_find_target_dirt   shutilt   rmtree(   R5   RP   RZ   t	   link_funct   nonsymlinked_dir(    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyRŠ   $  s    c         C   s/   |  j  j } |  j | | ƒ } t j | ƒ d  S(   N(   RU   R\   R   R   Rv   (   R5   RZ   R’   R“   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyR‹   )  s    c   
      C   sð   | | ƒ } i  } x" t  j D] } | | t  j | <q Wt } xq | r° t } | | k r@ | | } | | ƒ } t j j | ƒ r­ t j | ƒ | k r­ t } | } | } q­ q@ q@ Wx8 t j j | ƒ rë t j | ƒ }	 t j | ƒ |	 } q´ W| S(   s/  Delete symlinks and return the nonsymlinked directory path.

        :param str server_path: file path based on server
        :param callable link_func: callable that returns possible links
            given a server_path

        :returns: the final, non-symlinked target
        :rtype: str

        (	   R   Rj   R   R+   R   R]   Rt   t   readlinkRu   (
   R5   RZ   R’   R‡   t   reused_serverst   kt   possible_next_linkt   next_server_patht   next_dir_patht   target(    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyR   .  s&    	
'
c         C   s/   |  j  | j ƒ } t j | d |  j j ƒ | S(   NiÀ  (   R[   R4   R   RV   RU   RX   (   R5   RK   R`   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyRƒ   U  s    c         C   sD   t  j |  j | ƒ d d d ƒ } | j | j j ƒ  ƒ Wd  QXd  S(   Nt   wt   chmodi   (   R   t	   safe_openRb   t   writeR   t
   json_dumps(   R5   RK   R‡   R€   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyR„   [  s    $c         C   s•   t  |  j | ƒ d ƒ w } | j } t | j d ƒ r] t d | j j d i  d | j ƒ } n t j	 d i  d | j ƒ } | j
 | j ƒ  ƒ Wd  QXd  S(   NR›   s	   new-authzRS   t   bodyt   uri(   Rx   Ra   R   t   hasattrt	   directoryRR   t	   new_authzR¡   R   Ry   Rž   RŸ   (   R5   RK   t   acmeR‡   R   R   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyR†   `  s    		c         C   s;   t  |  j | ƒ d ƒ  } | j | j j ƒ  ƒ Wd  QXd  S(   NR›   (   Rx   Rc   Rž   R'   RŸ   (   R5   RK   R‡   R   (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyR…   s  s    (   R   R   R   R7   R[   RY   t   classmethodRa   Rb   Rc   Rk   RH   Rs   Rl   Rh   RQ   RM   Rˆ   R‰   RŒ   RŠ   R‹   R   Rƒ   R„   R†   R…   (    (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyRT      s0   																	'			(&   R   R    R   R(   t   loggingR   R%   t   cryptography.hazmat.primitivesR    t   josepyR   R8   R"   RF   R¥   R   R   R   t   acme.clientR   t   certbotR   R   R   t   certbot._internalR   t   certbot.compatR   t	   getLoggerR   RI   t   objectR	   t   AccountStorageRB   Ry   RR   RT   (    (    (    s=   /usr/lib/python2.7/site-packages/certbot/_internal/account.pyt   <module>   s0   E