a
    xd.?                     @   s2  d Z g dZddlZddlZddlmZmZ ddlmZ ddl	m
Z
mZmZ ddl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 dZdZdZdZdZddlm Z! ddlm"Z# e$e%Z&G dd de'Z(dd Z)dd Z*dd Z+dd Z,dd Z-d d! Z.d"d# Z/d$d% Z0d&d' Z1d(d) ZdS )*z9Functions to discover OpenID endpoints from identifiers.
)DiscoveryFailureOPENID_1_0_NSOPENID_1_0_TYPEOPENID_1_1_TYPEOPENID_2_0_TYPEOPENID_IDP_2_0_TYPEOpenIDServiceEndpointdiscover    N)fetchersurinorm)yadis)nsTag	XRDSError
XRD_NS_2_0)applyFilter)r   )r   )xriresfiltersxri)
html_parsezhttp://openid.net/xmlns/1.0z'http://specs.openid.net/auth/2.0/serverz'http://specs.openid.net/auth/2.0/signonzhttp://openid.net/signon/1.1zhttp://openid.net/signon/1.0)
OPENID1_NS)
OPENID2_NSc                   @   s   e Zd ZdZeeeegZdd Z	dd Z
dd Zdd	 Zd
d Zd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eeZdd ZeeZdd Zd S )!r   zObject representing an OpenID service endpoint.

    @ivar identity_url: the verified identifier.
    @ivar canonicalID: For XRI, the persistent identifier.
    c                 C   s.   d | _ d | _g | _d | _d | _d| _d | _d S )NF)
claimed_id
server_url	type_urislocal_idcanonicalID
used_yadisdisplay_identifierself r!   P/var/www/html/Ranjet/env/lib/python3.9/site-packages/openid/consumer/discover.py__init__;   s    zOpenIDServiceEndpoint.__init__c                 C   s
   || j v S N)r   )r    Zextension_urir!   r!   r"   usesExtensionD   s    z#OpenIDServiceEndpoint.usesExtensionc                 C   s    t | jv st| jv rtS tS d S r$   )r   r   r   OPENID_2_0_MESSAGE_NSOPENID_1_0_MESSAGE_NSr   r!   r!   r"   preferredNamespaceG   s
    
z(OpenIDServiceEndpoint.preferredNamespacec                 C   s   || j v p|tko|  S )zyDoes this endpoint support this type?

        I consider C{/server} endpoints to implicitly support C{/signon}.
        )r   r   isOPIdentifier)r    type_urir!   r!   r"   supportsTypeN   s    
z"OpenIDServiceEndpoint.supportsTypec                 C   s4   | j dur| j S | jdu rdS tj| jd S dS )zJReturn the display_identifier if set, else return the claimed_id.
        Nr	   )r   r   urllibparse	urldefragr   r!   r!   r"   getDisplayIdentifierV   s
    

z*OpenIDServiceEndpoint.getDisplayIdentifierc                 C   s   |   tkS r$   )r(   r&   r   r!   r!   r"   compatibilityMode`   s    z'OpenIDServiceEndpoint.compatibilityModec                 C   s
   t | jv S r$   )r   r   r   r!   r!   r"   r)   c   s    z$OpenIDServiceEndpoint.isOPIdentifierc                 C   s2   || _ || _d| _|  s.t|| j | _|| _dS )zRSet the state of this object based on the contents of the
        service element.TN)r   r   r   r)   findOPLocalIdentifierr   r   )r    	yadis_urlurir   service_elementr!   r!   r"   parseServicef   s    z"OpenIDServiceEndpoint.parseServicec                 C   s2   | j | j  u rdu r"n n| jS | j p,| jS dS )zaReturn the identifier that should be sent as the
        openid.identity parameter to the server.N)r   r   r   r   r!   r!   r"   
getLocalIDv   s    z OpenIDServiceEndpoint.getLocalIDc                 C   sB   | | j}|r:|jdur:|  }||j|j|j|j nd}|S )zCreate a new instance of this class from the endpoint
        object passed in.

        @return: None or OpenIDServiceEndpoint for this endpoint objectN)Z
matchTypesopenid_type_urisr3   r5   r2   r   r4   )clsendpointr   Zopenid_endpointr!   r!   r"   fromBasicServiceEndpoint   s    z.OpenIDServiceEndpoint.fromBasicServiceEndpointc                 C   s~   t ddftddfg}t|}g }|D ]R\}}}t||}	|	du rFq&|  }
||
_t|||
_|	|
_|g|
_|	|
 q&|S )zParse the given document as HTML looking for an OpenID <link
        rel=...>

        @rtype: [OpenIDServiceEndpoint]
        zopenid2.providerzopenid2.local_idzopenid.serverzopenid.delegateN)
r   r   r   ZparseLinkAttrsZfindFirstHrefr   r   r   r   append)r8   r3   htmlZdiscovery_typesZ
link_attrsservicesr*   Zop_endpoint_relZlocal_id_relop_endpoint_urlservicer!   r!   r"   fromHTML   s(    
zOpenIDServiceEndpoint.fromHTMLc                 C   s   t ||| S )zParse the given document as XRDS looking for OpenID services.

        @rtype: [OpenIDServiceEndpoint]

        @raises XRDSError: When the XRDS does not parse.

        @since: 2.1.0
        )extractServices)r8   r3   Zxrdsr!   r!   r"   fromXRDS   s    	zOpenIDServiceEndpoint.fromXRDSc                 C   s$   |  r| j}n| j}||j|jS )zCreate endpoints from a DiscoveryResult.

        @type discoveryResult: L{DiscoveryResult}

        @rtype: list of L{OpenIDServiceEndpoint}

        @raises XRDSError: When the XRDS does not parse.

        @since: 2.1.0
        )isXRDSrB   r@   normalized_uriresponse_text)r8   ZdiscoveryResultmethodr!   r!   r"   fromDiscoveryResult   s    z)OpenIDServiceEndpoint.fromDiscoveryResultc                 C   s   |  }||_ tg|_|S )zConstruct an OP-Identifier OpenIDServiceEndpoint object for
        a given OP Endpoint URL

        @param op_endpoint_url: The URL of the endpoint
        @rtype: OpenIDServiceEndpoint
        )r   r   r   )r8   r>   r?   r!   r!   r"   fromOPEndpointURL   s    z'OpenIDServiceEndpoint.fromOPEndpointURLc                 C   s(   d| j j| j j| j| j| j| j| jf S )NzM<%s.%s server_url=%r claimed_id=%r local_id=%r canonicalID=%r used_yadis=%s >)	__class__
__module____name__r   r   r   r   r   r   r!   r!   r"   __str__   s    zOpenIDServiceEndpoint.__str__N)rK   rJ   __qualname____doc__r   r   r   r   r7   r#   r%   r(   r+   r/   r0   r)   r5   r6   r:   classmethodr@   rB   rG   rH   rL   r!   r!   r!   r"   r   +   s4   		
r   c                 C   s   g }t |v st|v r$|ttd t|v r<|ttd d}|D ]F}| |D ]6}|du rf|j}qR||jkrRd}||f }t	|dqRqD|S )a  Find the OP-Local Identifier for this xrd:Service element.

    This considers openid:Delegate to be a synonym for xrd:LocalID if
    both OpenID 1.X and OpenID 2.0 types are present. If only OpenID
    1.X is present, it returns the value of openid:Delegate. If only
    OpenID 2.0 is present, it returns the value of xrd:LocalID. If
    there is more than one LocalID tag and the values are different,
    it raises a DiscoveryFailure. This is also triggered when the
    xrd:LocalID and openid:Delegate tags are different.

    @param service_element: The xrd:Service element
    @type service_element: ElementTree.Node

    @param type_uris: The xrd:Type values present in this service
        element. This function could extract them, but higher level
        code needs to do that anyway.
    @type type_uris: [str]

    @raises DiscoveryFailure: when discovery fails.

    @returns: The OP-Local Identifier for this service element, if one
        is present, or None otherwise.
    @rtype: str or unicode or NoneType
    ZDelegateZLocalIDNz1More than one %r tag found in one service element)
r   r   r;   r   r   r   r   findalltextr   )r4   r   Zlocal_id_tagsr   Zlocal_id_tagZlocal_id_elementformatmessager!   r!   r"   r1      s    

r1   c              
   C   sX   zt  | }W n4 tyB } ztd|f dW Y d}~nd}~0 0 tj|d S dS )zJNormalize a URL, converting normalization failures to
    DiscoveryFailurezNormalizing identifier: %sNr	   )r   
ValueErrorr   r,   r-   r.   )url
normalizedwhyr!   r!   r"   normalizeURL$  s
    &rX   c                 C   s   |  dr| dd } | S )z1Normalize an XRI, stripping its scheme if presentzxri://   N)
startswithr   r!   r!   r"   normalizeXRI/  s    
r[   c                    sZ   dd fdd  fdd| D }|   tt|D ]}|| d ||< q@|S )zzRearrange service_list in a new list so services are ordered by
    types listed in preferred_types.  Return the new list.c                 S   s   t tt tt| | S )zvReturn an iterable that pairs the index of an element with
        that element.

        For Python 2.2 compatibility)listziprangelen)eltsr!   r!   r"   	enumerate:  s    z arrangeByType.<locals>.enumeratec                    s0    D ]\}}| | j v r|  S qtS )au  Return the index of the first matching type, or something
        higher if no type matches.

        This provides an ordering in which service elements that
        contain a type that comes earlier in the preferred types list
        come before service elements that come later. If a service
        element has more than one type, the most preferred one wins.
        )r   r_   )r?   it)ra   preferred_typesr!   r"   bestMatchingServiceA  s    	
z*arrangeByType.<locals>.bestMatchingServicec                    s   g | ]\}} |||fqS r!   r!   ).0Z
orig_indexs)re   r!   r"   
<listcomp>R  s   z!arrangeByType.<locals>.<listcomp>   )sortr^   r_   )Zservice_listrd   Zprio_servicesrb   r!   )re   ra   rd   r"   arrangeByType6  s    
rk   c                 C   s    t | tg}t | tj} |p| S )a  Extract OP Identifier services.  If none found, return the
    rest, sorted with most preferred first according to
    OpenIDServiceEndpoint.openid_type_uris.

    openid_services is a list of OpenIDServiceEndpoint objects.

    Returns a list of OpenIDServiceEndpoint objects.)rk   r   r   r7   )openid_servicesZop_servicesr!   r!   r"   getOPOrUserServices^  s
    	rm   c                 C   sh   t | }|j}|j}zt||}W n ty:   g }Y n0 |s\| rPt| S t||}|t	|fS )aD  Discover OpenID services for a URI. Tries Yadis and falls back
    on old-style <link rel='...'> discovery if Yadis fails.

    @param uri: normalized identity URL
    @type uri: str

    @return: (claimed_id, services)
    @rtype: (str, list(OpenIDServiceEndpoint))

    @raises DiscoveryFailure: when discovery fails.
    )
yadisDiscoverrD   rE   r   rB   r   rC   discoverNoYadisr@   rm   )r3   responser2   bodyrl   r!   r!   r"   discoverYadiso  s    
rr   c                 C   s   g }t | } zVt | tj\}}|d u r:td| f tt}|D ]}|	|
| | qHW n  ty   td|   Y n0 |D ]}||_||_| |_q| t|fS )NzNo CanonicalID found for XRI %rzxrds error on )r[   r   ZProxyResolverqueryr   r7   r   r   ZmkFilterextendZgetServiceEndpointslogger	exceptionr   r   r   rm   )ZinameZ	endpointsr   r=   Zfltr4   r9   r!   r!   r"   discoverXRI  s$    
rw   c                 C   sB   t | }|jdvr&td|jf ||j}t||j}||fS )N)      zEHTTP Response status from identity URL host is not 200. Got status %r)r
   fetchstatusr   Z	final_urlr   r@   rq   )r3   Z	http_respr   rl   r!   r!   r"   ro     s    

ro   c                 C   s`   t j| }|d r4|d r4|d dvr<tdd nd|  } t| } t| \}}t|}||fS )Nr	      )httphttpszURI scheme is not HTTP or HTTPSzhttp://)r,   r-   urlparser   rX   rr   )r3   parsedr   rl   r!   r!   r"   discoverURI  s    r   c                 C   s"   t | dkrt| S t| S d S )NZXRI)r   ZidentifierSchemerw   r   )
identifierr!   r!   r"   r     s    r   )2rN   __all__urllib.parser,   loggingZopenidr
   r   r   Zopenid.yadis.etxrdr   r   r   Zopenid.yadis.servicesr   rA   Zopenid.yadis.discoverr   rn   r   Zopenid.yadisr   r   r   Zopenid.consumerr   r   r   r   r   r   Zopenid.messager   r'   r   r&   	getLoggerrK   ru   objectr   r1   rX   r[   rk   rm   rr   rw   ro   r   r!   r!   r!   r"   <module>   s@   
 H2(*