
    f.?                     >   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jH                  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y) 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y)r   zObject representing an OpenID service endpoint.

    @ivar identity_url: the verified identifier.
    @ivar canonicalID: For XRI, the persistent identifier.
    c                 f    d | _         d | _        g | _        d | _        d | _        d| _        d | _        y )NF)
claimed_id
server_url	type_urislocal_idcanonicalID
used_yadisdisplay_identifierselfs    S/var/www/cs2snipe.com/venv/lib/python3.12/site-packages/openid/consumer/discover.py__init__zOpenIDServiceEndpoint.__init__;   s5    "&    c                     || j                   v S N)r   )r#   extension_uris     r$   usesExtensionz#OpenIDServiceEndpoint.usesExtensionD   s    ..r&   c                 b    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4>>1((((r&   c                 X    || j                   v xs |t        k(  xr | j                         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   s2    
 T^^+ H_,F1D1D1F	Ir&   c                     | j                   | j                   S | j                  yt        j                  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   sG     "".***??"<<))$//:1==r&   c                 0    | j                         t        k7  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    "dnn44r&   c                     || _         || _        d| _        | j                         s#t	        || j                         | _        || _        yy)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   sI     #""$
 2/26..BDM'DO %r&   c                     | j                   | j                  cxu r| j                  S  | j                   xs | 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<     MMT--5??" 6 ==4D$4$44r&   c                     |j                  | j                        }|rQ|j                  E |        }|j                  |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_urisr>   r@   r=   r   r?   )clsendpointr   openid_endpoints       r$   fromBasicServiceEndpointz.OpenIDServiceEndpoint.fromBasicServiceEndpoint   ss    
 ''(<(<=	 1!eO((););X\\)1););)1)A)AC  #Or&   c                 2   t         ddft        ddfg}t        j                  |      }g }|D ]h  \  }}}t        j                  ||      }	|	  |        }
||
_        t        j                  ||      |
_        |	|
_        |g|
_        |j                  |
       j |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
7F 	%3Ho|(66z7FHO&eG!$G)77
8D 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                     |j                         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   s@     !!#\\F\\Fo44%335 	5r&   c                 :     |        }||_         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__   sI     ~~00$..2I2I'': 	;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   +   s     		'/)I>B5( 
5&  ++CD< 8$H	/ 8$H5$ &&9:
 $$56	;r&   r   c                 X   g }t         |v st        |v r|j                  t        t        d             t
        |v r|j                  t        t        d             d}|D ]J  }| j                  |      D ]4  }||j                  }||j                  k7  s"d}||fz  }t        |d       L |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U=*=>)#U:y9: H% 6 / 7 7 E 	6+00-222L L#33&w55	66 Or&   c                     	 t        j                   |       }t        j                  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~   $  sU    5__S)
 ||%%j1!44  MsEtLLMs   9 	AAAc                 4     | 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} |       D cg c]  \  }} ||      ||f }}}|j                          t        t        |            D ]  }||   d   ||<    |S c c}}w )zzRearrange service_list in a new list so services are ordered by
    types listed in preferred_types.  Return the new list.c                 b    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:  s$    
 CU3t9-.566r&   c                 d           D ]  \  }}|   | j                   v s|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  sB     o. 	DAqq!W%6%66	 ?##r&      )sortr   r   )service_listr   r   
orig_indexsprio_servicesr   r   s    `     @r$   arrangeByTyper   6  s    7$$ -6l,CE(*a *!,j!< EM E 3}%& /(+A.a/ Es   A%c                 d    t        | t        g      }t        | t        j                        } |xs | 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   ^  s7      2E1FGK#O$9$J$JLO )/)r&   c                    t        |       }|j                  }|j                  }	 t        j	                  ||      }|s1|j                         rt        |       S t        j                  ||      }|t        |      fS # t
        $ r g }Y Mw xY w)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
  ?? #3'' 088DI*?;<<#  s   A; ;B	B	c                    g }t        |       } 	 t        j                         j                  | t        j
                        \  }}|t        d|       t        j                  t              }|D ]#  }|j                  |j                  | |             % 	 |D ]  }|_        ||_        | |_         | t!        |      fS # t        $ r t        j                  d| z          Y Lw xY w)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     s    IE3 & 4 4 6 < <(99!;X IJJ45' 	NOS44UOLM	N
  ,  +)&+#, %i000  3)E123s   BB? ?!C#"C#c                     t        j                  |       }|j                  dvrt        d|j                  |      |j                  }t
        j                  ||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     sn    s#Iz)(//34=? 	? $$J+44Z5>^^EO&&r&   c                     t         j                  j                  |       }|d   r|d   r|d   dvrt        dd       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     su    \\""3'FayVAY!9--"#DdKK#o
s
C"/"4Jj)J&&r&   c                 ^    t        j                  |       dk(  rt        |       S t        |       S )NXRI)r   identifierSchemer   r   )
identifiers    r$   r	   r	     s+    
J'5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 (  &-? ;00 > >			8	$D;F D;N/d5%P*"'=T16
'''r&   