
                             S r SSKrSSKrSSKrSSKrSSKJr  SSKJr  SS/rSr	Sr
\R                  " S	5      SS
 j5       r\R                  " S5      SS j5       rg)z4Helper methods for creating & verifying XSRF tokens.    N)_helpers)utilz "Doug Coker" <dcoker@google.com>z&"Joe Gregorio" <jcgregorio@google.com>   :i     c                 V   [         R                  " [        R                  " U SS95      nUR	                  [        R                  " [        U5      SS95        UR	                  [        5        UR	                  [        R                  " USS95        UR	                  [        5        [        R                  " [        U=(       d    [        [        R                  " 5       5      5      SS9nUR	                  U5        UR                  5       n[        R                  " U[        -   U-   5      nU$ )a  Generates a URL-safe token for the given user, action, time tuple.

Args:
    key: secret key to use.
    user_id: the user ID of the authenticated user.
    action_id: a string identifier of the action they requested
               authorization for.
    when: the time in seconds since the epoch at which the user was
          authorized for this action. If not set the current time is used.

Returns:
    A string XSRF protection token.
zutf-8)encoding)hmacnewr   	_to_bytesupdatestr	DELIMITERinttimedigestbase64urlsafe_b64encode)keyuser_id	action_idwhendigesterr   tokens          0lib/third_party/oauth2client/contrib/xsrfutil.pygenerate_tokenr   %   s     xx**3ABHOOH&&s7|gFGOOIOOH&&y7CDOOIc$":#diik*:;gNDOOD__F$$Vi%7$%>?EL       c                    U(       d  g [         R                  " U5      n[        UR                  [        5      S   5      nUc  [        R                  " 5       nXF-
  [        :  a  g[        XUUS9n[        U5      [        U5      :w  a  gSn[        [        U5      [        U5      5       H  u  pXU
-  -  nM     U(       + $ ! [
        [        [        R                  4 a     gf = f)a"  Validates that the given token authorizes the user for the action.

Tokens are invalid if the time of issue is too old or if the token
does not match what generateToken outputs (i.e. the token was forged).

Args:
    key: secret key to use.
    token: a string of the token generated by generateToken.
    user_id: the user ID of the authenticated user.
    action_id: a string identifier of the action they requested
               authorization for.

Returns:
    A boolean - True if the user is authorized for the action, False
    otherwise.
F)r   r   r   )r   urlsafe_b64decoder   splitr   	TypeError
ValueErrorbinasciiErrorr   DEFAULT_TIMEOUT_SECSr   lenzip	bytearray)r   r   r   r   current_timedecoded
token_timeexpected_token	differentxys              r   validate_tokenr1   A   s    $ **51y1"56
 yy{ #77 $CI)35N
5zS(( IIe$i&?@U	 A=% z8>>2 s   7C C#"C#) N)__doc__r   r$   r	   r   oauth2clientr   r   __authors__r   r&   
positionalr   r1    r   r   <module>r8      st    ;     !  ', 	    6 ( (r   