1 // Copyright 2010 Google Inc. All rights reserved.
3 package com
.google
.appengine
.api
.oauth
;
5 import com
.google
.appengine
.api
.users
.User
;
6 import com
.google
.apphosting
.api
.ApiProxy
;
7 import com
.google
.apphosting
.api
.UserServicePb
.CheckOAuthSignatureRequest
;
8 import com
.google
.apphosting
.api
.UserServicePb
.CheckOAuthSignatureResponse
;
9 import com
.google
.apphosting
.api
.UserServicePb
.GetOAuthUserRequest
;
10 import com
.google
.apphosting
.api
.UserServicePb
.GetOAuthUserResponse
;
11 import com
.google
.apphosting
.api
.UserServicePb
.UserServiceError
;
12 import com
.google
.common
.base
.Objects
;
13 import com
.google
.io
.protocol
.ProtocolMessage
;
16 * Implementation of {@link OAuthService}.
19 final class OAuthServiceImpl
implements OAuthService
{
20 static final String GET_OAUTH_USER_RESPONSE_KEY
=
21 "com.google.appengine.api.oauth.OAuthService.get_oauth_user_response";
22 static final String GET_OAUTH_USER_SCOPE_KEY
=
23 "com.google.appengine.api.oauth.OAuthService.get_oauth_user_scope";
25 private static final String PACKAGE
= "user";
26 private static final String CHECK_SIGNATURE_METHOD
= "CheckOAuthSignature";
27 private static final String GET_OAUTH_USER_METHOD
= "GetOAuthUser";
29 public User
getCurrentUser() throws OAuthRequestException
{
30 GetOAuthUserResponse response
= getGetOAuthUserResponse(null);
31 return new User(response
.getEmail(), response
.getAuthDomain(),
32 response
.getUserId());
35 public User
getCurrentUser(String scope
) throws OAuthRequestException
{
36 GetOAuthUserResponse response
= getGetOAuthUserResponse(scope
);
37 return new User(response
.getEmail(), response
.getAuthDomain(),
38 response
.getUserId());
41 public boolean isUserAdmin() throws OAuthRequestException
{
42 GetOAuthUserResponse response
= getGetOAuthUserResponse(null);
43 return response
.isIsAdmin();
46 public String
getOAuthConsumerKey() throws OAuthRequestException
{
47 CheckOAuthSignatureRequest request
= new CheckOAuthSignatureRequest();
48 byte[] responseBytes
= makeSyncCall(CHECK_SIGNATURE_METHOD
, request
);
49 CheckOAuthSignatureResponse response
= new CheckOAuthSignatureResponse();
50 response
.mergeFrom(responseBytes
);
51 return response
.getOauthConsumerKey();
54 private GetOAuthUserResponse
getGetOAuthUserResponse(String scope
)
55 throws OAuthRequestException
{
56 ApiProxy
.Environment environment
= ApiProxy
.getCurrentEnvironment();
57 GetOAuthUserResponse response
= (GetOAuthUserResponse
)
58 environment
.getAttributes().get(GET_OAUTH_USER_RESPONSE_KEY
);
59 String lastScope
= (String
) environment
.getAttributes().get(GET_OAUTH_USER_SCOPE_KEY
);
60 if (response
== null || !Objects
.equal(lastScope
, scope
)) {
61 GetOAuthUserRequest request
= new GetOAuthUserRequest();
63 request
.setScope(scope
);
65 byte[] responseBytes
= makeSyncCall(GET_OAUTH_USER_METHOD
, request
);
66 response
= new GetOAuthUserResponse();
67 response
.mergeFrom(responseBytes
);
68 environment
.getAttributes().put(GET_OAUTH_USER_RESPONSE_KEY
, response
);
69 environment
.getAttributes().put(GET_OAUTH_USER_SCOPE_KEY
, scope
);
74 private byte[] makeSyncCall(String methodName
, ProtocolMessage request
)
75 throws OAuthRequestException
{
78 byte[] requestBytes
= request
.toByteArray();
79 responseBytes
= ApiProxy
.makeSyncCall(PACKAGE
, methodName
, requestBytes
);
80 } catch (ApiProxy
.ApplicationException ex
) {
81 UserServiceError
.ErrorCode errorCode
=
82 UserServiceError
.ErrorCode
.valueOf(ex
.getApplicationError());
85 case OAUTH_INVALID_REQUEST
:
86 throw new InvalidOAuthParametersException(ex
.getErrorDetail());
87 case OAUTH_INVALID_TOKEN
:
88 throw new InvalidOAuthTokenException(ex
.getErrorDetail());
91 throw new OAuthServiceFailureException(ex
.getErrorDetail());