
    qh~G                     &   S r SSKrSSKrSSKJr  SSKJr  SSKJr  SSKJr  / SQr	SS	/r
\" \S
5      (       a  \" \
5      r/ SQrSS/rO
S/rSS/rS/rS rS r " S S\5      r\" \5      r\" \5      rS r\R.                   " S S\5      5       rg)a  
This module contains code for dealing with associations between
consumers and servers. Associations contain a shared secret that is
used to sign C{openid.mode=id_res} messages.

Users of the library should not usually need to interact directly with
associations. The L{store<openid.store>}, L{server<openid.server.server>}
and L{consumer<openid.consumer.consumer>} objects will create and manage
the associations. The consumer and server code will make use of a
C{L{SessionNegotiator}} when managing associations, which enables
users to express a preference for what kind of associations should be
allowed, and what kind of exchange should be done to establish the
association.

@var default_negotiator: A C{L{SessionNegotiator}} that allows all
    association types that are specified by the OpenID
    specification. It prefers to use HMAC-SHA1/DH-SHA1, if it's
    available. If HMAC-SHA256 is not supported by your Python runtime,
    HMAC-SHA256 and DH-SHA256 will not be available.

@var encrypted_negotiator: A C{L{SessionNegotiator}} that
    does not support C{'no-encryption'} associations. It prefers
    HMAC-SHA1/DH-SHA1 association types if available.
    N)	cryptutil)kvform)oidutil)	OPENID_NS)default_negotiatorencrypted_negotiatorSessionNegotiatorAssociation	HMAC-SHA1HMAC-SHA256
hmacSha256)r   DH-SHA1r   no-encryptionr   	DH-SHA256)r   r   r   r   r   c                 8    SS/SS/S.nUR                  U / 5      $ )z=Return the allowed session types for a given association typer   r   r   r   r   )get)
assoc_typeassoc_to_sessions     D/var/www/html/env/lib/python3.13/site-packages/openid/association.pygetSessionTypesr   J   s1      1#_5 
B//    c                 H    U[        U 5      ;  a  [        SU< SU < 35      eg)zLCheck to make sure that this pair of assoc type and session
type are allowedzSession type z not valid for assocation type N)r   
ValueError)r   session_types     r   checkSessionTyper   S   s-     ?:66&
4 5 	5 7r   c                   @    \ rS rSrSrS rS rS rSS jrS r	S	 r
S
rg)r	   [   a  A session negotiator controls the allowed and preferred
association types and association session types. Both the
C{L{Consumer<openid.consumer.consumer.Consumer>}} and
C{L{Server<openid.server.server.Server>}} use negotiators when
creating associations.

You can create and use negotiators if you:

 - Do not want to do Diffie-Hellman key exchange because you use
   transport-layer encryption (e.g. SSL)

 - Want to use only SHA-256 associations

 - Do not want to support plain-text associations over a non-secure
   channel

It is up to you to set a policy for what kinds of associations to
accept. By default, the library will make any kind of association
that is allowed in the OpenID 2.0 specification.

Use of negotiators in the library
=================================

When a consumer makes an association request, it calls
C{L{getAllowedType}} to get the preferred association type and
association session type.

The server gets a request for a particular association/session
type and calls C{L{isAllowed}} to determine if it should
create an association. If it is supported, negotiation is
complete. If it is not, the server calls C{L{getAllowedType}} to
get an allowed association type to return to the consumer.

If the consumer gets an error response indicating that the
requested association/session type is not supported by the server
that contains an assocation/session type to try, it calls
C{L{isAllowed}} to determine if it should try again with the
given combination of association/session type.

@ivar allowed_types: A list of association/session types that are
    allowed by the server. The order of the pairs in this list
    determines preference. If an association/session type comes
    earlier in the list, the library is more likely to use that
    type.
@type allowed_types: [(str, str)]
c                 &    U R                  U5        g N)setAllowedTypes)selfallowed_typess     r   __init__SessionNegotiator.__init__   s    ]+r   c                 J    U R                  [        U R                  5      5      $ r#   )	__class__listr&   r%   s    r   copySessionNegotiator.copy   s    ~~d4#5#5677r   c                 <    U H  u  p#[        X#5        M     Xl        g)zSSet the allowed association types, checking to make sure
each combination is valid.N)r   r&   )r%   r&   r   r   s       r   r$   !SessionNegotiator.setAllowedTypes   s!     +8&ZZ6 +8 +r   Nc                 
   U R                   c  / U l         UcE  [        U5      nU(       d  [        SU< 35      e[        U5       H  nU R                  X5        M     g[	        X5        U R                   R                  X45        g)zAdd an association type and session type to the allowed
types list. The assocation/session pairs are tried in the
order that they are added.Nz*No session available for association type )r&   r   r   addAllowedTyper   append)r%   r   r   	availables       r   r2    SessionNegotiator.addAllowedType   s     %!#D'
3I $."2 3 3 !0
 ;##J= !< Z6%%z&@Ar   c                 T    X4U R                   ;   nU[        U5      ;   nU=(       a    U$ )zAIs this combination of association type and session type allowed?)r&   r   )r%   r   r   
assoc_goodmatchess        r   	isAllowedSessionNegotiator.isAllowed   s/     /43E3EE
/*"==%g%r   c                 B     U R                   S   $ ! [         a     gf = f)zAGet a pair of assocation type and session type that are
supportedr   )NN)r&   
IndexErrorr,   s    r   getAllowedType SessionNegotiator.getAllowedType   s*    	 %%a(( 	 	 s    
)r&   r#   )__name__
__module____qualname____firstlineno____doc__r'   r-   r$   r2   r9   r=   __static_attributes__ r   r   r	   r	   [   s'    -^,8+B(& r   r	   c                 <    U S:X  a  gU S:X  a  g[        SU < 35      e)Nr      r       zUnsupported association type: )r   )r   s    r   getSecretSizerI      s&    [ 	}	$zLMMr   c                       \ rS rSrSr/ SQr\R                  \R                  S.r	\
S 5       rS r\SS j5       rS	 rS
 rS rS r\
S 5       rS rS rS rS rS rS rSrg)r
      a  
This class represents an association between a server and a
consumer.  In general, users of this library will never see
instances of this object.  The only exception is if you implement
a custom C{L{OpenIDStore<openid.store.interface.OpenIDStore>}}.

If you do implement such a store, it will need to store the values
of the C{L{handle}}, C{L{secret}}, C{L{issued}}, C{L{lifetime}}, and
C{L{assoc_type}} instance variables.

@ivar handle: This is the handle the server gave this association.

@type handle: C{str}


@ivar secret: This is the shared secret the server generated for
    this association.

@type secret: C{str}


@ivar issued: This is the time this association was issued, in
    seconds since 00:00 GMT, January 1, 1970.  (ie, a unix
    timestamp)

@type issued: C{int}


@ivar lifetime: This is the amount of time this association is
    good for, measured in seconds since the association was
    issued.

@type lifetime: C{int}


@ivar assoc_type: This is the type of association this instance
    represents.  The only valid value of this field at this time
    is C{'HMAC-SHA1'}, but new types may be defined in the future.

@type assoc_type: C{str}


@sort: __init__, fromExpiresIn, expiresIn, __eq__, __ne__,
    handle, secret, issued, lifetime, assoc_type
versionhandlesecretissuedlifetimer   r   c                 V    [        [        R                  " 5       5      nUnU " X#XVU5      $ )a!  
This is an alternate constructor used by the OpenID consumer
library to create associations.  C{L{OpenIDStore
<openid.store.interface.OpenIDStore>}} implementations
shouldn't use this constructor.


@param expires_in: This is the amount of time this association
    is good for, measured in seconds since the association was
    issued.

@type expires_in: C{int}


@param handle: This is the handle the server gave this
    association.

@type handle: C{str}


@param secret: This is the shared secret the server generated
    for this association.

@type secret: C{str}


@param assoc_type: This is the type of association this
    instance represents.  The only valid value of this field
    at this time is C{'HMAC-SHA1'}, but new types may be
    defined in the future.

@type assoc_type: C{str}
)inttime)cls
expires_inrN   rO   r   rP   rQ   s          r   fromExpiresInAssociation.fromExpiresIn  s)    F TYY[!66Z@@r   c                     U[         ;  a  Sn[        Xe4-  5      eXl        [        U[        5      (       a  UR                  S5      nX l        X0l        X@l        XPl	        g)a8  
This is the standard constructor for creating an association.


@param handle: This is the handle the server gave this
    association.

@type handle: C{str}


@param secret: This is the shared secret the server generated
    for this association.

@type secret: C{str}


@param issued: This is the time this association was issued,
    in seconds since 00:00 GMT, January 1, 1970.  (ie, a unix
    timestamp)

@type issued: C{int}


@param lifetime: This is the amount of time this association
    is good for, measured in seconds since the association was
    issued.

@type lifetime: C{int}


@param assoc_type: This is the type of association this
    instance represents.  The only valid value of this field
    at this time is C{'HMAC-SHA1'}, but new types may be
    defined in the future.

@type assoc_type: C{str}
z&%r is not a supported association typeutf-8N)
all_association_typesr   rN   
isinstancestrencoderO   rP   rQ   r   )r%   rN   rO   rP   rQ   r   fmts          r   r'   Association.__init__/  sY    L 22:CS>122 fc""]]7+F $r   Nc                     Uc  [        [        R                  " 5       5      n[        SU R                  U R                  -   U-
  5      $ )a  
This returns the number of seconds this association is still
valid for, or C{0} if the association is no longer valid.


@return: The number of seconds this association is still valid
    for, or C{0} if the association is no longer valid.

@rtype: C{int}
r   )rS   rT   maxrP   rQ   )r%   nows     r   	expiresInAssociation.expiresInh  s8     ;diik"C1dkkDMM1C788r   c                 4    U R                   UR                   :  $ )z
Compare two C{L{Association}} instances to determine relative
ordering.

Currently compares object lifetimes -- C{L{Association}} A < B
if A.lifetime < B.lifetime.
)rQ   r%   others     r   __lt__Association.__lt__y  s     }}u~~--r   c                 n    [        U 5      [        U5      L =(       a    U R                  UR                  :H  $ )z
This checks to see if two C{L{Association}} instances
represent the same association.


@return: C{True} if the two instances represent the same
    association, C{False} otherwise.

@rtype: C{bool}
)type__dict__rg   s     r   __eq__Association.__eq__  s)     DzT%[(LT]]enn-LLr   c                     X:X  + $ )z
This checks to see if two C{L{Association}} instances
represent different associations.


@return: C{True} if the two instances represent different
    associations, C{False} otherwise.

@rtype: C{bool}
rE   rg   s     r   __ne__Association.__ne__  s     ""r   c           	         SU R                   [        R                  " U R                  5      [	        [        U R                  5      5      [	        [        U R                  5      5      U R                  S.n[        U5      [        U R                  5      :X  d   e/ nU R                   H  nUR                  X1U   45        M     [        R                  " USS9$ )z~
Convert an association to KV form.

@return: String in KV form suitable for deserialization by
    deserialize.

@rtype: str
2rL   Tstrict)rN   r   toBase64rO   r]   rS   rP   rQ   r   len
assoc_keysr3   r   seqToKV)r%   datapairs
field_names       r   	serializeAssociation.serialize  s     kk&&t{{3#dkk*+C.///
 4yC0000//JLL*:&678 * ~~eD11r   c                 d   [         R                  " USS9n/ n/ nU H'  u  pVUR                  U5        UR                  U5        M)     X0R                  :w  a  [	        SU5      eUu  pxppUS:w  a  [	        SU-  5      e[        U
5      n
[        U5      n[        R                  " U	5      n	U " XXU5      $ )z
Parse an association as stored by serialize().

inverse of serialize

@param assoc_s: Association as serialized by serialize()
@type assoc_s: bytes

@return: instance of this class
Tru   zUnexpected key values: %rrt   zUnknown version: %r)r   kvToSeqr3   ry   r   rS   r   
fromBase64)rU   assoc_sr|   keysvalueskvrM   rN   rO   rP   rQ   r   s                r   deserializeAssociation.deserialize  s     wt4DAKKNMM!  >>!8$??@F=c>2W<==Vx=##F+66Z@@r   c                     [         R                  " U5      n U R                  U R                     nU" U R                  U5      $ ! [         a    [        SU R                  < 35      ef = f)z
Generate a signature for a sequence of (key, value) pairs


@param pairs: The pairs to sign, in order
@type pairs: sequence of (str, str)


@return: The binary signature of this sequence of pairs
@rtype: bytes
zUnknown association type: )r   rz   _macsr   KeyErrorr   rO   )r%   r|   kvmacs       r   signAssociation.sign  sf     ^^E"	2**T__-C
 4;;##	  	2"oo1 2 2	2s   A $A(c                 n    U R                  U5      n[        R                  " U R                  U5      5      $ )a  Return the signature of a message.

If I am not a sign-all association, the message must have a
signed list.

@return: the signature, base64 encoded

@rtype: bytes

@raises ValueError: If there is no signed list and I am not a sign-all
    type of association.
)
_makePairsr   rw   r   )r%   messager|   s      r   getMessageSignatureAssociation.getMessageSignature  s,     (		% 011r   c                    UR                  [        S5      (       d  UR                  [        S5      (       a  [        S5      eUR                  [        S5      nU(       a  X R                  :w  a  [        S5      eUR                  5       nUR                  [        SU R                  5        [        UR                  5       R                  5       5      nU Vs/ s H  oUR                  S5      (       d  M  USS PM!     nnUR                  S5        UR                  5         UR                  [        SS	R                  U5      5        U R                  U5      nUR                  [        SU5        U$ s  snf )
zAdd a signature (and a signed list) to a message.

@return: a new Message object with a signature
@rtype: L{openid.message.Message}
sigsignedz,Message already has signed list or signatureassoc_handlez*Message has a different association handleopenid.   N,)hasKeyr   r   getArgrN   r-   setArgr+   
toPostArgsr   
startswithr3   sortjoinr   )r%   r   extant_handlesigned_messagemessage_keysr   signed_listr   s           r   signMessageAssociation.signMessage  s    NN9e,,y(33KLLy.A]kk9IJJ iEN557<<>?&2Nlll96MuqulN8$i388K3HI&&~6i4 Os   E.2	E.c                     UR                  [        S5      nU(       d  [        U< S35      eU R                  U5      nUR	                  S5      n[
        R                  " X25      $ )zGiven a message with a signature, calculate a new signature
and return whether it matches the signature in the message.

@raises ValueError: if the message has no signature or no signature
    can be calculated for it.
r   z has no sig.rZ   )r   r   r   r   decoder   const_eq)r%   r   message_sigcalculated_sigs       r   checkMessageSignature!Association.checkMessageSignature  sV     nnY6;<<11':'..w7!!.>>r   c                     UR                  [        S5      nU(       d  [        SU< 35      eUR                  S5      n/ nUR	                  5       nU H(  nUR                  XeR                  SU-   S5      45        M*     U$ )Nr   zMessage has no signed list: r   r    )r   r   r   splitr   r3   r   )r%   r   r   r   r|   r{   fields          r   r   Association._makePairs#  sr    	84KLLll3'!!# ELL%)e*;R!@AB !r   c           	          SU R                   R                  < SU R                   R                  < SU R                  < SU R                  < S3	$ )N<. >)r*   r@   r?   r   rN   r,   s    r   __repr__Association.__repr__/  s3    "&..";";"&.."9"94??"&++/ 	/r   )r   rN   rP   rQ   rO   r#   )r?   r@   rA   rB   rC   ry   r   hmacSha1r   r   classmethodrW   r'   propertyrd   ri   rn   rq   r~   r   r   r   r   r   r   r   rD   rE   r   r   r
   r
      s    ,^J '' ++E
 $A $AL7%r 9 9 .M#24 A A:$,2 2?
/r   r
   )rC   rT   	functoolsopenidr   r   r   openid.messager   __all__r[   hasattrr+   supported_association_typesdefault_association_order only_encrypted_association_orderr   r   objectr	   r   r   rI   total_orderingr
   rE   r   r   <module>r      s   0      $  
 9l##"&'<"=! 	!$($
 $/- 	!&! 	!($
05^  ^ B ''@A ()IJ N h/& h/ h/r   