o
    ˷e.?                     @   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!   O/var/www/ideatree/venv/lib/python3.10/site-packages/openid/consumer/discover.py__init__;   s   
zOpenIDServiceEndpoint.__init__c                 C   s
   || j v S N)r   )r    extension_urir!   r!   r"   usesExtensionD      
z#OpenIDServiceEndpoint.usesExtensionc                 C   s   t | jv s
t| jv rtS t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   s0   | j dur| j S | jdu rdS tj| j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   r'   z$OpenIDServiceEndpoint.isOPIdentifierc                 C   s6   || _ || _d| _|  st|| j | _|| _dS 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   s*   | j | j  u rdu r| jS  | j p| j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   sD   | | j}|r|jdur|  }||j|j|j|j |S d}|S )zCreate a new instance of this class from the endpoint
        object passed in.

        @return: None or OpenIDServiceEndpoint for this endpoint objectN)
matchTypesopenid_type_urisr5   r7   r4   r   r6   )clsendpointr   openid_endpointr!   r!   r"   fromBasicServiceEndpoint   s   z.OpenIDServiceEndpoint.fromBasicServiceEndpointc                 C   s~   t ddftddfg}t|}g }|D ])\}}}t||}	|	du r#q|  }
||
_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   parseLinkAttrsfindFirstHrefr   r   r   r   append)r;   r5   htmldiscovery_types
link_attrsservicesr,   op_endpoint_rel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)r;   r5   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
        )isXRDSrM   rJ   normalized_uriresponse_text)r;   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   )r;   rH   rI   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)rW   rV   __qualname____doc__r   r   r   r   r:   r#   r&   r*   r-   r1   r2   r+   r7   r8   r>   classmethodrJ   rM   rS   rT   rX   r!   r!   r!   r"   r   +   s6    		
r   c                 C   s   g }t |v s
t|v r|ttd t|v r|ttd d}|D ]#}| |D ]}|du r3|j}q)||jkrDd}||f }t	|dq)q"|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
    DelegateLocalIDNz1More than one %r tag found in one service element)
r   r   rA   r   r   r   r   findalltextr   )r6   r   local_id_tagsr   local_id_taglocal_id_elementformatmessager!   r!   r"   r3      s"   


r3   c              
   C   sH   zt  | }W n ty } ztd|f dd}~ww tj|d S )zJNormalize a URL, converting normalization failures to
    DiscoveryFailurezNormalizing identifier: %sNr	   )r   
ValueErrorr   r.   r/   r0   )url
normalizedwhyr!   r!   r"   normalizeURL$  s   ri   c                 C   s   |  dr| dd } | S )z1Normalize an XRI, stripping its scheme if presentzxri://   N)
startswithr   r!   r!   r"   normalizeXRI/  s   
rl   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   rp   )rI   it)rr   preferred_typesr!   r"   bestMatchingServiceA  s
   	z*arrangeByType.<locals>.bestMatchingServicec                    s   g | ]\}} |||fqS r!   r!   ).0
orig_indexs)rv   r!   r"   
<listcomp>R  s    z!arrangeByType.<locals>.<listcomp>   )sortro   rp   )service_listru   prio_servicesrs   r!   )rv   rr   ru   r"   arrangeByType6  s   
r   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.)r   r   r   r:   )openid_servicesop_servicesr!   r!   r"   getOPOrUserServices^  s
   	r   c                 C   sh   t | }|j}|j}zt||}W n ty   g }Y nw |s.| r(t| 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.
    )
yadisDiscoverrO   rP   r   rM   r   rN   discoverNoYadisrJ   r   )r5   responser4   bodyr   r!   r!   r"   discoverYadiso  s   r   c                 C   s   g }t | } z+t | tj\}}|d u rtd| f tt}|D ]}|	|
| | q$W n tyA   td|   Y nw |D ]}||_||_| |_qD| t|fS )NzNo CanonicalID found for XRI %rzxrds error on )rl   r   ProxyResolverqueryr   r:   r   r   mkFilterextendgetServiceEndpointslogger	exceptionr   r   r   r   )iname	endpointsr   rE   fltr6   r<   r!   r!   r"   discoverXRI  s(   
r   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   	final_urlr   rJ   r   )r5   	http_respr   r   r!   r!   r"   r     s   

r   c                 C   s`   t j| }|d r|d r|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   ri   r   )r5   parsedr   r   r!   r!   r"   discoverURI  s   
r   c                 C   s   t | dkrt| S t| S )NXRI)r   identifierSchemer   r   )
identifierr!   r!   r"   r     s   r   )2rZ   __all__urllib.parser.   loggingopenidr
   r   r   openid.yadis.etxrdr   r   r   openid.yadis.servicesr   rK   openid.yadis.discoverr   r   r   openid.yadisr   r   r   openid.consumerr   r   r   r   r   r   openid.messager   r)   r   r(   	getLoggerrW   r   objectr   r3   ri   rl   r   r   r   r   r   r   r!   r!   r!   r"   <module>   sB   
 H2(*