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."""
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
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'
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
58 login_url
=_DEFAULT_LOGIN_URL
,
59 logout_url
=_DEFAULT_LOGOUT_URL
,
61 auth_domain
=_DEFAULT_AUTH_DOMAIN
,
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.
77 super(UserServiceStub
, self
).__init
__(service_name
,
78 request_data
=request_data
)
79 self
._login
_url
= login_url
80 self
._logout
_url
= logout_url
88 os
.environ
['AUTH_DOMAIN'] = auth_domain
90 def SetOAuthUser(self
,
92 domain
=_OAUTH_AUTH_DOMAIN
,
93 user_id
=_OAUTH_USER_ID
,
96 client_id
=_OAUTH_CLIENT_ID
):
97 """Set test OAuth user.
99 Determines what user is returned by requests to GetOAuthUser.
102 email: Email address of oauth user. None indicates that no oauth user
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
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().
121 request: a CreateLoginURLRequest
122 response: a CreateLoginURLResponse
123 request_id: A unique string identifying the request associated with the
126 response
.set_login_url(
128 urllib
.quote(self
._AddHostToContinueURL
(request
.destination_url(),
131 def _Dynamic_CreateLogoutURL(self
, request
, response
, request_id
):
132 """Trivial implementation of UserService.CreateLogoutURL().
135 request: a CreateLogoutURLRequest
136 response: a CreateLogoutURLResponse
137 request_id: A unique string identifying the request associated with the
140 response
.set_logout_url(
142 urllib
.quote(self
._AddHostToContinueURL
(request
.destination_url(),
145 def _Dynamic_GetOAuthUser(self
, request
, response
, request_id
):
146 """Trivial implementation of UserService.GetOAuthUser().
149 request: a GetOAuthUserRequest
150 response: a GetOAuthUserResponse
151 request_id: A unique string identifying the request associated with the
154 if self
.__email
is None:
155 raise apiproxy_errors
.ApplicationError(
156 user_service_pb
.UserServiceError
.OAUTH_INVALID_REQUEST
)
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().
173 unused_request: a CheckOAuthSignatureRequest
174 response: a CheckOAuthSignatureResponse
175 request_id: A unique string identifying the request associated with the
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.
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
191 (protocol
, host
, path
, parameters
, query
, fragment
) = urlparse
.urlparse(continue_url
)
193 if host
and protocol
:
196 protocol
, host
, _
, _
, _
, _
= urlparse
.urlparse(
197 self
.request_data
.get_request_url(request_id
))
203 return urlparse
.urlunparse(
204 (protocol
, host
, path
, parameters
, query
, fragment
))