o
    ˷eX                     @   s  d Z g dZddlZddlZddlZddlZddlmZ ddlm	Z	 ze
 ZW n ey3   dZY nw dZdZdZd	ZeefZd
ZedZedZedZdZg dZG dd deZG dd deZe Zi aG dd deZ dd Z!G dd deZ"G dd deZ#dS )z#Extension argument processing code
)
MessageNamespaceMap
no_defaultregisterNamespaceAlias	OPENID_NSBARE_NS
OPENID1_NS
OPENID2_NSSREG_URIIDENTIFIER_SELECT    N)oidutil)kvformz2http://specs.openid.net/auth/2.0/identifier_selectzhttp://openid.net/sreg/1.0zhttp://openid.net/signon/1.0zhttp://openid.net/signon/1.1z http://specs.openid.net/auth/2.0zNull namespacezOpenID namespacezBare namespacei  )nsmodeerror	return_tocontact	referencesigned
assoc_typesession_type
dh_modulusdh_gendh_consumer_public
claimed_ididentityrealminvalidate_handleop_endpointresponse_noncesigassoc_handle
trust_rootopenidc                   @      e Zd ZdZdS )UndefinedOpenIDNamespaceznRaised if the generic OpenID namespace is accessed when there
    is no OpenID namespace set for this message.N__name__
__module____qualname____doc__ r+   r+   E/var/www/ideatree/venv/lib/python3.10/site-packages/openid/message.pyr%   Q   s    r%   c                   @   s   e Zd ZdZdd ZdS )InvalidOpenIDNamespacezxRaised if openid.ns is not a recognized value.

    For recognized values, see L{Message.allowed_openid_namespaces}
    c                 C   s"   d}| j r|d| j d f 7 }|S )NzInvalid OpenID Namespacez %rr   args)selfsr+   r+   r,   __str__\   s   zInvalidOpenIDNamespace.__str__N)r'   r(   r)   r*   r2   r+   r+   r+   r,   r-   V   s    r-   c                   @   r$   )NamespaceAliasRegistrationErrorzL
    Raised when an alias or namespace URI has already been registered.
    Nr&   r+   r+   r+   r,   r3   l   s    r3   c                 C   sR   t || kr	dS | tt  v rtd| f |t v r#td|f | t |< dS )a:  
    Registers a (namespace URI, alias) mapping in a global namespace
    alias map.  Raises NamespaceAliasRegistrationError if either the
    namespace URI or alias has already been registered with a
    different value.  This function is required if you want to use a
    namespace with an OpenID 1 message.
    Nz#Namespace uri %r already registeredzAlias %r already registered)registered_aliasesgetlistvaluesr3   )namespace_urialiasr+   r+   r,   r   s   s   
r   c                   @   s  e Zd ZdZeeegZd>ddZe	dd Z
e	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dd Zdd Z		d?ddZd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd>d,d-Zd.d/ Zd0d1 Zd2d3 Z d4d5 Z!d6d7 Z"d8d9 Z#d:d; Z$d>d<d=Z%dS )@r   a  
    In the implementation of this object, None represents the global
    namespace as well as a namespace with no key.

    @cvar namespaces: A dictionary specifying specific
        namespace-URI to alias mappings that should be used when
        generating namespace aliases.

    @ivar ns_args: two-level dictionary of the values in this message,
        grouped by namespace URI. The first level is the namespace
        URI.
    Nc                 C   s8   i | _ t | _|du rd| _dS |tv }| || dS )zCreate an empty Message.

        @raises InvalidOpenIDNamespace: if openid_namespace is not in
            L{Message.allowed_openid_namespaces}
        N)r/   r   
namespaces_openid_ns_uriOPENID1_NAMESPACESsetOpenIDNamespace)r0   openid_namespaceimplicitr+   r+   r,   __init__   s   
zMessage.__init__c              	   C   s   |  }i }|  D ]6\}}t|trtd|f z
|dd\}}W n ty.   d}Y nw |dkr;||jt|f< q	|||< q	|| |S )zAConstruct a Message containing a set of POST arguments.

        zNquery dict must have one value for each key, not lists of values.  Query is %r.   Nr#   )	items
isinstancer6   	TypeErrorsplit
ValueErrorr/   r   _fromOpenIDArgs)clsr/   r0   openid_argskeyvalueprefixrestr+   r+   r,   fromPostArgs   s"   


zMessage.fromPostArgsc                 C   s   |  }| | |S )zConstruct a Message from a parsed KVForm message.

        @raises InvalidOpenIDNamespace: if openid.ns is not in
            L{Message.allowed_openid_namespaces}
        )rH   )rI   rJ   r0   r+   r+   r,   fromOpenIDArgs   s   
zMessage.fromOpenIDArgsc              	   C   s  g }|  D ]?\}}z
|dd\}}W n ty!   t}|}Y nw |dkr.| j|| q|tkr=|dkr=| |d q||||f q|  sP| t	d |D ]3\}}}| j
|}|d u r~| |}|d u ru|  }d||f }n	| jj||dd | ||| qRd S )NrA   rB   r   FT%s.%s)r?   )rC   rF   rG   NULL_NAMESPACEr:   addAliasr=   appendgetOpenIDNamespacer   getNamespaceURI_getDefaultNamespacesetArg)r0   rJ   ns_argsrN   rL   ns_aliasns_keyns_urir+   r+   r,   rH      s2   
zMessage._fromOpenIDArgsc                 C   s   |   r	t|S dS )zWOpenID 1 compatibility: look for a default namespace URI to
        use for this alias.N)	isOpenID1r4   r5   )r0   mystery_aliasr+   r+   r,   rW      s   
zMessage._getDefaultNamespacec                 C   sB   t |trt|dd}|| jvrt|| j|t| || _dS )zSet the OpenID namespace URI used in this message.

        @raises InvalidOpenIDNamespace: if the namespace is not in
            L{Message.allowed_openid_namespaces}
        utf-8encodingN)	rD   bytesstrallowed_openid_namespacesr-   r:   rS   rR   r;   )r0   openid_ns_urir?   r+   r+   r,   r=      s   


zMessage.setOpenIDNamespacec                 C   s   | j S N)r;   r0   r+   r+   r,   rU     s   zMessage.getOpenIDNamespacec                 C   s   |   tv S rf   )rU   r<   rg   r+   r+   r,   r]        zMessage.isOpenID1c                 C   s   |   tkS rf   )rU   r   rg   r+   r+   r,   	isOpenID2  rh   zMessage.isOpenID2c                 C   s   |  t|S )z%Create a Message from a KVForm string)rP   r   kvToDict)rI   kvform_stringr+   r+   r,   
fromKVForm  s   zMessage.fromKVFormc                 C   s
   t | S rf   )copydeepcopyrg   r+   r+   r,   rm        
zMessage.copyc                 C   s   i }| j  D ]\}}| j |rq|tkrd}nd| }t|||< q| j D ]\\}}}| ||}t|||< q*|S )zk
        Return all arguments with openid. in front of namespaced arguments.
        @return bytes
        z	openid.nsz
openid.ns.)r:   rC   
isImplicitrR   r   	toUnicoder/   getKey)r0   r/   r\   r9   r[   rL   rK   r+   r+   r,   
toPostArgs   s   zMessage.toPostArgsc                 C   sD   |   }i }| D ]\}}|dstd|||dd < q
|S )zWReturn all namespaced arguments, failing if any
        non-namespaced arguments exist.openid.znThis message can only be encoded as a POST, because it contains arguments that are not prefixed with "openid."   N)rs   rC   
startswithrG   )r0   	post_argskvargskvr+   r+   r,   toArgs8  s   
zMessage.toArgsContinuec           
      C   s   t du rtd|dusJ t d}|r#| D ]	\}}||j|< qt||jd< d|jd< d|jd< d	|jd
< |   D ]\}}dt|t|d}|t d| q@t ddt|d}	||	 t	t j
|ddddS )a:  Generate HTML form markup that contains the values in this
        message, to be HTTP POSTed as x-www-form-urlencoded UTF-8.

        @param action_url: The URL to which the form will be POSTed
        @type action_url: str

        @param form_tag_attrs: Dictionary of attributes to be added to
            the form tag. 'accept-charset' and 'enctype' have defaults
            that can be overridden. If a value is supplied for
            'action' or 'method', it will be replaced.
        @type form_tag_attrs: {unicode: unicode}

        @param submit_text: The text that will appear on the submit
            button for this form.
        @type submit_text: unicode

        @returns: A string containing (X)HTML markup for a form that
            encodes the values in this Message object.
        @rtype: str
        Nz#This function requires ElementTree.formactionpostmethodzUTF-8zaccept-charsetz!application/x-www-form-urlencodedenctypehidden)typenamerL   inputsubmit)r   rL   r_   r`   )ElementTreeRuntimeErrorElementrC   attribr   rq   rs   rT   rc   tostring)
r0   
action_urlform_tag_attrssubmit_textr}   r   attrrL   attrsr   r+   r+   r,   toFormMarkupH  s6   




zMessage.toFormMarkupc                 C   s   t ||  S )z\Generate a GET URL with the parameters in this message
        attached as query parameters.)r   
appendArgsrs   )r0   base_urlr+   r+   r,   toURL  s   zMessage.toURLc                 C   s   t |  S )zGenerate a KVForm string that contains the parameters in
        this message. This will fail if the message contains arguments
        outside of the 'openid.' prefix.
        )r   dictToKVr{   rg   r+   r+   r,   toKVForm  s   zMessage.toKVFormc                 C   s   t |   }tj|S )z#Generate an x-www-urlencoded string)sortedrs   rC   urllibparse	urlencode)r0   r/   r+   r+   r,   toURLEncoded  s   zMessage.toURLEncodedc                 C   s   t |trt|dd}|tkr| jdu rtd| j}|tkr+t |ts+td|f |tkrOd|vrOd}t	||f t
 |dkrOd	}t	|tf t
 tS |S )
zConvert an input value into the internally used values of
        this object

        @param namespace: The string or constant to convert
        @type namespace: str or unicode or BARE_NS or OPENID_NS
        r_   r`   NzOpenID namespace not setz8Namespace must be BARE_NS, OPENID_NS or a string. got %r:z7OpenID 2.0 namespace identifiers SHOULD be URIs. Got %rsregz'Using %r instead of "sreg" as namespace)rD   rb   rc   r   r;   r%   r   rE   warningswarnDeprecationWarningr	   )r0   	namespacefmtr+   r+   r,   _fixNS  s.   

zMessage._fixNSc                 C   s   |  |}||f| jv S rf   r   r/   )r0   r   r[   r+   r+   r,   hasKey  s   
zMessage.hasKeyc                 C   sV   |  |}|tkr|S | j|}|du rdS |tkr!|}d| S d||f }d| S )z0Get the key for a particular namespaced argumentNrQ   rt   )r   r   r:   getAliasrR   )r0   r   r[   rZ   tailr+   r+   r,   rr     s   
zMessage.getKeyc                 C   sJ   |  |}||f}z| j| W S  ty$   |tu r t||f| Y S w )a  Get a value for a namespaced key.

        @param namespace: The namespace in the message for this key
        @type namespace: str

        @param key: The key to get within this namespace
        @type key: str

        @param default: The value to use if this key is absent from
            this message. Using the special value
            openid.message.no_default will result in this method
            raising a KeyError instead of returning the default.

        @rtype: str or the type of default
        @raises KeyError: if default is no_default
        @raises UndefinedOpenIDNamespace: if the message has not yet
            had an OpenID namespace set
        )r   r/   KeyErrorr   )r0   r   rK   defaultargs_keyr+   r+   r,   getArg  s   
zMessage.getArgc                 C   s|   |  |}g }| j D ]-\\}}}||kr9t|tr"t|dd}n|}t|tr0t|dd}n|}|||f qt|S )zGet the arguments that are defined for this namespace URI

        @returns: mapping from namespaced keys to values
        @returntype: dict of {str:bytes}
        r_   r`   )r   r/   rC   rD   rb   rc   rT   dict)r0   r   r/   pair_nsr[   rL   ry   rz   r+   r+   r,   getArgs  s   


zMessage.getArgsc                 C   s.   |  |}| D ]\}}| ||| q	dS )zSet multiple key/value pairs in one call

        @param updates: The values to set
        @type updates: {unicode:unicode}
        N)r   rC   rX   )r0   r   updatesry   rz   r+   r+   r,   
updateArgs  s   
zMessage.updateArgsc                 C   sb   |dusJ |dusJ |  |}t|trt|dd}|| j||f< |tur/| j| dS dS )z'Set a single argument in this namespaceNr_   r`   )r   rD   rb   rc   r/   r   r:   add)r0   r   rK   rL   r+   r+   r,   rX     s   

zMessage.setArgc                 C   s   |  |}| j||f= d S rf   r   )r0   r   rK   r+   r+   r,   delArg  s   
zMessage.delArgc                 C   s   d| j j| j j| jf S )Nz
<%s.%s %r>)	__class__r(   r'   r/   rg   r+   r+   r,   __repr__  s   
zMessage.__repr__c                 C   s   | j |j kS rf   r.   r0   otherr+   r+   r,   __eq__  rh   zMessage.__eq__c                 C   s
   | |k S rf   r+   r   r+   r+   r,   __ne__  ro   zMessage.__ne__c                 C   s   |dkr|   S |dr%| j|dd  }|d u r#|tkr!t|S |S z
|dd\}}W n ty:   d }Y nw | j|}|d u rK|}|   }| |||S )Nr   zns.   rA   rB   )	rU   rv   r:   rV   r   r   rF   rG   r   )r0   aliased_keyr   urir9   rK   r   r+   r+   r,   getAliasedArg!  s&   
zMessage.getAliasedArgrf   )Nr|   )&r'   r(   r)   r*   r   THE_OTHER_OPENID1_NSr   rd   r@   classmethodrO   rP   rH   rW   r=   rU   r]   ri   rl   rm   rs   r{   r   r   r   r   r   r   rr   r   r   r   rX   r   r   r   r   r   r+   r+   r+   r,   r      sJ    




%
9"

r   c                   @   sj   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dddZ
dd Zdd Zdd Zdd ZdS )r   zBMaintains a bijective map between namespace uris and aliases.
    c                 C   s   i | _ i | _g | _d S rf   )alias_to_namespacenamespace_to_aliasimplicit_namespacesrg   r+   r+   r,   r@   B  s   
zNamespaceMap.__init__c                 C      | j |S rf   )r   r5   r0   r8   r+   r+   r,   r   G  rh   zNamespaceMap.getAliasc                 C   r   rf   )r   r5   )r0   r9   r+   r+   r,   rV   J  rh   zNamespaceMap.getNamespaceURIc                 C   
   t | jS )z*Return an iterator over the namespace URIs)iterr   rg   r+   r+   r,   iterNamespaceURIsM     
zNamespaceMap.iterNamespaceURIsc                 C   r   )z#Return an iterator over the aliases)r   r   rg   r+   r+   r,   iterAliasesQ  r   zNamespaceMap.iterAliasesc                 C   s
   | j  S )zWIterate over the mapping

        @returns: iterator of (namespace_uri, alias)
        )r   rC   rg   r+   r+   r,   rC   U  s   
zNamespaceMap.itemsFc                 C   s  t |trt|dd}|tvsJ d|f t |tr&d|vs&J d|f | j|}|durB||krBd}|||||f }t|| j|}|dur[||kr[d}t||||f |tksmt	|ttfv smJ t
||| jvstJ || j|< || j|< |r| j| |S )	zBAdd an alias from this namespace URI to the desired alias
        r_   r`   z$%r is not an allowed namespace aliasrA   z%r must not contain a dotNz;Cannot map %r to alias %r. %r is already mapped to alias %rz;Cannot map %r to alias %r. It is already mapped to alias %r)rD   rb   rc   OPENID_PROTOCOL_FIELDSr   r5   r   r   rR   r   reprr   rT   )r0   r8   desired_aliasr?   current_namespace_urir   msgr9   r+   r+   r,   rS   \  s:   





zNamespaceMap.addAliasc                 C   sZ   | j |}|dur|S d}	 dt| }z	| || W |S  ty+   |d7 }Y nw q)zXAdd this namespace URI to the mapping, without caring what
        alias it ends up withNr   TextrB   )r   r5   rc   rS   r   )r0   r8   r9   ir+   r+   r,   r     s   zNamespaceMap.addc                 C   
   || j v S rf   )r   r   r+   r+   r,   	isDefined  ro   zNamespaceMap.isDefinedc                 C   s
   |  |S rf   )r   r   r+   r+   r,   __contains__  ro   zNamespaceMap.__contains__c                 C   r   rf   )r   r   r+   r+   r,   rp     ro   zNamespaceMap.isImplicitN)F)r'   r(   r)   r*   r@   r   rV   r   r   rC   rS   r   r   r   rp   r+   r+   r+   r,   r   >  s    
.r   )$r*   __all__rm   r   urllib.requestr   urllib.errorr#   r   r   importElementTreer   ImportErrorr
   r	   r   r   r<   r   SymbolrR   r   r   OPENID1_URL_LIMITr   rG   r%   r-   objectr   r4   	Exceptionr3   r   r   r   r+   r+   r+   r,   <module>   sF    


   6