1 // Copyright 2007 Google Inc. All rights reserved.
3 package com
.google
.appengine
.api
.users
;
5 import com
.google
.apphosting
.api
.ApiProxy
;
6 import com
.google
.apphosting
.api
.UserServicePb
.CreateLoginURLRequest
;
7 import com
.google
.apphosting
.api
.UserServicePb
.CreateLoginURLResponse
;
8 import com
.google
.apphosting
.api
.UserServicePb
.CreateLogoutURLRequest
;
9 import com
.google
.apphosting
.api
.UserServicePb
.CreateLogoutURLResponse
;
10 import com
.google
.apphosting
.api
.UserServicePb
.UserServiceError
;
11 import com
.google
.io
.protocol
.ProtocolMessage
;
14 import java
.util
.logging
.Logger
;
17 * The UserService provides information useful for forcing a user to
18 * log in or out, and retrieving information about the user who is
19 * currently logged-in.
22 final class UserServiceImpl
implements UserService
{
23 static final String USER_ID_KEY
=
24 "com.google.appengine.api.users.UserService.user_id_key";
26 static final String FEDERATED_IDENTITY_KEY
=
27 "com.google.appengine.api.users.UserService.federated_identity";
29 static final String FEDERATED_AUTHORITY_KEY
=
30 "com.google.appengine.api.users.UserService.federated_authority";
32 static final String IS_FEDERATED_USER_KEY
=
33 "com.google.appengine.api.users.UserService.is_federated_user";
35 private static final String PACKAGE
= "user";
36 private static final String LOGIN_URL_METHOD
= "CreateLoginURL";
37 private static final String LOGOUT_URL_METHOD
= "CreateLogoutURL";
39 private static final String OPENID_DEPRECATION_WARNING
=
40 "Open ID 2.0 support in the App Engine Users service is deprecated and "
41 + "will soon be removed. Please see "
42 + "https://cloud.google.com/appengine/docs/deprecations/open_id "
45 private static final Logger logger
= Logger
.getLogger(
46 UserServiceImpl
.class.getName());
48 public String
createLoginURL(String destinationURL
) {
49 return createLoginURL(destinationURL
, null, null, null);
52 public String
createLoginURL(String destinationURL
,
54 return createLoginURL(destinationURL
, authDomain
, null, null);
57 public String
createLoginURL(String destinationURL
,
59 String federatedIdentity
,
60 Set
<String
> attributesRequest
) {
61 CreateLoginURLRequest request
= new CreateLoginURLRequest();
62 request
.setDestinationUrl(destinationURL
);
63 if (authDomain
!= null) {
64 request
.setAuthDomain(authDomain
);
66 if (federatedIdentity
!= null) {
67 logger
.warning(OPENID_DEPRECATION_WARNING
);
68 request
.setFederatedIdentity(federatedIdentity
);
70 byte[] responseBytes
= makeSyncCall(LOGIN_URL_METHOD
, request
,
72 CreateLoginURLResponse response
= new CreateLoginURLResponse();
73 boolean parsed
= response
.mergeFrom(responseBytes
);
74 if (!parsed
|| !response
.isInitialized()) {
75 throw new UserServiceFailureException("Could not parse CreateLoginURLResponse");
77 return response
.getLoginUrl();
80 public String
createLogoutURL(String destinationURL
) {
81 return createLogoutURL(destinationURL
, null);
84 public String
createLogoutURL(String destinationURL
,
86 CreateLogoutURLRequest request
= new CreateLogoutURLRequest();
87 request
.setDestinationUrl(destinationURL
);
88 if (authDomain
!= null) {
89 request
.setAuthDomain(authDomain
);
91 byte[] responseBytes
= makeSyncCall(LOGOUT_URL_METHOD
, request
,
93 CreateLogoutURLResponse response
= new CreateLogoutURLResponse();
94 boolean parsed
= response
.mergeFrom(responseBytes
);
95 if (!parsed
|| !response
.isInitialized()) {
96 throw new UserServiceFailureException("Could not parse CreateLogoutURLResponse");
98 return response
.getLogoutUrl();
101 public boolean isUserLoggedIn() {
102 ApiProxy
.Environment environment
= ApiProxy
.getCurrentEnvironment();
103 return environment
.isLoggedIn();
106 public boolean isUserAdmin() {
107 if (isUserLoggedIn()) {
108 return ApiProxy
.getCurrentEnvironment().isAdmin();
110 throw new IllegalStateException("The current user is not logged in.");
114 public User
getCurrentUser() {
115 ApiProxy
.Environment environment
= ApiProxy
.getCurrentEnvironment();
116 if (!environment
.isLoggedIn()) {
119 String userId
= (String
) environment
.getAttributes().get(USER_ID_KEY
);
120 Boolean isFederated
= (Boolean
) environment
.getAttributes().get(IS_FEDERATED_USER_KEY
);
121 if ((isFederated
== null) || !isFederated
) {
122 return new User(environment
.getEmail(), environment
.getAuthDomain(), userId
);
124 return new User(environment
.getEmail(),
125 (String
) environment
.getAttributes().get(FEDERATED_AUTHORITY_KEY
),
127 (String
) environment
.getAttributes().get(FEDERATED_IDENTITY_KEY
));
131 private byte[] makeSyncCall(String methodName
,
132 ProtocolMessage request
,
133 String destinationURL
) {
134 byte[] responseBytes
;
136 byte[] requestBytes
= request
.toByteArray();
137 responseBytes
= ApiProxy
.makeSyncCall(PACKAGE
, methodName
, requestBytes
);
138 } catch (ApiProxy
.ApplicationException ex
) {
139 UserServiceError
.ErrorCode errorCode
=
140 UserServiceError
.ErrorCode
.valueOf(ex
.getApplicationError());
142 case REDIRECT_URL_TOO_LONG
:
143 throw new IllegalArgumentException("URL too long: " + destinationURL
);
145 throw new IllegalArgumentException("The requested URL was not allowed: " +
148 throw new UserServiceFailureException(ex
.getErrorDetail());
152 return responseBytes
;