App Engine Python SDK version 1.8.1
[gae.git] / python / google / appengine / api / user_service_stub.py
blob4ffee32df90280753ad315d11c0a048acb3ec544
1 #!/usr/bin/env python
3 # Copyright 2007 Google Inc.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
22 """Trivial implementation of the UserService."""
25 import os
26 import urllib
27 import urlparse
28 from google.appengine.api import apiproxy_stub
29 from google.appengine.api import user_service_pb
31 if os.environ.get('APPENGINE_RUNTIME') == 'python27':
32 from google.appengine.runtime import apiproxy_errors
33 else:
34 from google.appengine.runtime import apiproxy_errors
36 _DEFAULT_LOGIN_URL = 'https://www.google.com/accounts/Login?continue=%s'
37 _DEFAULT_LOGOUT_URL = 'https://www.google.com/accounts/Logout?continue=%s'
38 _DEFAULT_AUTH_DOMAIN = 'gmail.com'
43 _OAUTH_CONSUMER_KEY = 'example.com'
44 _OAUTH_EMAIL = 'example@example.com'
45 _OAUTH_USER_ID = '0'
46 _OAUTH_AUTH_DOMAIN = _DEFAULT_AUTH_DOMAIN
47 _OAUTH_CLIENT_ID = '123456789.apps.googleusercontent.com'
50 class UserServiceStub(apiproxy_stub.APIProxyStub):
51 """Trivial implementation of the UserService."""
53 _ACCEPTS_REQUEST_ID = True
55 THREADSAFE = True
57 def __init__(self,
58 login_url=_DEFAULT_LOGIN_URL,
59 logout_url=_DEFAULT_LOGOUT_URL,
60 service_name='user',
61 auth_domain=_DEFAULT_AUTH_DOMAIN,
62 request_data=None):
63 """Initializer.
65 Args:
66 login_url: String containing the URL to use for logging in.
67 logout_url: String containing the URL to use for logging out.
68 service_name: Service name expected for all calls.
69 auth_domain: The authentication domain for the service e.g. "gmail.com".
70 request_data: A apiproxy_stub.RequestData instance used to look up state
71 associated with the request that generated an API call.
73 Note: Both the login_url and logout_url arguments must contain one format
74 parameter, which will be replaced with the continuation URL where the user
75 should be redirected after log-in or log-out has been completed.
76 """
77 super(UserServiceStub, self).__init__(service_name,
78 request_data=request_data)
79 self._login_url = login_url
80 self._logout_url = logout_url
81 self.__scopes = None
83 self.SetOAuthUser()
88 os.environ['AUTH_DOMAIN'] = auth_domain
90 def SetOAuthUser(self,
91 email=_OAUTH_EMAIL,
92 domain=_OAUTH_AUTH_DOMAIN,
93 user_id=_OAUTH_USER_ID,
94 is_admin=False,
95 scopes=None,
96 client_id=_OAUTH_CLIENT_ID):
97 """Set test OAuth user.
99 Determines what user is returned by requests to GetOAuthUser.
101 Args:
102 email: Email address of oauth user. None indicates that no oauth user
103 is authenticated.
104 domain: Domain of oauth user.
105 user_id: User ID of oauth user.
106 is_admin: Whether the user is an admin.
107 scopes: List of scopes that user is authenticated against.
108 client_id: Client ID of the OAuth2 request
110 self.__email = email
111 self.__domain = domain
112 self.__user_id = user_id
113 self.__is_admin = is_admin
114 self.__scopes = scopes
115 self._client_id = client_id
117 def _Dynamic_CreateLoginURL(self, request, response, request_id):
118 """Trivial implementation of UserService.CreateLoginURL().
120 Args:
121 request: a CreateLoginURLRequest
122 response: a CreateLoginURLResponse
123 request_id: A unique string identifying the request associated with the
124 API call.
126 response.set_login_url(
127 self._login_url %
128 urllib.quote(self._AddHostToContinueURL(request.destination_url(),
129 request_id)))
131 def _Dynamic_CreateLogoutURL(self, request, response, request_id):
132 """Trivial implementation of UserService.CreateLogoutURL().
134 Args:
135 request: a CreateLogoutURLRequest
136 response: a CreateLogoutURLResponse
137 request_id: A unique string identifying the request associated with the
138 API call.
140 response.set_logout_url(
141 self._logout_url %
142 urllib.quote(self._AddHostToContinueURL(request.destination_url(),
143 request_id)))
145 def _Dynamic_GetOAuthUser(self, request, response, request_id):
146 """Trivial implementation of UserService.GetOAuthUser().
148 Args:
149 request: a GetOAuthUserRequest
150 response: a GetOAuthUserResponse
151 request_id: A unique string identifying the request associated with the
152 API call.
154 if self.__email is None:
155 raise apiproxy_errors.ApplicationError(
156 user_service_pb.UserServiceError.OAUTH_INVALID_REQUEST)
157 else:
158 if self.__scopes is not None:
160 if request.scope() not in self.__scopes:
161 raise apiproxy_errors.ApplicationError(
162 user_service_pb.UserServiceError.OAUTH_INVALID_TOKEN)
163 response.set_email(self.__email)
164 response.set_user_id(self.__user_id)
165 response.set_auth_domain(self.__domain)
166 response.set_is_admin(self.__is_admin)
167 response.set_client_id(self._client_id)
169 def _Dynamic_CheckOAuthSignature(self, unused_request, response, request_id):
170 """Trivial implementation of UserService.CheckOAuthSignature().
172 Args:
173 unused_request: a CheckOAuthSignatureRequest
174 response: a CheckOAuthSignatureResponse
175 request_id: A unique string identifying the request associated with the
176 API call.
178 response.set_oauth_consumer_key(_OAUTH_CONSUMER_KEY)
180 def _AddHostToContinueURL(self, continue_url, request_id):
181 """Adds the request host to the continue url if no host is specified.
183 Args:
184 continue_url: the URL which may or may not have a host specified
185 request_id: A unique string identifying the request associated with the
186 API call.
188 Returns:
189 string
191 (protocol, host, path, parameters, query, fragment) = urlparse.urlparse(continue_url)
193 if host and protocol:
194 return continue_url
196 protocol, host, _, _, _, _ = urlparse.urlparse(
197 self.request_data.get_request_url(request_id))
200 if path == '':
201 path = '/'
203 return urlparse.urlunparse(
204 (protocol, host, path, parameters, query, fragment))