
    )JfE                        d Z ddlmZmZ ddlmZ ddlZ ej        e          Z		 e
 n# e$ r e
e
fZ
Y nw xY wg dZdddd	d
ddddd	Zd ZdZdZeZ	  eed           n9# e$ r1Ze	                    deddd e
e                     Y dZ[ndZ[ww xY wd Z G d de          Zd Z G d de          Z G d de          ZdS )a  Simple registration request and response parsing and object representation

This module contains objects representing simple registration requests
and responses that can be used with both OpenID relying parties and
OpenID providers.

  1. The relying party creates a request object and adds it to the
     C{L{AuthRequest<openid.consumer.consumer.AuthRequest>}} object
     before making the C{checkid_} request to the OpenID provider::

      auth_request.addExtension(SRegRequest(required=['email']))

  2. The OpenID provider extracts the simple registration request from
     the OpenID request using C{L{SRegRequest.fromOpenIDRequest}},
     gets the user's approval and data, creates a C{L{SRegResponse}}
     object and adds it to the C{id_res} response::

      sreg_req = SRegRequest.fromOpenIDRequest(checkid_request)
      # [ get the user's approval and data, informing the user that
      #   the fields in sreg_response were requested ]
      sreg_resp = SRegResponse.extractResponse(sreg_req, user_data)
      sreg_resp.toMessage(openid_response.fields)

  3. The relying party uses C{L{SRegResponse.fromSuccessResponse}} to
     extract the data from the OpenID response::

      sreg_resp = SRegResponse.fromSuccessResponse(success_response)

@since: 2.0

@var sreg_data_fields: The names of the data fields that are listed in
    the sreg spec, and a description of them in English

@var sreg_uri: The preferred URI to use for the simple registration
    namespace and XRD Type value
    )registerNamespaceAliasNamespaceAliasRegistrationError)	ExtensionN)SRegRequestSRegResponsedata_fieldsns_uri
ns_uri_1_0
ns_uri_1_1supportsSRegz	Full NameNicknamezDate of BirthzE-mail AddressGenderzPostal CodeCountryLanguagez	Time Zone)	fullnamenicknamedobemailgenderpostcodecountrylanguagetimezonec                 <    | t           vrt          | d          dS )zCheck to see that the given value is a valid simple
    registration data field name.

    @raise ValueError: if the field name is not a valid simple
        registration data field name
    z+ is not a defined simple registration fieldN)r   
ValueError)
field_names    Y/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/openid/extensions/sreg.pycheckFieldNamer   K   s5     $$$**( ) ) 	) %$    zhttp://openid.net/sreg/1.0z%http://openid.net/extensions/sreg/1.1sregzregisterNamespaceAlias(z, z
) failed: c                 j    |                      t                    p|                      t                    S )a%  Does the given endpoint advertise support for simple
    registration?

    @param endpoint: The endpoint object as returned by OpenID discovery
    @type endpoint: openid.consumer.discover.OpenIDEndpoint

    @returns: Whether an sreg type was advertised by the endpoint
    @rtype: bool
    )usesExtensionr   r
   )endpoints    r   r   r   j   s0     "":.. /"":..0r   c                       e Zd ZdZdS )SRegNamespaceErrora  The simple registration namespace was not found and could not
    be created using the expected name (there's another extension
    using the name 'sreg')

    This is not I{illegal}, for OpenID 2, although it probably
    indicates a problem, since it's not expected that other extensions
    will re-use the alias that is in use for OpenID 1.

    If this is an OpenID 1 request, then there is no recourse. This
    should not happen unless some code has modified the namespaces for
    the message that is being processed.
    N)__name__
__module____qualname____doc__ r   r   r%   r%   x   s           r   r%   c                     t           t          fD ] }| j                            |          }| nK!t           }	 | j                            t           d           n!# t
          $ r}t          |          d}~ww xY w|S )a  Extract the simple registration namespace URI from the given
    OpenID message. Handles OpenID 1 and 2, as well as both sreg
    namespace URIs found in the wild, as well as missing namespace
    definitions (for OpenID 1)

    @param message: The OpenID message from which to parse simple
        registration fields. This may be a request or response message.
    @type message: C{L{openid.message.Message}}

    @returns: the sreg namespace URI for the supplied message. The
        message may be modified to define a simple registration
        namespace.
    @rtype: C{str}

    @raise ValueError: when using OpenID 1 if the message defines
        the 'sreg' alias to be something other than a simple
        registration type.
    Nr    )r   r
   
namespacesgetAliasaddAliasKeyErrorr%   )messagesreg_ns_urialiaswhys       r   	getSRegNSr4      s    * #J/ * *"++K88E 
 !	*''
F;;;; 	* 	* 	* %S)))	* s    A 
A7#A22A7c                       e Zd ZdZdZdddefdZ ee          Z	d Z
 ee
          Z
ddZd Zd	 Zd
 ZddZddZd ZdS )r   a4  An object to hold the state of a simple registration request.

    @ivar required: A list of the required fields in this simple
        registration request
    @type required: [str]

    @ivar optional: A list of the optional fields in this simple
        registration request
    @type optional: [str]

    @ivar policy_url: The policy URL that was provided with the request
    @type policy_url: str or NoneType

    @group Consumer: requestField, requestFields, getExtensionArgs, addToOpenIDRequest
    @group Server: fromOpenIDRequest, parseExtensionArgs
    r    Nc                     t          j        |            g | _        g | _        || _        || _        |r|                     |dd           |r|                     |dd           dS dS )z/Initialize an empty simple registration requestT)requiredstrictFN)r   __init__r7   optional
policy_urlr	   requestFields)selfr7   r:   r;   r1   s        r   r9   zSRegRequest.__init__   s     	4   $! 	Ex$tDDD 	Fx%EEEEE	F 	Fr   c                      |             }|j                                         }|                    |          |_        |                    |j                  }|                    |           |S )a\  Create a simple registration request that contains the
        fields that were requested in the OpenID request with the
        given arguments

        @param request: The OpenID request
        @type request: openid.server.CheckIDRequest

        @returns: The newly created simple registration request
        @rtype: C{L{SRegRequest}}
        )r0   copy
_getSRegNSr	   getArgsparseExtensionArgs)clsrequestr=   r0   argss        r   fromOpenIDRequestzSRegRequest.fromOpenIDRequest   sc     suu /&&((oog..t{++%%%r   Fc                    dD ]b}|dk    }|                     |          }|rC|                    d          D ]-}	 |                     |||           # t          $ r |r Y *w xY wc|                     d          | _        dS )aA  Parse the unqualified simple registration request
        parameters and add them to this object.

        This method is essentially the inverse of
        C{L{getExtensionArgs}}. This method restores the serialized simple
        registration request fields.

        If you are extracting arguments from a standard OpenID
        checkid_* request, you probably want to use C{L{fromOpenIDRequest}},
        which will extract the sreg namespace and arguments from the
        OpenID request. This method is intended for cases where the
        OpenID server needs more control over how the arguments are
        parsed than that method provides.

        >>> args = message.getArgs(ns_uri)
        >>> request.parseExtensionArgs(args)

        @param args: The unqualified simple registration arguments
        @type args: {str:str}

        @param strict: Whether requests with fields that are not
            defined in the simple registration specification should be
            tolerated (and ignored)
        @type strict: bool

        @returns: None; updates this object
        r7   r:   r7   ,r;   N)getsplitrequestFieldr   r;   )r=   rE   r8   	list_namer7   itemsr   s          r   rB   zSRegRequest.parseExtensionArgs   s    8 2 		" 		"I!Z/HHHY''E ""'++c"2"2 " "J"))*hGGGG% " " "! "!" "" ((<00s   AA"!A"c                      | j         | j        z   S )zA list of all of the simple registration fields that were
        requested, whether they were required or optional.

        @rtype: [str]
        rH   r=   s    r   allRequestedFieldszSRegRequest.allRequestedFields  s     }t},,r   c                 D    t          |                                           S )zRHave any simple registration fields been requested?

        @rtype: bool
        )boolrQ   rP   s    r   wereFieldsRequestedzSRegRequest.wereFieldsRequested$  s    
 D++--...r   c                 &    || j         v p|| j        v S )zWas this field in the request?rH   r=   r   s     r   __contains__zSRegRequest.__contains__+  s    dm+JzT]/JKr   c                 B   t          |           |r"|| j        v s	|| j        v rt          d          n3|| j        v rdS || j        v r|r| j                            |           ndS |r| j                            |           dS | j                            |           dS )ae  Request the specified field from the OpenID user

        @param field_name: the unqualified simple registration field name
        @type field_name: str

        @param required: whether the given field should be presented
            to the user as being a required to successfully complete
            the request

        @param strict: whether to raise an exception when a field is
            added to a request more than once

        @raise ValueError: when the field requested is not a simple
            registration field or strict is set and the field was
            requested more than once
        z%That field has already been requestedN)r   r7   r:   r   removeappend)r=   r   r7   r8   s       r   rL   zSRegRequest.requestField/  s    " 	z""" 	T]**jDM.I.I !HIII /J T]**T]** M((4444F 	-M  ,,,,,M  ,,,,,r   c                     t          |t                    r t          dt          |          d          |D ]}|                     |||           dS )a$  Add the given list of fields to the request

        @param field_names: The simple registration data fields to request
        @type field_names: [str]

        @param required: Whether these values should be presented to
            the user as required

        @param strict: whether to raise an exception when a field is
            added to a request more than once

        @raise ValueError: when a field requested is not a simple
            registration field or strict is set and a field was
            requested more than once
        z2Fields should be passed as a list of strings (not ))r8   N)
isinstancestr	TypeErrortyperL   )r=   field_namesr7   r8   r   s        r   r<   zSRegRequest.requestFieldsT  s      k3'' 	H)26{2C2C2C2CG H H H & 	C 	CJj(6BBBB	C 	Cr   c                     i }| j         rd                    | j                   |d<   | j        rd                    | j                  |d<   | j        r
| j        |d<   |S )a   Get a dictionary of unqualified simple registration
        arguments representing this request.

        This method is essentially the inverse of
        C{L{parseExtensionArgs}}. This method serializes the simple
        registration request fields.

        @rtype: {str:str}
        rI   r7   r:   r;   )r7   joinr:   r;   )r=   rE   s     r   getExtensionArgszSRegRequest.getExtensionArgsk  sh     = 	7"xx66D= 	7"xx66D? 	1!%Dr   )F)FF)r&   r'   r(   r)   ns_aliasr	   r9   staticmethodr4   r@   rF   classmethodrB   rQ   rT   rW   rL   r<   rd   r*   r   r   r   r      s         " H  #	F F F F( i((J  . $$566'1 '1 '1 '1R- - -/ / /L L L#- #- #- #-JC C C C.    r   r   c                       e Zd ZdZdZdefdZd Z ee          Z e	e
          ZddZ ee          Zd Zdd	Zd
 Zd Zd Zd Zd Zd Zd Zd Zd ZdS )r   a  Represents the data returned in a simple registration response
    inside of an OpenID C{id_res} response. This object will be
    created by the OpenID server, added to the C{id_res} response
    object, and then extracted from the C{id_res} message by the
    Consumer.

    @ivar data: The simple registration data, keyed by the unqualified
        simple registration name of the field (i.e. nickname is keyed
        by C{'nickname'})

    @ivar ns_uri: The URI under which the simple registration data was
        stored in the response message.

    @group Server: extractResponse
    @group Consumer: fromSuccessResponse
    @group Read-only dictionary interface: keys, iterkeys, items, iteritems,
        __iter__, get, __getitem__, keys, has_key
    r    Nc                 ^    t          j        |            |i | _        n|| _        || _        d S N)r   r9   datar	   )r=   rk   r1   s      r   r9   zSRegResponse.__init__  s3    4   <DIIDI!r   c                      |             }|j         |_         |                                D ]#}|                    |          }|
||j        |<   $|S )a  Take a C{L{SRegRequest}} and a dictionary of simple
        registration values and create a C{L{SRegResponse}}
        object containing that data.

        @param request: The simple registration request object
        @type request: SRegRequest

        @param data: The simple registration data for this
            response, as a dictionary from unqualified simple
            registration field name to string (unicode) value. For
            instance, the nickname should be stored under the key
            'nickname'.
        @type data: {str:str}

        @returns: a simple registration response object
        @rtype: SRegResponse
        )r	   rQ   rJ   rk   )rC   rD   rk   r=   fieldvalues         r   extractResponsezSRegResponse.extractResponse  sZ    $ suun//11 	) 	)EHHUOOE #(	% r   Tc                     |             }|                     |j                  |_        |r|                    |j                  }n|j                            |j                  }|sdS t
          D ]}||v r||         |j        |<   |S )a  Create a C{L{SRegResponse}} object from a successful OpenID
        library response
        (C{L{openid.consumer.consumer.SuccessResponse}}) response
        message

        @param success_response: A SuccessResponse from consumer.complete()
        @type success_response: C{L{openid.consumer.consumer.SuccessResponse}}

        @param signed_only: Whether to process only data that was
            signed in the id_res message from the server.
        @type signed_only: bool

        @rtype: SRegResponse
        @returns: A simple registration response containing the data
            that was supplied with the C{id_res} response.
        N)r@   r0   r	   getSignedNSrA   r   rk   )rC   success_responsesigned_onlyr=   rE   r   s         r   fromSuccessResponsez SRegResponse.fromSuccessResponse  s    " suuoo&6&>?? 	A#//<<DD#+33DK@@D 	4% 	9 	9JT!!(,Z(8	*%r   c                     | j         S )zGet the fields to put in the simple registration namespace
        when adding them to an id_res message.

        @see: openid.extension
        )rk   rP   s    r   rd   zSRegResponse.getExtensionArgs  s     yr   c                 V    t          |           | j                            ||          S )ztLike dict.get, except that it checks that the field name is
        defined by the simple registration specification)r   rk   rJ   )r=   r   defaults      r   rJ   zSRegResponse.get  s(     	z"""y}}Z111r   c                 N    t          | j                                                  S )zDAll of the data values in this simple registration response
        )listrk   rN   rP   s    r   rN   zSRegResponse.items  s     DIOO%%&&&r   c                 N    t          | j                                                  S rj   )iterrk   rN   rP   s    r   	iteritemszSRegResponse.iteritems  s    DIOO%%&&&r   c                 N    t          | j                                                  S rj   )ry   rk   keysrP   s    r   r~   zSRegResponse.keys      DINN$$%%%r   c                 N    t          | j                                                  S rj   )r{   rk   r~   rP   s    r   iterkeyszSRegResponse.iterkeys  r   r   c                 
    || v S rj   r*   )r=   keys     r   has_keyzSRegResponse.has_key  s    d{r   c                 2    t          |           || j        v S rj   r   rk   rV   s     r   rW   zSRegResponse.__contains__  s    z"""TY&&r   c                 *    t          | j                  S rj   )r{   rk   rP   s    r   __iter__zSRegResponse.__iter__	      DIr   c                 :    t          |           | j        |         S rj   r   rV   s     r   __getitem__zSRegResponse.__getitem__  s    z"""y$$r   c                 *    t          | j                  S rj   )rS   rk   rP   s    r   __bool__zSRegResponse.__bool__  r   r   )Trj   )r&   r'   r(   r)   re   r	   r9   ro   rg   rf   r4   r@   rt   rd   rJ   rN   r|   r~   r   r   rW   r   r   r   r*   r   r   r   r     sA        & H f " " " "  4 "k/22O i((J   B &+&9::  2 2 2 2' ' '
' ' '& & && & &  ' ' '  % % %    r   r   )r)   openid.messager   r   openid.extensionr   logging	getLoggerr&   loggerr^   	NameError__all__r   r   r
   r   r	   e	exceptionr   r   r%   r4   r   r   r*   r   r   <module>r      s=  # #J% % % % % % % % & & & & & & 		8	$	$CC   *CCC   
 
	) 	) 	) *
 5
 
6:v....& 6 6 6
!zz66633q6665 6 6 6 6 6 6 6 66
0 0 0       & & &RP P P P P) P P PfN N N N N9 N N N N Ns$   ) 	55A B$'BB