Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / oauth / OAuthServiceImpl.java
blob3a1be6be666c9aa009954be4377aae694d8c18e2
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.io.protocol.ProtocolMessage;
14 import java.util.Arrays;
15 import java.util.Objects;
17 /**
18 * Implementation of {@link OAuthService}.
21 final class OAuthServiceImpl implements OAuthService {
22 static final String GET_OAUTH_USER_RESPONSE_KEY =
23 "com.google.appengine.api.oauth.OAuthService.get_oauth_user_response";
24 static final String GET_OAUTH_USER_SCOPE_KEY =
25 "com.google.appengine.api.oauth.OAuthService.get_oauth_user_scope";
26 static final String REQUEST_WRITER_PERMISSION_KEY =
27 "com.google.appengine.api.oauth.OAuthService.request_writer_permission";
29 private static final String PACKAGE = "user";
30 private static final String CHECK_SIGNATURE_METHOD = "CheckOAuthSignature";
31 private static final String GET_OAUTH_USER_METHOD = "GetOAuthUser";
33 @Override
34 public User getCurrentUser() throws OAuthRequestException {
35 return getCurrentUser((String[]) null);
38 @Override
39 public User getCurrentUser(String scope) throws OAuthRequestException {
40 String[] scopes = {scope};
41 return getCurrentUser(scopes);
44 @Override
45 public User getCurrentUser(String... scopes) throws OAuthRequestException {
46 GetOAuthUserResponse response = getGetOAuthUserResponse(scopes);
47 return new User(response.getEmail(), response.getAuthDomain(),
48 response.getUserId());
51 @Override
52 public boolean isUserAdmin() throws OAuthRequestException {
53 return isUserAdmin((String[]) null);
56 @Override
57 public boolean isUserAdmin(String scope) throws OAuthRequestException {
58 String[] scopes = {scope};
59 return isUserAdmin(scopes);
62 @Override
63 public boolean isUserAdmin(String... scopes) throws OAuthRequestException {
64 return getGetOAuthUserResponse(scopes).isIsAdmin();
67 @Override
68 public String getOAuthConsumerKey() throws OAuthRequestException {
69 CheckOAuthSignatureRequest request = new CheckOAuthSignatureRequest();
70 byte[] responseBytes = makeSyncCall(CHECK_SIGNATURE_METHOD, request);
71 CheckOAuthSignatureResponse response = new CheckOAuthSignatureResponse();
72 response.mergeFrom(responseBytes);
73 return response.getOauthConsumerKey();
76 @Override
77 public String getClientId(String scope) throws OAuthRequestException {
78 String[] scopes = {scope};
79 return getClientId(scopes);
82 @Override
83 public String getClientId(String... scopes) throws OAuthRequestException {
84 GetOAuthUserResponse response = getGetOAuthUserResponse(scopes);
85 return response.getClientId();
88 @Override
89 public String[] getAuthorizedScopes(String... scopes) throws OAuthRequestException {
90 GetOAuthUserResponse response = getGetOAuthUserResponse(scopes);
91 return response.scopess().toArray(new String[response.scopesSize()]);
94 private GetOAuthUserResponse getGetOAuthUserResponse(String[] scopes)
95 throws OAuthRequestException {
96 ApiProxy.Environment environment = ApiProxy.getCurrentEnvironment();
97 GetOAuthUserResponse response = (GetOAuthUserResponse)
98 environment.getAttributes().get(GET_OAUTH_USER_RESPONSE_KEY);
99 String scopesKey = "[]";
100 if (scopes != null && scopes.length > 0) {
101 String[] scopesCopy = scopes.clone();
102 Arrays.sort(scopesCopy);
103 scopesKey = Arrays.toString(scopesCopy);
105 String lastScopesKey = (String) environment.getAttributes().get(GET_OAUTH_USER_SCOPE_KEY);
106 if (response == null || !Objects.equals(lastScopesKey, scopesKey)) {
107 GetOAuthUserRequest request = new GetOAuthUserRequest();
108 if (scopes != null) {
109 for (String scope : scopes) {
110 request.addScopes(scope);
113 Boolean requestWriterPermission = (Boolean) environment.getAttributes().get(
114 REQUEST_WRITER_PERMISSION_KEY);
115 if (requestWriterPermission != null && requestWriterPermission) {
116 request.setRequestWriterPermission(true);
118 byte[] responseBytes = makeSyncCall(GET_OAUTH_USER_METHOD, request);
119 response = new GetOAuthUserResponse();
120 response.mergeFrom(responseBytes);
121 environment.getAttributes().put(GET_OAUTH_USER_RESPONSE_KEY, response);
122 environment.getAttributes().put(GET_OAUTH_USER_SCOPE_KEY, scopesKey);
124 return response;
127 private byte[] makeSyncCall(String methodName, ProtocolMessage request)
128 throws OAuthRequestException {
129 byte[] responseBytes;
130 try {
131 byte[] requestBytes = request.toByteArray();
132 responseBytes = ApiProxy.makeSyncCall(PACKAGE, methodName, requestBytes);
133 } catch (ApiProxy.ApplicationException ex) {
134 UserServiceError.ErrorCode errorCode =
135 UserServiceError.ErrorCode.valueOf(ex.getApplicationError());
136 switch (errorCode) {
137 case NOT_ALLOWED:
138 case OAUTH_INVALID_REQUEST:
139 throw new InvalidOAuthParametersException(ex.getErrorDetail());
140 case OAUTH_INVALID_TOKEN:
141 throw new InvalidOAuthTokenException(ex.getErrorDetail());
142 case OAUTH_ERROR:
143 default:
144 throw new OAuthServiceFailureException(ex.getErrorDetail());
148 return responseBytes;