
    )Jf.?                     B   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j$        e%          Z& G d de'          Z(d Z)d Z*d Z+d Z,d Z-d Z.d Z/d Z0d Z1d 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                       e Zd ZdZeeeegZd Z	d Z
d Zd Zd Zd Zd Zd	 Zd
 Zd Z ee          Zd Z ee          Zd Z ee          Zd Z ee          Zd Z ee          Z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                 h    d | _         d | _        g | _        d | _        d | _        d| _        d | _        d S )NF)
claimed_id
server_url	type_urislocal_idcanonicalID
used_yadisdisplay_identifierselfs    [/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/openid/consumer/discover.py__init__zOpenIDServiceEndpoint.__init__;   s;    "&    c                     || j         v S N)r   )r#   extension_uris     r$   usesExtensionz#OpenIDServiceEndpoint.usesExtensionD   s    ..r&   c                 V    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"   s    r$   preferredNamespacez(OpenIDServiceEndpoint.preferredNamespaceG   s(    4>114>11((((r&   c                 R    || 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_uris     r$   supportsTypez"OpenIDServiceEndpoint.supportsTypeN   s3    
 T^+ H_,F1D1D1F1F	Ir&   c                     | j         | j         S | j        dS t          j                            | j                  d         S )zJReturn the display_identifier if set, else return the claimed_id.
        Nr
   )r!   r   urllibparse	urldefragr"   s    r$   getDisplayIdentifierz*OpenIDServiceEndpoint.getDisplayIdentifierV   s@     ".**?"4<))$/::1==r&   c                 <    |                                  t          k    S r(   )r.   r,   r"   s    r$   compatibilityModez'OpenIDServiceEndpoint.compatibilityMode`   s    &&((,AAAr&   c                     t           | j        v S r(   )r   r   r"   s    r$   r0   z$OpenIDServiceEndpoint.isOPIdentifierc   s    "dn44r&   c                     || _         || _        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    r0   findOPLocalIdentifierr   r   )r#   	yadis_urlurir   service_elements        r$   parseServicez"OpenIDServiceEndpoint.parseServicef   s^     #""$$ 	(
 2/26.B BDM'DOOO	( 	(r&   c                 T    | j         | j        cxu r
n n| j        S | j         p| j        S )zaReturn the identifier that should be sent as the
        openid.identity parameter to the server.)r   r   r   r"   s    r$   
getLocalIDz OpenIDServiceEndpoint.getLocalIDv   s>     MT-55555555?"=4D$44r&   c                     |                     | j                  }|r>|j        7 |             }|                    |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)
matchTypesopenid_type_urisr>   r@   r=   r   r?   )clsendpointr   openid_endpoints       r$   fromBasicServiceEndpointz.OpenIDServiceEndpoint.fromBasicServiceEndpoint   sy    
 ''(<==	  	#1!ceeO(();X\)1);)1)AC C C C #Or&   c                 :   t           ddft          ddfg}t          j        |          }g }|D ]m\  }}}t          j        ||          }	|	 |             }
||
_        t          j        ||          |
_        |	|
_        |g|
_        |	                    |
           n|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.delegate)
r   r   r   parseLinkAttrsfindFirstHrefr   r   r   r   append)rF   r>   htmldiscovery_types
link_attrsservicesr1   op_endpoint_rellocal_id_relop_endpoint_urlservices              r$   fromHTMLzOpenIDServiceEndpoint.fromHTML   s     02DEo/@A

  .t44
7F 	% 	%3Ho|(6z7FH HO&ceeG!$G)7
8D F  FG!0G!)
GOOG$$$$r&   c                 $    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)rF   r>   xrdss      r$   fromXRDSzOpenIDServiceEndpoint.fromXRDS   s     sD#...r&   c                 t    |                                 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
        )isXRDSrZ   rV   normalized_uriresponse_text)rF   discoveryResultmethods      r$   fromDiscoveryResultz)OpenIDServiceEndpoint.fromDiscoveryResult   sG     !!## 	"\FF\Fvo4%35 5 	5r&   c                 B     |             }||_         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   )rF   rT   rU   s      r$   fromOPEndpointURLz'OpenIDServiceEndpoint.fromOPEndpointURL   s'     #%%,01r&   c                     d| j         j        d| j         j        d| j        d| j        d| j        d| j        d| j        dS )	N<.z server_url=z claimed_id=z
 local_id=z canonicalID=z used_yadis=z >)	__class__
__module____name__r   r   r   r   r    r"   s    r$   __str__zOpenIDServiceEndpoint.__str__   sX      ~000$.2I2I2I''': 	;r&   N)ri   rh   __qualname____doc__r   r   r   r   rE   r%   r*   r.   r2   r7   r9   r0   r@   rB   rI   classmethodrV   rZ   ra   rc   rj    r&   r$   r   r   +   sv         		' ' '/ / /) ) )I I I> > >B B B5 5 5( ( ( 
5 
5 
5  &  +{+CDD  < {8$$H	/ 	/ 	/ {8$$H5 5 5$ &+&9::
 
 
 $$566	; 	; 	; 	; 	;r&   r   c                 x   g }t           |v s	t          |v r(|                    t          t          d                     t
          |v r(|                    t          t          d                     d}|D ]G}|                     |          D ]/}||j        }||j        k    rd}||fz  }t          |d          0H|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   rM   r   r   r   r   findalltextr   )r?   r   local_id_tagsr   local_id_taglocal_id_elementformatmessages           r$   r<   r<      s    8 M9$$9(D(DU=*==>>>)##U:y99::: H% 6 6 / 7 7 E E 	6 	6+0-222L L#33&w555 3	6 Or&   c                     	 t          j         |           }t          j                            |          d         S # t          $ r}t          d|d          d}~ww xY w)zJNormalize a URL, converting normalization failures to
    DiscoveryFailurer
   zNormalizing identifier: N)r   r4   r5   r6   
ValueErrorr   )url
normalizedwhys      r$   normalizeURLr~   $  sp    5_S))
 |%%j11!44  M M MssEtLLLMs   ; 
AAAc                 :     | j         d          r
| dd         } | S )z1Normalize an XRI, stripping its scheme if presentzxri://   N)
startswithr   s    r$   normalizeXRIr   /  s(    s~h !""gJr&   c                     d fdfd |           D             }|                                  t          t          |                    D ]}||         d         ||<   |S )zzRearrange service_list in a new list so services are ordered by
    types listed in preferred_types.  Return the new list.c                     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)eltss    r$   	enumeratez arrangeByType.<locals>.enumerate:  s2    
 CU3t99--..55666r&   c                 j               D ]\  }}|         | j         v r|c S 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   )rU   itr   preferred_typess      r$   bestMatchingServicez*arrangeByType.<locals>.bestMatchingServiceA  sS     Io.. 	 	DAqq!W%666 7 ?###r&   c                 2    g | ]\  }} |          ||fS rn   rn   ).0
orig_indexsr   s      r$   
<listcomp>z!arrangeByType.<locals>.<listcomp>R  sE     E E E(*a *)!,,j!< E E Er&      )sortr   r   )service_listr   prio_servicesr   r   r   s    `  @@r$   arrangeByTyper   6  s    7 7 7$ $ $ $ $ $"E E E E,5Il,C,CE E EM 3}%%&& / /(+A.ar&   c                 j    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   rE   )openid_servicesop_servicess     r$   getOPOrUserServicesr   ^  s=      2E1FGGK#O$9$JL LO )/)r&   c                 <   t          |           }|j        }|j        }	 t                              ||          }n# t
          $ r g }Y nw xY 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.
    )
yadisDiscoverr]   r^   r   rZ   r   r\   discoverNoYadisrV   r   )r>   responser=   bodyr   s        r$   discoverYadisr   o  s      S!!H'I!D/88DII     J ?? 	( #3''' 088DII*?;;<<s   ; A
	A
c                    g }t          |           } 	 t          j                                        | t          j                  \  }}|t          d|           t          j        t                    }|D ]+}|	                    |
                    | |                     ,n-# t          $ r  t                              d| z              Y nw xY w|D ]}||_        ||_        | |_        | t!          |          fS )NzNo CanonicalID found for XRI zxrds error on )r   r   ProxyResolverqueryr   rE   r   r   mkFilterextendgetServiceEndpointslogger	exceptionr   r   r!   r   )iname	endpointsr   rQ   fltr?   rG   s          r$   discoverXRIr     s0   IE3 & 4 6 6 < <(9!; !;X )IJJJ455' 	N 	NOS44UOLLMMMM	N 3 3 3)E1222223  , ,  +)&+## %i0000s   BB# #'CCc                     t          j        |           }|j        dvrt          d|j        |          |j        }t
                              ||j                  }||fS )N)      zCHTTP Response status from identity URL host is not 200. Got status )r   fetchstatusr   	final_urlr   rV   r   )r>   	http_respr   r   s       r$   r   r     sx    s##Iz))(//34=? ? 	? $J+44Z5>^E EO&&r&   c                    t           j                            |           }|d         r#|d         r|d         dvrt          dd           nd| z   } t	          |           } t          |           \  }}t	          |          }||fS )Nr
      )httphttpszURI scheme is not HTTP or HTTPSzhttp://)r4   r5   urlparser   r~   r   )r>   parsedr   r   s       r$   discoverURIr     s    \""3''Fay VAY !9---"#DdKKK . #o
s

C"/"4"4Jj))J&&r&   c                 n    t          j        |           dk    rt          |           S t          |           S )NXRI)r   identifierSchemer   r   )
identifiers    r$   r	   r	     s4    
J''500:&&&:&&&r&   )2rl   __all__urllib.parser4   loggingopenidr   r   r   openid.yadis.etxrdr   r   r   openid.yadis.servicesr   rX   openid.yadis.discoverr	   r   r   openid.yadisr   r   r   openid.consumerr   r   r   r   r   r   openid.messager   r-   r   r,   	getLoggerri   r   objectr   r<   r~   r   r   r   r   r   r   r   rn   r&   r$   <module>r      s   	 	 	      $ $ $ $ $ $ $ $       ; ; ; ; ; ; ; ; ; ; @ @ @ @ @ @ ; ; ; ; ; ; 2 2 2 2 2 2 ( ( ( ( ( ( ( (       & & & & & &-? ;00 > > > > > > > > > > > >		8	$	$D; D; D; D; D;F D; D; D;N/ / /d5 5 5  % % %P* * *"'= '= '=T1 1 16
' 
' 
'' ' '' ' ' ' 'r&   