
    )Jfh                     T   d Z g dZddlmZ ddlmZ ddlmZmZ dZ	dZ
d Z G d	 d
e          Z G d de          Z G d dej                  Z G d de          Zd Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          ZdS )zTImplements the OpenID Attribute Exchange specification, version 1.0.

@since: 2.1.0
)AttributeRequestFetchRequestFetchResponseStoreRequestStoreResponse    )	extension)	TrustRoot)NamespaceMap	OPENID_NS	unlimited    c                 b    d| v rt          d| d          d| v rt          d| d          dS )zx
    Check an alias for invalid characters; raise AXError if any are
    found.  Return None if the alias is valid.
    ,zAlias z must not contain comma.z must not contain periodN)AXError)aliass    W/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/openid/extensions/ax.py
checkAliasr      sK    
 e||g555CDDD
e||gEEEDEEE |    c                       e Zd ZdZdS )r   zQResults from data that does not meet the attribute exchange 1.0
    specificationN)__name__
__module____qualname____doc__ r   r   r   r   '   s           r   r   c                       e Zd ZdZd Zd ZdS )NotAXMessagez?Raised when there is no Attribute Exchange mode in the message.c                     | j         j        S N	__class__r   selfs    r   __repr__zNotAXMessage.__repr__/       ~&&r   c                     | j         j        S r   r    r"   s    r   __str__zNotAXMessage.__str__2   r%   r   N)r   r   r   r   r$   r'   r   r   r   r   r   ,   s8        II' ' '' ' ' ' 'r   r   c                   *    e Zd ZdZdZdZdZd Zd ZdS )	AXMessagea  Abstract class containing common code for attribute exchange messages

    @cvar ns_alias: The preferred namespace alias for attribute
        exchange messages

    @cvar mode: The type of this attribute exchange message. This must
        be overridden in subclasses.
    axzhttp://openid.net/srv/ax/1.0Nc                     |                     d          }t          |t                    rt          |d          }|| j        k    r*|st                      t          d| j        d|          dS )a  Raise an exception if the mode in the attribute exchange
        arguments does not match what is expected for this class.

        @raises NotAXMessage: When there is no mode value in ax_args at all.

        @raises AXError: When mode does not match.
        modezutf-8)encodingzExpected mode z; got N)get
isinstancebytesstrr,   r   r   )r#   ax_argsr,   s      r   
_checkModezAXMessage._checkModeI   s~     {{6""dE"" 	/tg...D49 N"nn$gDIIIttLMMM	 r   c                     d| j         iS )zReturn a set of attribute exchange arguments containing the
        basic information that must be in every attribute exchange
        message.
        r,   )r,   r"   s    r   _newArgszAXMessage._newArgsZ   s    
 	""r   )	r   r   r   r   ns_aliasns_urir,   r3   r5   r   r   r   r)   r)   6   sP          H+FDN N N"# # # # #r   r)   c                        e Zd ZdZddZd ZdS )AttrInfoa  Represents a single attribute in an attribute exchange
    request. This should be added to an AXRequest object in order to
    request the attribute.

    @ivar required: Whether the attribute will be marked as required
        when presented to the subject of the attribute exchange
        request.
    @type required: bool

    @ivar count: How many values of this type to request from the
        subject. Defaults to one.
    @type count: int

    @ivar type_uri: The identifier that determines what the attribute
        represents and how it is serialized. For example, one type URI
        representing dates could represent a Unix timestamp in base 10
        and another could represent a human-readable string.
    @type type_uri: str

    @ivar alias: The name that should be given to this alias in the
        request. If it is not supplied, a generic name will be
        assigned. For example, if you want to call a Unix timestamp
        value 'tstamp', set its alias to that value. If two attributes
        in the same message request to use the same alias, the request
        will fail to be generated.
    @type alias: str or NoneType
       FNc                 x    || _         || _        || _        || _        | j        t	          | j                   d S d S r   )requiredcounttype_urir   r   )r#   r>   r=   r<   r   s        r   __init__zAttrInfo.__init__   sD     
 
:!tz""""" "!r   c                 "    | j         t          k    S )a@  
        When processing a request for this attribute, the OP should
        call this method to determine whether all available attribute
        values were requested.  If self.count == UNLIMITED_VALUES,
        this returns True.  Otherwise this returns False, in which
        case self.count is an integer.
        )r=   UNLIMITED_VALUESr"   s    r   wantsUnlimitedValueszAttrInfo.wantsUnlimitedValues   s     z---r   )r:   FN)r   r   r   r   r?   rB   r   r   r   r9   r9   b   sB         B# # # #. . . . .r   r9   c                     g }|rV|                     d          D ]@}|                     |          }|t          d|          |                    |           A|S )a  Given a namespace mapping and a string containing a
    comma-separated list of namespace aliases, return a list of type
    URIs that correspond to those aliases.

    @param namespace_map: The mapping from namespace URI to alias
    @type namespace_map: openid.message.NamespaceMap

    @param alias_list_s: The string containing the comma-separated
        list of aliases. May also be None for convenience.
    @type alias_list_s: str or NoneType

    @returns: The list of namespace URIs that corresponds to the
        supplied list of aliases. If the string was zero-length or
        None, an empty list will be returned.

    @raise KeyError: If an alias is present in the list of aliases but
        is not present in the namespace map.
    r   Nz&No type is defined for attribute name )splitgetNamespaceURIKeyErrorappend)namespace_mapalias_list_surisr   r>   s        r   
toTypeURIsrK      s    & D &!'',, 	& 	&E$44U;;Hh % ) * * * H%%%%Kr   c                   h    e Zd ZdZdZddZd Zd Zd Zd Z	 e
e	          Z	d	 Zd
 Zd Zd ZeZdS )r   a  An attribute exchange 'fetch_request' message. This message is
    sent by a relying party when it wishes to obtain attributes about
    the subject of an OpenID authentication request.

    @ivar requested_attributes: The attributes that have been
        requested thus far, indexed by the type URI.
    @type requested_attributes: {str:AttrInfo}

    @ivar update_url: A URL that will accept responses for this
        attribute exchange request, even in the absence of the user
        who made this request.
    fetch_requestNc                 V    t                               |            i | _        || _        d S r   )r)   r?   requested_attributes
update_url)r#   rP   s     r   r?   zFetchRequest.__init__   s)    4   $&!$r   c                 p    |j         | j        v rt          d|j         d          || j        |j         <   dS )a+  Add an attribute to this attribute exchange request.

        @param attribute: The attribute that is being requested
        @type attribute: C{L{AttrInfo}}

        @returns: None

        @raise KeyError: when the requested attribute is already
            present in this fetch request.
        zThe attribute z has already been requestedN)r>   rO   rF   )r#   	attributes     r   addzFetchRequest.add   sS     !:::(%...2 3 3 3 9B!)"4555r   c                 &   t                      }g }g }|                                 }| j                                        D ]\  }}|j        |                    |          }n|                    ||j                  }|j        r|                    |           n|                    |           |j	        dk    rt          |j	                  |d|z   <   ||d|z   <   |rd                    |          |d<   |rd                    |          |d<   |S )zGet the serialized form of this attribute fetch request.

        @returns: The fetch request message parameters
        @rtype: {unicode:unicode}
        Nr:   count.type.r   r<   if_available)r
   r5   rO   itemsr   rS   addAliasr<   rG   r=   r1   join)r#   aliasesr<   rW   r2   r>   rR   r   s           r   getExtensionArgszFetchRequest.getExtensionArgs   s'    ..--//#'#<#B#B#D#D 	0 	0Hi&H--  ((9?CC! +&&&&##E***!##,/	,@,@5()'/GGeO$$ 	5"%((8"4"4GJ 	=&)hh|&<&<GN#r   c                     g }| j                                         D ]!\  }}|j        r|                    |           "|S )zGet the type URIs for all attributes that have been marked
        as required.

        @returns: A list of the type URIs for attributes that have
            been marked as required.
        @rtype: [str]
        )rO   rX   r<   rG   )r#   r<   r>   rR   s       r   getRequiredAttrszFetchRequest.getRequiredAttrs  sO     #'#<#B#B#D#D 	* 	*Hi! *)))r   c                    |j         }|                    | j                  } |             }	 |                    |           n# t          $ r}Y d}~dS d}~ww xY w|j        r|                    t          d|                    t          d                    }|st          d|j        d          t          j
        |          }|                    |j                  st          d|j        d|          |S )au  Extract a FetchRequest from an OpenID message

        @param openid_request: The OpenID authentication request
            containing the attribute fetch request
        @type openid_request: C{L{openid.server.server.CheckIDRequest}}

        @rtype: C{L{FetchRequest}} or C{None}
        @returns: The FetchRequest extracted from the message or None, if
            the message contained no AX extension.

        @raises KeyError: if the AuthRequest is not consistent in its use
            of namespace aliases.

        @raises AXError: When parseExtensionArgs would raise same.

        @see: L{parseExtensionArgs}
        Nrealm	return_tozCannot validate update_url z against absent realmzUpdate URL z! failed validation against realm )messagegetArgsr7   parseExtensionArgsr   rP   getArgr   r   r	   parsevalidateURL)clsopenid_requestrb   r2   r#   errr`   trs           r   fromOpenIDRequestzFetchRequest.fromOpenIDRequest  s*   $ !(//#*--suu	##G,,,, 	 	 	44444	 ? 	0 NN9g#*>>)[#I#IK KE  +g*+ + + ''B>>$/22 0g___ee/0 0 0 s   A 
AAc                    |                      |           t                      }|                                D ]\  }}|                    d          r|dd         }|}|                    ||           d|z   }|                    |          }|r^	 t          |          }	|	dk    rt          d|d|          n4# t          $ r% |t          k    rt          d|d	|          |}	Y nw xY wd
}	| 
                    t          |||	                     t          ||                    d                    }
|
D ]}d| j        |         _        t          ||                    d                    }|
|z   }|                                D ]}||vrt          d|d          |                    d          | _        dS )a  Given attribute exchange arguments, populate this FetchRequest.

        @param ax_args: Attribute Exchange arguments from the request.
            As returned from L{Message.getArgs<openid.message.Message.getArgs>}.
        @type ax_args: dict

        @raises KeyError: if the message is not consistent in its use
            of namespace aliases.

        @raises NotAXMessage: If ax_args does not include an Attribute Exchange
            mode.

        @raises AXError: If the data to be parsed does not follow the
            attribute exchange specification. At least when
            'if_available' or 'required' is not specified for a
            particular attribute type.
        rV      NrU   r   zCount z  must be greater than zero, got zInvalid count value for z: r:   )r   r=   r<   TrW   z	Type URI zC was in the request but not present in "required" or "if_available"rP   )r3   r
   rX   
startswithrY   r.   intr   
ValueErrorrA   rS   r9   rK   rO   r<   iterNamespaceURIsrP   )r#   r2   r[   keyvaluer   r>   	count_keycount_sr=   r<   rW   all_type_uriss                r   rd   zFetchRequest.parseExtensionArgsN  s)   & 	   ..!--// 	G 	GJC~~g&& GABB   5111$u,	!++i00 
( #G A::")'!*GG!7#8 #8 8 & & ( ( ("&666")'+499gg+A #B #B B '	( E(%uEEEFFFgw{{:'>'>??  	@ 	@H;?D%h/88!'7;;~+F+FGG </1133 	, 	,H},,g'xx+ , , , -
 "++l33s   *B<<,C+*C+c                 N    t          | j                                                  S )z\Iterate over the AttrInfo objects that are
        contained in this fetch_request.
        )iterrO   valuesr"   s    r   	iterAttrszFetchRequest.iterAttrs  s!     D-4466777r   c                 *    t          | j                  S )zCIterate over the attribute type URIs in this fetch_request
        )ry   rO   r"   s    r   __iter__zFetchRequest.__iter__  s     D-...r   c                     || j         v S )z=Is the given type URI present in this fetch_request?
        )rO   r#   r>   s     r   has_keyzFetchRequest.has_key  s     4444r   r   )r   r   r   r   r,   r?   rS   r\   r^   rl   classmethodrd   r{   r}   r   __contains__r   r   r   r   r      s          D% % % %
B B B"0 0 0d  + + +Z $$566?4 ?4 ?4B8 8 8/ / /
5 5 5
 LLLr   r   c                   F    e Zd ZdZd Zd Zd ZddZd ZddZ	d	 Z
d
 ZdS )AXKeyValueMessagezAn abstract class that implements a message that has attribute
    keys and values. It contains the common code between
    fetch_response and store_request.
    c                 H    t                               |            i | _        d S r   )r)   r?   datar"   s    r   r?   zAXKeyValueMessage.__init__  s!    4   			r   c                     	 | j         |         }n# t          $ r g x}| j         |<   Y nw xY w|                    |           dS )a  Add a single value for the given attribute type to the
        message. If there are already values specified for this type,
        this value will be sent in addition to the values already
        specified.

        @param type_uri: The URI for the attribute

        @param value: The value to add to the response to the relying
            party for this attribute
        @type value: unicode

        @returns: None
        N)r   rF   rG   )r#   r>   rt   rz   s       r   addValuezAXKeyValueMessage.addValue  sa    	.Yx(FF 	. 	. 	.+--FTYx(((	. 	es    ))c                     || j         |<   dS )a  Set the values for the given attribute type. This replaces
        any values that have already been set for this attribute.

        @param type_uri: The URI for the attribute

        @param values: A list of values to send for this attribute.
        @type values: [unicode]
        Nr   )r#   r>   rz   s      r   	setValueszAXKeyValueMessage.setValues  s     %	(r   Nc                 .   |t                      }i }| j                                        D ]h\  }}|                    |          }||d|z   <   t	          t          |                    |d|z   <   t          |          D ]\  }}d||dz   fz  }|||<   i|S )zGet the extension arguments for the key/value pairs
        contained in this message.

        @param aliases: An alias mapping. Set to None if you don't
            care about the aliases for this request.
        NrV   rU   value.%s.%dr:   )r
   r   rX   rS   r1   len	enumerate)	r#   r[   r2   r>   rz   r   irt   rs   s	            r   _getExtensionKVArgsz%AXKeyValueMessage._getExtensionKVArgs  s     ?"nnG $	 1 1 	% 	%HfKK))E'/GGeO$(+CKK(8(8GHu$%%f-- % %5#ua!en4$% r   c                 R   |                      |           t                      }|                                D ]K\  }}|                    d          r1|}|dd         }t	          |           |                    ||           L|                                D ]\  }}	 |d|z            }t          |          }g }	t          d|dz             D ]&}
d||
fz  }||         }|	                    |           'n'# t          $ r |d|z            }|dk    rg }	n|g}	Y nw xY w|	| j
        |<   dS )	a  Parse attribute exchange key/value arguments into this
        object.

        @param ax_args: The attribute exchange fetch_response
            arguments, with namespacing removed.
        @type ax_args: {unicode:unicode}

        @returns: None

        @raises ValueError: If the message has bad values for
            particular fields

        @raises KeyError: If the namespace mapping is bad or required
            arguments are missing
        rV   rn   NrU   r:   r   zvalue. )r3   r
   rX   ro   r   rY   rp   rangerG   rF   r   )r#   r2   r[   rs   rt   r>   r   rv   r=   rz   r   	value_keys               r   rd   z$AXKeyValueMessage.parseExtensionArgs  su     	   ..!--// 	2 	2JC~~g&& 2 ABB5!!!  5111&}} 	) 	)OHe)!(U"23 Gq%!),, ) )A -
 :I#I.EMM%(((()  % % %5 01B;;FF#WF% #)DIh%	) 	)s   C55!DDc                     | j                             |          }|s|S t          |          dk    r|d         S t          d|          )a  Get a single value for an attribute. If no value was sent
        for this attribute, use the supplied default. If there is more
        than one value for this attribute, this method will fail.

        @type type_uri: str
        @param type_uri: The URI for the attribute

        @param default: The value to return if the attribute was not
            sent in the fetch_response.

        @returns: The value of the attribute in the fetch_response
            message, or the default supplied
        @rtype: unicode or NoneType

        @raises ValueError: If there is more than one value for this
            parameter in the fetch_response message.
        @raises KeyError: If the attribute was not sent in this response
        r:   r   z More than one value present for )r   r.   r   r   )r#   r>   defaultrz   s       r   	getSinglezAXKeyValueMessage.getSingle  sU    & x(( 	ON[[A!9'((MNNNr   c                     | j         |         S )a  Get the list of values for this attribute in the
        fetch_response.

        XXX: what to do if the values are not present? default
        parameter? this is funny because it's always supposed to
        return a list, so the default may break that, though it's
        provided by the user's code, so it might be okay. If no
        default is supplied, should the return be None or []?

        @param type_uri: The URI of the attribute

        @returns: The list of values for this attribute in the
            response. May be an empty list.
        @rtype: [unicode]

        @raises KeyError: If the attribute was not sent in the response
        r   r   s     r   r.   zAXKeyValueMessage.get4  s    $ y""r   c                 F    t          |                     |                    S )az  Get the number of responses for a particular attribute in
        this fetch_response message.

        @param type_uri: The URI of the attribute

        @returns: The number of values sent for this attribute

        @raises KeyError: If the attribute was not sent in the
            response. KeyError will not be raised if the number of
            values was zero.
        )r   r.   r   s     r   r=   zAXKeyValueMessage.countH  s     488H%%&&&r   r   )r   r   r   r   r?   r   r   r   rd   r   r.   r=   r   r   r   r   r     s             *
% 
% 
%   0-) -) -)^O O O O6# # #(' ' ' ' 'r   r   c                   R     e Zd ZdZdZd	dZd Z fdZd
dZ e	e          Z xZ
S )r   z0A fetch_response attribute exchange message
    fetch_responseNc                 V    t                               |            || _        || _        dS )a  
        @param request: When supplied, I will use namespace aliases
            that match those in this request.  I will also check to
            make sure I do not respond with attributes that were not
            requested.

        @type request: L{FetchRequest}

        @param update_url: By default, C{update_url} is taken from the
            request.  But if you do not supply the request, you may set
            the C{update_url} here.

        @type update_url: str
        N)r   r?   rP   request)r#   r   rP   s      r   r?   zFetchResponse.__init__\  s*     	""4((($r   c                 T   t                      }g }| j        | j        D ]}|| j        vrt          d|          | j                                        D ]}|j        |                    |j                   n |                    |j        |j                   	 | j        |j                 }n'# t          $ r g }|	                    |           Y nw xY w|j
        t          k    r/|j
        t          |          k     rt          d|j                  |                     |          }|                                 }|D ]1}|                    |j                  }|j        |d|z   <   d|d|z   <   2| j        r| j        j        p| j        }	|	r|	|d<   |                    |           |S )zSerialize this object into arguments in the attribute
        exchange namespace

        @returns: The dictionary of unqualified attribute exchange
            arguments that represent this fetch_response.
        @rtype: {unicode;unicode}
        Nz+Response attribute not present in request: z<More than the number of requested values were specified for rV   0rU   rP   )r
   r   r   rF   r{   r   rS   r>   rY   rG   r=   rA   r   r   r   r5   getAliasrP   update)
r#   r[   zero_value_typesr>   	attr_inforz   kv_argsr2   r   rP   s
             r   r\   zFetchResponse.getExtensionArgso  s    ..<# !I & &4<//"(!%& & & 0
 "\3355 E E	 ?*KK	 23333$$Y%7III7!Yy'9:FF 7 7 7F$++I666667 O'777!#f++55!'.7.@.@DE E E **733 --// * 	, 	,I$$Y%788E'0'9GGeO$(+GHu$%%|?(? &o 	  	/$.GL!ws   B..!CCc                     t          t          |                               |           |                    d          | _        dS )zS@see: {Extension.parseExtensionArgs<openid.extension.Extension.parseExtensionArgs>}rP   N)superr   rd   r.   rP   )r#   r2   r!   s     r   rd   z FetchResponse.parseExtensionArgs  s8    mT""55g>>>!++l33r   Tc                      |             }|                     |j        |          }	 |                    |           |S # t          $ r}Y d}~dS d}~ww xY w)a  Construct a FetchResponse object from an OpenID library
        SuccessResponse object.

        @param success_response: A successful id_res response object
        @type success_response: openid.consumer.consumer.SuccessResponse

        @param signed: Whether non-signed args should be
            processsed. If True (the default), only signed arguments
            will be processsed.
        @type signed: bool

        @returns: A FetchResponse containing the data from the OpenID
            message, or None if the SuccessResponse did not contain AX
            extension data.

        @raises AXError: when the AX data cannot be parsed.
        N)extensionResponser7   rd   r   )rh   success_responsesignedr#   r2   rj   s         r   fromSuccessResponsez!FetchResponse.fromSuccessResponse  st    $ suu"44T[&II	##G,,, K  	 	 	44444	s   > 
AA)NN)T)r   r   r   r   r,   r?   r\   rd   r   r   __classcell__r!   s   @r   r   r   W  s         D   &B B BH4 4 4 4 4
   8 &+&9::r   r   c                   .     e Zd ZdZdZd fd	Zd Z xZS )r   z>A store request attribute exchange message representation
    store_requestNc                 d    t          t          |                                            || _        dS )z
        @param aliases: The namespace aliases to use when making this
            store request.  Leave as None to use defaults.
        N)r   r   r?   r[   )r#   r[   r!   s     r   r?   zStoreRequest.__init__  s,    
 	lD!!**,,,r   c                     |                                  }|                     | j                  }|                    |           |S )zb
        @see: L{Extension.getExtensionArgs<openid.extension.Extension.getExtensionArgs>}
        )r5   r   r[   r   )r#   r2   r   s      r   r\   zStoreRequest.getExtensionArgs  s=     --//**4<88wr   r   )r   r   r   r   r,   r?   r\   r   r   s   @r   r   r     s[         D           r   r   c                   .    e Zd ZdZdZdZd	dZd Zd ZdS )
r   z_An indication that the store request was processed along with
    this OpenID transaction.
    store_response_successstore_response_failureTNc                     t                               |            |r|t          d          |r| j        | _        n| j        | _        || _        d S )NzAAn error message may only be included in a failing fetch response)r)   r?   r   SUCCESS_MODEr,   FAILURE_MODEerror_message)r#   	succeededr   s      r   r?   zStoreResponse.__init__  sf    4    	42 3 4 4 4 	*)DII)DI*r   c                 "    | j         | j        k    S )z%Was this response a success response?)r,   r   r"   s    r   r   zStoreResponse.succeeded  s    yD---r   c                 x    |                                  }|                                 s| j        r
| j        |d<   |S )zO@see: {Extension.getExtensionArgs<openid.extension.Extension.getExtensionArgs>}error)r5   r   r   )r#   r2   s     r   r\   zStoreResponse.getExtensionArgs  s=    --//~~ 	2D$6 	2#1GGr   )TN)	r   r   r   r   r   r   r?   r   r\   r   r   r   r   r     s\          ,L+L+ + + +. . .    r   r   N)r   __all__openidr   openid.server.trustrootr	   openid.messager
   r   rA   MINIMUM_SUPPORTED_ALIAS_LENGTHr   rq   r   r   	Extensionr)   objectr9   rK   r   r   r   r   r   r   r   r   <module>r      s6   
         - - - - - - 2 2 2 2 2 2 2 2   "$ F F F    j   
' ' ' ' '7 ' ' ')# )# )# )# )#	# )# )# )#X3. 3. 3. 3. 3.v 3. 3. 3.l  Bf f f f f9 f f fRr' r' r' r' r'	 r' r' r'j}; }; }; }; };% }; }; };@    $   .    I     r   