o
    /׾gW8                     @   s   d Z g dZddlmZ ddlmZ ddlmZmZ ddl	Z	ddl
Z
e
eZddgZg d	Ze	d
ZG dd deZdd ZG dd deZdZdd Zdd Zdd ZefddZdS )aQ  
This module contains the C{L{TrustRoot}} class, which helps handle
trust root checking.  This module is used by the
C{L{openid.server.server}} module, but it is also available to server
implementers who wish to use it for additional trust root checking.

It also implements relying party return_to URL verification, based on
the realm.
)	TrustRootRP_RETURN_TO_URL_TYPEextractReturnToURLsreturnToMatchesverifyReturnTo    )urinorm)services)urlparse
urlunparseNhttphttps(  acadaeaeroafagaialamanaoaqararpaasasiaatauawaxazbabbbdbebfbgbhbibizbjbmbnbobrbsbtbvbwbybzcacatcccdcfcgchcickclcmcncocomcoopcrcucvcxcyczdedjdkdmdodzecedueeegereseteufifjfkfmfofrgagbgdgegfggghgiglgmgngovgpgqgrgsgtgugwgyhkhmhnhrhthuidieilimininfointioiqirisitjejmjojobsjpkekgkhkikmknkpkrkwkykzlalblclilklrlsltlulvlymamcmdmemgmhmilmkmlmmmnmomobimpmqmrmsmtmumuseummvmwmxmymznanamencnenetnfngninlnonpnrnunzomorgpapepfpgphpkplpmpnprpropsptpwpyqarerorsrurwsasbscsdsesgshsisjskslsmsnsosrstsusvsysztctdteltftgthtjtktltmtntotptrtraveltttvtwtzuaugukusuyuzvavcvevgvivnvuwfwszxn--0zwm56dzxn--11b5bs3a9aj6gzxn--80akhbyknj4fzxn--9t4b11yi5azxn--deba0adzxn--g6w251dzxn--hgbk6aj7f53bbazxn--hlcj6aya9esc7azxn--jxalpdlpzxn--kgbechtvz
xn--zckzahyeytyuzazmzwz3(?:[-a-zA-Z0-9!$&'\(\)\*+,;=._~]|%[a-zA-Z0-9]{2})+$c                   @   s    e Zd ZdZdd Zdd ZdS )RealmVerificationRedirectedzOAttempting to verify this realm resulted in a redirect.

    @since: 2.1.0
    c                 C   s   || _ || _d S Nrelying_party_urlrp_url_after_redirects)selfr  r   r   a/var/www/html/backend_erp/backend_erp_env/lib/python3.10/site-packages/openid/server/trustroot.py__init__H   s   
z$RealmVerificationRedirected.__init__c                 C   s   d| j | jf S )Nz2Attempting to verify %r resulted in redirect to %rr  r  r   r   r!  __str__L   s
   z#RealmVerificationRedirected.__str__N)__name__
__module____qualname____doc__r"  r$  r   r   r   r!  r  B   s    r  c           	      C   s   zt  | } W n
 ty   Y d S w t| \}}}}}}|s.|s,d|v r,|dd\}}d}tdd||||f}d|v rYz	|d\}}W n
 tyO   Y d S w td|sXd S n|}d}| }t|shd S ||||fS )N?   / :z\d+$)	r   
ValueErrorr	   splitr
   r   matchlowerhost_segment_re)	urlprotonetlocpathparamsqueryfraghostportr   r   r!  	_parseURLR   s4   
r<  c                   @   sp   e Zd ZdZdd Zdd Zdd Zdd	 ZeeZd
d Z	ee	Z	dd Z
ee
Z
dd Zdd Zdd ZdS )r   a  
    This class represents an OpenID trust root.  The C{L{parse}}
    classmethod accepts a trust root string, producing a
    C{L{TrustRoot}} object.  The method OpenID server implementers
    would be most likely to use is the C{L{isSane}} method, which
    checks the trust root for given patterns that indicate that the
    trust root is too broad or points to a local network resource.

    @sort: parse, isSane
    c                 C   s(   || _ || _|| _|| _|| _|| _d S r  unparsedr4  wildcardr:  r;  r6  )r  r>  r4  r?  r:  r;  r6  r   r   r!  r"     s   
zTrustRoot.__init__c                 C   s   | j dkrdS | j d}| jr|d dksJ ||d= |r&|d s&|d= |s*dS d|v r0dS |d }|tvr:dS t|dkrBdS | jrYt|d	krYt|d
 dkrYt|d	kS dS )aA  
        This method checks the to see if a trust root represents a
        reasonable (sane) set of URLs.  'http://*.com/', for example
        is not a reasonable pattern, as it cannot meaningfully specify
        the site claiming it.  This function attempts to find many
        related examples, but it can only work via heuristics.
        Negative responses from this method should be treated as
        advisory, used only to alert the user to examine the trust
        root carefully.


        @return: Whether the trust root is sane

        @rtype: C{bool}
        	localhostT.r   r,  Fr*        )r:  r/  r?  _top_level_domainslen)r  
host_partstldr   r   r!  isSane   s*   
zTrustRoot.isSanec                 C   s   t |}|du r
dS |\}}}}|| jkrdS || jkrdS d|v r$dS | js/|| jkr.dS n|| js>d| | jkr>dS || jkrrt| j}| jd| }|d| }	||	kr[dS d| jv rcd}
nd}
| jd |
v pq|| |
v S d	S )
z
        Validates a URL against this trust root.


        @param url: The URL to check

        @type url: C{str}


        @return: Whether the given URL is within this trust root.

        @rtype: C{bool}
        NF*rA  r)  &z?/rB  T)r<  r4  r;  r?  r:  endswithr6  rG  )r  r3  	url_partsr4  r:  r;  r6  path_lentrust_prefix
url_prefixallowedr   r   r!  validateURL   s6   





zTrustRoot.validateURLc           	      C   s   t |}|du r
dS |\}}}}|tvrdS |ddkrdS |dddkr)dS |drEt|dkr<|d dkr<dS |dd }d}nd}| ||||||}|S )	a  
        This method creates a C{L{TrustRoot}} instance from the given
        input, if possible.


        @param trust_root: This is the trust root to parse into a
        C{L{TrustRoot}} object.

        @type trust_root: C{str}


        @return: A C{L{TrustRoot}} instance if trust_root parses as a
        trust root, C{None} otherwise.

        @rtype: C{NoneType} or C{L{TrustRoot}}
        N#rB  rK  r*  rA  TF)r<  
_protocolsfind
startswithrG  )	cls
trust_rootrN  r4  r:  r;  r6  wilcardr   r   r   r!  parse   s$   
zTrustRoot.parsec                 C   s   |  |}|du rdS | S )z8str -> bool

        is this a sane trust root?
        NF)r[  rJ  )rX  trust_root_stringrY  r   r   r!  checkSanity-  s   
zTrustRoot.checkSanityc                 C   s   |  |}|duo||S )zpquick func for validating a url against a trust root.  See the
        TrustRoot class if you need more control.N)r[  rS  )rX  rY  r3  r   r   r   r!  checkURL:  s   
zTrustRoot.checkURLc                 C   s>   | j r| jdsJ | jd| j }d| j|| jf S | jS )aZ  Return a discovery URL for this realm.

        This function does not check to make sure that the realm is
        valid. Its behaviour on invalid inputs is undefined.

        @rtype: str

        @returns: The URL upon which relying party discovery should be run
            in order to verify the return_to URL

        @since: 2.1.0
        rA  wwwz	%s://%s%s)r?  r:  rW  r4  r6  r>  )r  
www_domainr   r   r!  buildDiscoveryURLB  s
   
zTrustRoot.buildDiscoveryURLc                 C   s    d| j | j| j| j| j| jf S )Nz!TrustRoot(%r, %r, %r, %r, %r, %r)r=  r#  r   r   r!  __repr__W  s   zTrustRoot.__repr__c                 C   s   t | S r  )reprr#  r   r   r!  r$  \  s   zTrustRoot.__str__N)r%  r&  r'  r(  r"  rJ  rS  r[  classmethodr]  r^  ra  rb  r$  r   r   r   r!  r   t   s    694r   z*http://specs.openid.net/auth/2.0/return_toc                 C   s   |  tgr	| jS dS )a  If the endpoint is a relying party OpenID return_to endpoint,
    return the endpoint URL. Otherwise, return None.

    This function is intended to be used as a filter for the Yadis
    filtering interface.

    @see: C{L{openid.yadis.services}}
    @see: C{L{openid.yadis.filters}}

    @param endpoint: An XRDS BasicServiceEndpoint, as returned by
        performing Yadis dicovery.

    @returns: The endpoint URL or None if the endpoint is not a
        relying party endpoint.
    @rtype: str or NoneType
    N)
matchTypesr   uri)endpointr   r   r!  _extractReturnURLg  s   rh  c                 C   s6   | D ]}t |}|dur|js||r dS qdS )zbIs the return_to URL under one of the supplied allowed
    return_to URLs?

    @since: 2.1.0
    NTF)r   r[  r?  rS  )allowed_return_to_urls	return_toallowed_return_toreturn_realmr   r   r!  r   ~  s   
r   c                 C   s&   t | t\}}|| krt| ||S )z\Given a relying party discovery URL return a list of return_to URLs.

    @since: 2.1.0
    )r   getServiceEndpointsrh  r  )r  r  return_to_urlsr   r   r!  getAllowedReturnURLs  s   ro  c              
   C   s   t | }|du rdS z|| }W n ty- } ztt| W Y d}~dS d}~ww t||r5dS td|| |f  dS )a  Verify that a return_to URL is valid for the given realm.

    This function builds a discovery URL, performs Yadis discovery on
    it, makes sure that the URL does not redirect, parses out the
    return_to URLs, and finally checks to see if the current return_to
    URL matches the return_to.

    @raises DiscoveryFailure: When Yadis discovery fails
    @returns: True if the return_to URL is valid for the realm

    @since: 2.1.0
    NFTz;Failed to validate return_to %r for realm %r, was not in %s)	r   r[  ra  r  logger	exceptionstrr   error)	realm_strrj  _vrfyrealmallowable_urlserrr   r   r!  r     s    

r   )r(  __all__openidr   openid.yadisr   urllib.parser	   r
   r   logging	getLoggerr%  rp  rU  rF  compiler2  	Exceptionr  r<  objectr   r   rh  r   ro  r   r   r   r   r!  <module>   s,   

" q