
 $Nc           @   s   d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d Z d Z d Z	 d Z
 d Z d Z d	 e Z d
 Z e d  Z d   Z e d  Z e d  Z d Z d   Z d   Z d   Z d   Z e d j o e   n d S(   s  Helper CGI for logins/logout in the development application server.

This CGI has these parameters:

  continue: URL to redirect to after a login or logout has completed.
  email: Email address to set for the client.
  admin: If 'True', the client should be logged in as an admin.
  action: What action to take ('Login' or 'Logout').

To view the current user information and a form for logging in and out,
supply no parameters.
iNt   continuet   emailt   admint   actiont   Logoutt   Logins	   action=%st   dev_appserver_loginc         C   s   y t  i |   } Wn# t  i j
 o d t d f Sn Xd } | | j o | | i } n | i d  d d d g d  \ } } } | | d j | f S(   s  Get the requestor's user info from the HTTP cookie in the CGI environment.

  Args:
    http_cookie: Value of the HTTP_COOKIE environment variable.
    cookie_name: Name of the cookie that stores the user info.

  Returns:
    Tuple (email, admin, user_id) where:
      email: The user's email address, if any.
      admin: True if the user is an admin; False otherwise.
      user_id: The user ID, if any.
  t    t   :i   t   True(   t   Cookiet   SimpleCookiet   CookieErrort   Falset   valuet   split(   t   http_cookiet   cookie_namet   cookiet   cookie_valueR   R   t   user_id(    (    so   /afs/csail.mit.edu/u/j/jeanyang/public_html/apps/google_appengine/google/appengine/tools/dev_appserver_login.pyt   GetUserInfo=   s    )c         C   s   d } | o
 d } n |  o[ t  i |  i    i   } d d i g  } | D] } | d t |  qM ~  d  } n d } d |  | | f S(   s   Creates cookie payload data.

  Args:
    email, admin: Parameters to incorporate into the cookie.

  Returns:
    String containing the cookie payload.
  R   R	   t   1R   s   %02di   s   %s:%s:%s(   t   hashlibt   md5t   lowert   digestt   joint   ord(   R   R   t   admin_stringt   user_id_digestt   _[1]t   xR   (    (    so   /afs/csail.mit.edu/u/j/jeanyang/public_html/apps/google_appengine/google/appengine/tools/dev_appserver_login.pyt   CreateCookieDataW   s    	
@c         C   s;   t  |  |  } t i   } | | | <d | | d <d | S(   s>  Creates a cookie to set the user information for the requestor.

  Args:
    email: Email to set for the user.
    admin: True if the user should be admin; False otherwise.
    cookie_name: Name of the cookie that stores the user info.

  Returns:
    'Set-Cookie' header for setting the user info of the requestor.
  t   /t   paths   %s
(   R!   R
   R   (   R   R   R   R   t
   set_cookie(    (    so   /afs/csail.mit.edu/u/j/jeanyang/public_html/apps/google_appengine/google/appengine/tools/dev_appserver_login.pyt   SetUserInfoCookiek   s
    
c         C   s:   t  i   } d | |  <d | |  d <d | |  d <d | S(   s   Clears the user info cookie from the requestor, logging them out.

  Args:
    cookie_name: Name of the cookie that stores the user info.

  Returns:
    'Set-Cookie' header for clearing the user info of the requestor.
  R   R"   R#   t   0s   max-ages   %s
(   R
   R   (   R   R$   (    (    so   /afs/csail.mit.edu/u/j/jeanyang/public_html/apps/google_appengine/google/appengine/tools/dev_appserver_login.pyt   ClearUserInfoCookie}   s
    	
sF  <html>
<head>
  <title>Login</title>
</head>
<body>

<form method="get" action="%(login_url)s"
      style="text-align:center; font: 13px sans-serif">
  <div style="width: 20em; margin: 1em auto;
              text-align:left;
              padding: 0 2em 1.25em 2em;
              background-color: #d6e9f8;
              border: 2px solid #67a7e3">
    <h3>%(login_message)s</h3>
    <p style="padding: 0; margin: 0">
      <label for="email" style="width: 3em">Email:</label>
      <input name="email" type="text" value="%(email)s" id="email"/>
    </p>
    <p style="margin: .5em 0 0 3em; font-size:12px">
      <input name="admin" type="checkbox" value="True"
       %(admin_checked)s id="admin"/>
        <label for="admin">Sign in as Administrator</label>
    </p>
    <p style="margin-left: 3em">
      <input name="action" value="Login" type="submit"
             id="submit-login" />
      <input name="action" value="Logout" type="submit"
             id="submit-logout" />
    </p>
  </div>
  <input name="continue" type="hidden" value="%(continue_url)s"/>
</form>

</body>
</html>
c         C   s   d } | o
 d } n d } | o
 d } n h  t  i | d d p d d <| d	 <| d
 <t  i |  d d d <t  i | d d d <} t | S(   s   Renders the login page.

  Args:
    login_url, continue_url, email, admin: Parameters passed to
      LoginCGI.

  Returns:
    String containing the contents of the login page.
  s   Not logged ins	   Logged inR   t   checkedt   quotei   s   test@example.comR   t   admin_checkedt   login_messaget	   login_urlt   continue_url(   t   cgit   escapet   LOGIN_TEMPLATE(   R,   R-   R   R   R+   R*   t   template_dict(    (    so   /afs/csail.mit.edu/u/j/jeanyang/public_html/apps/google_appengine/google/appengine/tools/dev_appserver_login.pyt   RenderLoginTemplate   s    


		c         C   sW   d | | | f } d | | |  t  t i |  f } | i d  | i d |  d S(   sq  Writes a login redirection URL to a user.

  Args:
    login_url: Relative URL which should be used for handling user logins.
    hostname: Name of the host on which the webserver is running.
    port: Port on which the webserver is running.
    relative_url: String containing the URL accessed.
    outfile: File-like object to which the response should be written.
  s   http://%s:%s%ss   http://%s:%s%s?%s=%ss   Status: 302 Requires login
s   Location: %s

N(   t   CONTINUE_PARAMt   urllibR)   t   write(   R,   t   hostnamet   portt   relative_urlt   outfilet   dest_urlt   redirect_url(    (    so   /afs/csail.mit.edu/u/j/jeanyang/public_html/apps/google_appengine/google/appengine/tools/dev_appserver_login.pyt   LoginRedirect   s    c         C   s  d } g  }	 | o_ | i    t i    j o |	 i t    n" | o |	 i t | |   n | p |  } n | oM | i d  x |	 D] }
 | i |
  q W| i d |  | i d  nD | i d  | i d  | i d  | i t |  | | |   d S(   sS  Runs the login CGI.

  This CGI does not care about the method at all. For both POST and GET the
  client will be redirected to the continue URL.

  Args:
    login_url: URL used to run the CGI.
    email: Current email address of the requesting user.
    admin: True if the requesting user is an admin; False otherwise.
    action: The action used to run the CGI; 'Login' for a login action, 'Logout'
      for when a logout should occur.
    set_email: Email to set for the user; Empty if no email should be set.
    set_admin: True if the user should be an admin; False otherwise.
    continue_url: URL to which the user should be redirected when the CGI
      finishes loading; defaults to the login_url with no parameters (showing
      current status) if not supplied.
    outfile: File-like object to which all output data should be written.
  R   s)   Status: 302 Redirecting to continue URL
s   Location: %s
s   
s   Status: 200
s   Content-Type: text/html
N(   R   t   LOGOUT_ACTIONt   appendR'   R%   R5   R2   (   R,   R   R   R   t	   set_emailt	   set_adminR-   R9   R;   t   output_headerst   header(    (    so   /afs/csail.mit.edu/u/j/jeanyang/public_html/apps/google_appengine/google/appengine/tools/dev_appserver_login.pyt   LoginCGI   s,     c       	   C   s   t  i   }  t i d } t i i d d  } t i i d d  d j } |  i t  } |  i t d  } |  i t d  d j } |  i t	 d  } t
 | | | | | | | t i  d S(	   s%   Runs the login and logout CGI script.t	   PATH_INFOt
   USER_EMAILR   t   USER_IS_ADMINR&   R   R	   i    (   R.   t   FieldStoraget   ost   environt   gett   getfirstt   ACTION_PARAMt   EMAIL_PARAMt   ADMIN_PARAMR3   RC   t   syst   stdout(   t   formR,   R   R   R   R?   R@   R-   (    (    so   /afs/csail.mit.edu/u/j/jeanyang/public_html/apps/google_appengine/google/appengine/tools/dev_appserver_login.pyt   main!  s"    
t   __main__(   t   __doc__R.   R
   RH   RO   R4   R   R3   RM   RN   RL   R=   t   LOGIN_ACTIONt   LOGOUT_PARAMt   COOKIE_NAMER   R!   R%   R'   R0   R2   R<   RC   RR   t   __name__(    (    (    so   /afs/csail.mit.edu/u/j/jeanyang/public_html/apps/google_appengine/google/appengine/tools/dev_appserver_login.pys   <module>   s2   
	4			6	