thirdparty/google_appengine/google/appengine/api/user_service_stub.py
changeset 109 620f9b141567
child 686 df109be0567c
equal deleted inserted replaced
108:261778de26ff 109:620f9b141567
       
     1 #!/usr/bin/env python
       
     2 #
       
     3 # Copyright 2007 Google Inc.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #     http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 #
       
    17 
       
    18 """Trivial implementation of the UserService."""
       
    19 
       
    20 
       
    21 import os
       
    22 import urllib
       
    23 import urlparse
       
    24 from google.appengine.api import user_service_pb
       
    25 
       
    26 
       
    27 _DEFAULT_LOGIN_URL = 'https://www.google.com/accounts/Login?continue=%s'
       
    28 _DEFAULT_LOGOUT_URL = 'https://www.google.com/accounts/Logout?continue=%s'
       
    29 
       
    30 
       
    31 class UserServiceStub(object):
       
    32   """Trivial implementation of the UserService."""
       
    33 
       
    34   def __init__(self,
       
    35                login_url=_DEFAULT_LOGIN_URL,
       
    36                logout_url=_DEFAULT_LOGOUT_URL):
       
    37     """Initializer.
       
    38 
       
    39     Args:
       
    40       login_url: String containing the URL to use for logging in.
       
    41       logout_url: String containing the URL to use for logging out.
       
    42 
       
    43     Note: Both the login_url and logout_url arguments must contain one format
       
    44     parameter, which will be replaced with the continuation URL where the user
       
    45     should be redirected after log-in or log-out has been completed.
       
    46     """
       
    47     self.__num_requests = 0
       
    48     self._login_url = login_url
       
    49     self._logout_url = logout_url
       
    50 
       
    51     os.environ['AUTH_DOMAIN'] = 'gmail.com'
       
    52 
       
    53   def num_requests(self):
       
    54     return self.__num_requests
       
    55 
       
    56   def MakeSyncCall(self, service, call, request, response):
       
    57     """The apiproxy entry point.
       
    58 
       
    59     Args:
       
    60       service: must be 'user'
       
    61       call: string representing which function to call
       
    62       request: the URL to redirect to, a base.StringProto
       
    63       response: the URL, a base.StringProto
       
    64 
       
    65     Currently, CreateLoginURL and CreateLogoutURL are supported.
       
    66     """
       
    67     assert service == 'user'
       
    68 
       
    69     method = getattr(self, "_Dynamic_" + call)
       
    70     method(request, response)
       
    71 
       
    72   def _Dynamic_CreateLoginURL(self, request, response):
       
    73     """Trivial implementation of UserService.CreateLoginURL().
       
    74 
       
    75     Args:
       
    76       request: the URL to redirect to after login; a base.StringProto
       
    77       response: the login URL; a base.StringProto
       
    78     """
       
    79     self.__num_requests += 1
       
    80     response.set_value(
       
    81         self._login_url %
       
    82         urllib.quote(self._AddHostToContinueURL(request.value())))
       
    83 
       
    84   def _Dynamic_CreateLogoutURL(self, request, response):
       
    85     """Trivial implementation of UserService.CreateLogoutURL().
       
    86 
       
    87     Args:
       
    88       request: the URL to redirect to after logout; a base.StringProto
       
    89       response: the logout URL; a base.StringProto
       
    90     """
       
    91     self.__num_requests += 1
       
    92     response.set_value(
       
    93         self._logout_url %
       
    94         urllib.quote(self._AddHostToContinueURL(request.value())))
       
    95 
       
    96   def _AddHostToContinueURL(self, continue_url):
       
    97     """Adds the request host to the continue url if no host is specified.
       
    98 
       
    99     Args:
       
   100       continue_url: the URL which may or may not have a host specified
       
   101 
       
   102     Returns:
       
   103       string
       
   104     """
       
   105     (protocol, host, path, parameters, query, fragment) = urlparse.urlparse(continue_url, 'http')
       
   106 
       
   107     if host:
       
   108       return continue_url
       
   109 
       
   110     host = os.environ['SERVER_NAME']
       
   111     if os.environ['SERVER_PORT'] != '80':
       
   112       host = host + ":" + os.environ['SERVER_PORT']
       
   113 
       
   114     if path == '':
       
   115       path = '/'
       
   116 
       
   117     return urlparse.urlunparse(
       
   118       (protocol, host, path, parameters, query, fragment))