ó
Èuübc           @   sñ   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 j j	 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 Z d  d l Z d d	 d „  ƒ  YZ d S(
   iÿÿÿÿN(   t   datetime(   t   sha1(   t   BeautifulSoup(   t
   PKCS1_v1_5(   t   RSAt   proxyc           B   s•   e  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(   s0   Class to handle building of the soap client
    c         C   s   | |  _  | |  _ | |  _ d S(   s™   Constructor

        Arguments:
            wsdl {str} -- WSDL url
            username {str} -- Username
            password {str} -- Password
        N(   t   wsdlt   usernamet   password(   t   selfR   R   R   t   certificate_file_path(    (    s   /var/www/nira/nira_proxy.pyt   __init__   s    		c         C   s   t  j d ƒ S(   s!   Generates Nonce as bytes
        i   (   t   ost   urandom(   R	   (    (    s   /var/www/nira/nira_proxy.pyt   generatenonce_asbytes(   s    c         C   s   t  t j d ƒ ƒ S(   s%   Generates Nonce as bytearray
        i   (   t	   bytearrayR   R   (   R	   (    (    s   /var/www/nira/nira_proxy.pyt   generatenonce_asbytearray-   s    c         C   sS   t  j j t j ƒ  ƒ } | j t  j d ƒ ƒ } | j ƒ  } d j | d  ƒ } | S(   sB   Creates timestamp to be used when
        sending request
        s   Africa/Kampalas   {}+03:00i÷ÿÿÿ(	   t   pytzt   utct   localizeR    t   utcnowt
   astimezonet   timezonet	   isoformatt   format(   R	   t   utc_nowt   eat_nowt   eat_timet	   timestamp(    (    s   /var/www/nira/nira_proxy.pyt   create_requesttimestamp2   s
    c         C   s@   t  j j t j ƒ  ƒ } | j t  j d ƒ ƒ } | j ƒ  } | S(   s   Create timestamp
        s   Africa/Kampala(   R   R   R   R    R   R   R   R   (   R	   R   R   R   (    (    s   /var/www/nira/nira_proxy.pyt   create_timestamp>   s    c         C   s   d j  | d  ƒ S(   s§   Formats timestamp for request

        Arguments:
            timestamp {string} -- Timestamp in the format
            to be sent with the request as Created
        s   {}+03:00i÷ÿÿÿ(   R   (   R	   R   (    (    s   /var/www/nira/nira_proxy.pyt   timestamp_forrequestG   s    c         C   s   d j  | d  ƒ S(   s¤   Formates timestamp for digest

        Arguments:
            timestamp {string} -- Timestamp in the format
            to be used to creat password digest
        s   {}+0300i÷ÿÿÿ(   R   (   R	   R   (    (    s   /var/www/nira/nira_proxy.pyt   timestamp_fordigestP   s    c         C   s   | j  d ƒ S(   s]   Gets timestamp as bytes

        Arguments:
            timestamp {str} -- Timestamp
        s   utf-8(   t   encode(   R	   R   (    (    s   /var/www/nira/nira_proxy.pyt   gettimestamp_asbytesY   s    c         C   s   t  |  j j d ƒ ƒ j ƒ  S(   s3   Hash password using sha1.digest() function
        s   utf-8(   R   R   R!   t   digest(   R	   (    (    s   /var/www/nira/nira_proxy.pyt   hashpassword_withdigesta   s    c         C   s^   t  ƒ  } | j | ƒ | j | ƒ | j | ƒ t | ƒ j ƒ  } t j | ƒ } | j d ƒ S(   sØ   Generates password digest using sha1.digest
        function

        Arguments:
            nonce {bytes} -- Nonce
            created {bytes} -- Created
            password_hash {bytes} -- Hashed password
        s   utf-8(   R   t   extendR   R#   t   base64t	   b64encodet   decode(   R	   t   noncet   createdt   password_hasht   combined_bytearrayt   encoded_digestt   password_digest(    (    s   /var/www/nira/nira_proxy.pyt   generatedigest_withbytesvaluesf   s    		c         C   s¦   |  j  } |  j ƒ  } t j | ƒ j d ƒ } |  j ƒ  } |  j | ƒ } |  j | ƒ } |  j ƒ  } |  j	 | | | ƒ }	 |  j
 | ƒ }
 d j | |	 | |
 | ƒ } | S(   s.   
         Build request to get person
        s   utf-8sr  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fac="http://facade.server.pilatus.thirdparty.tidis.muehlbauer.de/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><soapenv:Header><wsse:UsernameToken><wsse:Username>{0}</wsse:Username><wsse:Password Type="PasswordDigest">{1}</wsse:Password><wsse:Nonce>{2}</wsse:Nonce><wsse:Created>{3}</wsse:Created></wsse:UsernameToken></soapenv:Header><soapenv:Body><fac:getPerson><!--Optional:--><request><!--Optional:--><nationalId>{4}</nationalId></request></fac:getPerson></soapenv:Body></soapenv:Envelope>(   R   R   R&   R'   R(   R   R    R"   R$   R/   R   R   (   R	   t
   nationalIdR   t   nonce_bytesR)   R   t   created_digestt   created_digest_bytest   passwordhash_bytesR.   t   created_requestt   body(    (    s   /var/www/nira/nira_proxy.pyt
   get_person~   s    	c         C   sA   |  j  } i d d 6} t j | d | d | ƒ} | j j d ƒ S(   s   Sends the SOAP request
        s   text/xmls   Content-Typet   datat   headerss   utf-8(   R   t   requestst   postt   contentR(   (   R	   R6   t   urlR9   t   response(    (    s   /var/www/nira/nira_proxy.pyt   send_request“   s    	c         C   s   t  | d ƒ } | j d ƒ d j d k rb g  } | j d ƒ } t | ƒ } t j t j | ƒ ƒ Sg  } | j d ƒ } t | ƒ } t j t j | ƒ ƒ Sd S(   sØ   Parses the response returned from the API request
        
        Arguments:
            response {string} -- API response

        Returns:
            tuple (transactionStatus, cardStatus, matchingStatus)
        s   lxml-xmlt   transactionStatusi   t   Okt   returnN(	   R   t   find_allt   stringt   findt   strt   jsont   dumpst	   xmltodictt   parse(   R	   R>   t   soupt   retnt   rtt   retn2(    (    s   /var/www/nira/nira_proxy.pyt   parse_request   s    	c         C   s§   d } t  j | ƒ t  j t j ƒ t  j t j ƒ t  j t j ƒ g g  t d ƒ D]1 } t  j t j t j t j | t j ƒ ^ qO } t  j | ƒ d j | ƒ } | S(   s   Generates password.
        s   @!#_+$%*i   t    (	   t   randomt   choiceRD   t   digitst   ascii_lowercaset   ascii_uppercaset   ranget   shufflet   join(   R	   t   special_characterst   it   password_listR   (    (    s   /var/www/nira/nira_proxy.pyt   password_generator·   s    Bc         C   sg   | j  d ƒ } t j t | d ƒ j ƒ  ƒ } t j | ƒ } | j | ƒ } t j	 | ƒ j
 d ƒ } | S(   s(   
        Encrypts raw pasword.

        s   utf-8t   r(   R!   R   t	   importKeyt   opent   readR   t   newt   encryptR&   R'   R(   (   R	   t   raw_passwordR
   t   messaget
   the_pubkeyt   ciphert
   ciphertextt   pwd_to_base_64(    (    s   /var/www/nira/nira_proxy.pyt   encrypt_pwd_with_PyCryptoÕ   s    (   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R    R"   R$   R/   R7   R?   RO   R\   Ri   (    (    (    s   /var/www/nira/nira_proxy.pyR      s    															
		(    (   R   R   R&   R:   R    t   hashlibR   t   xml.etree.ElementTreet   etreet   ElementTreet   ETt   bs4R   RD   RG   t   syst   Cryptot   Crypto.CipherR   t   Crypto.PublicKeyR   RQ   RI   R   (    (    (    s   /var/www/nira/nira_proxy.pyt   <module>   s    