#!/usr/bin/env python # # Copyright 2007 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """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. """ import cgi import Cookie import md5 import os import sys import urllib CONTINUE_PARAM = 'continue' EMAIL_PARAM = 'email' ADMIN_PARAM = 'admin' ACTION_PARAM = 'action' LOGOUT_ACTION = 'Logout' LOGIN_ACTION = 'Login' LOGOUT_PARAM = 'action=%s' % LOGOUT_ACTION COOKIE_NAME = 'dev_appserver_login' def GetUserInfo(http_cookie, cookie_name=COOKIE_NAME): """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) where: email: The user's email address, if any. admin: True if the user is an admin; False otherwise. """ cookie = Cookie.SimpleCookie(http_cookie) cookie_value = '' if cookie_name in cookie: cookie_value = cookie[cookie_name].value email, admin, user_id = (cookie_value.split(':') + ['', '', ''])[:3] return email, (admin == 'True'), user_id def CreateCookieData(email, admin): """Creates cookie payload data. Args: email, admin: Parameters to incorporate into the cookie. Returns: String containing the cookie payload. """ admin_string = 'False' if admin: admin_string = 'True' if email: user_id_digest = md5.new(email.lower()).digest() user_id = '1' + ''.join(['%02d' % ord(x) for x in user_id_digest])[:20] else: user_id = '' return '%s:%s:%s' % (email, admin_string, user_id) def SetUserInfoCookie(email, admin, cookie_name=COOKIE_NAME): """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. """ cookie_value = CreateCookieData(email, admin) set_cookie = Cookie.SimpleCookie() set_cookie[cookie_name] = cookie_value set_cookie[cookie_name]['path'] = '/' return '%s\r\n' % set_cookie def ClearUserInfoCookie(cookie_name=COOKIE_NAME): """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. """ set_cookie = Cookie.SimpleCookie() set_cookie[cookie_name] = '' set_cookie[cookie_name]['path'] = '/' set_cookie[cookie_name]['max-age'] = '0' return '%s\r\n' % set_cookie LOGIN_TEMPLATE = """