use of com.google.appengine.api.users.User in project spring-security by spring-projects.
the class GaeAuthenticationFilter method doFilter.
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User googleUser = UserServiceFactory.getUserService().getCurrentUser();
if (authentication != null && !loggedInUserMatchesGaeUser(authentication, googleUser)) {
SecurityContextHolder.clearContext();
authentication = null;
((HttpServletRequest) request).getSession().invalidate();
}
if (authentication == null) {
if (googleUser != null) {
logger.debug("Currently logged on to GAE as user " + googleUser);
logger.debug("Authenticating to Spring Security");
// User has returned after authenticating via GAE. Need to authenticate
// through Spring Security.
PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken(googleUser, null);
token.setDetails(ads.buildDetails((HttpServletRequest) request));
try {
authentication = authenticationManager.authenticate(token);
SecurityContextHolder.getContext().setAuthentication(authentication);
if (authentication.getAuthorities().contains(AppRole.NEW_USER)) {
logger.debug("New user authenticated. Redirecting to registration page");
((HttpServletResponse) response).sendRedirect(REGISTRATION_URL);
return;
}
} catch (AuthenticationException e) {
failureHandler.onAuthenticationFailure((HttpServletRequest) request, (HttpServletResponse) response, e);
return;
}
}
}
chain.doFilter(request, response);
}
use of com.google.appengine.api.users.User in project java-docs-samples by GoogleCloudPlatform.
the class AuthorizeServlet method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
// Instantiate a pusher connection
Pusher pusher = PusherService.getDefaultInstance();
// Get current logged in user credentials
User user = UserServiceFactory.getUserService().getCurrentUser();
// redirect to homepage if user is not authorized
if (user == null) {
response.sendRedirect("/");
return;
}
String currentUserId = user.getUserId();
String displayName = user.getNickname().replaceFirst("@.*", "");
String query = CharStreams.toString(request.getReader());
// socket_id, channel_name parameters are automatically set in the POST body of the request
// eg.socket_id=1232.12&channel_name=presence-my-channel
Map<String, String> data = splitQuery(query);
String socketId = data.get("socket_id");
String channelId = data.get("channel_name");
// Presence channels (presence-*) require user identification for authentication
Map<String, String> userInfo = new HashMap<>();
userInfo.put("displayName", displayName);
// Inject custom authentication code for your application here to allow /deny current request
String auth = pusher.authenticate(socketId, channelId, new PresenceUser(currentUserId, userInfo));
// if successful, returns authorization in the format
// {
// "auth":"49e26cb8e9dde3dfc009:a8cf1d3deefbb1bdc6a9d1547640d49d94b4b512320e2597c257a740edd17",
// "channel_data":"{\"user_id\":\"23423435252\",\"user_info\":{\"displayName\":\"John Doe\"}}"
// }
response.getWriter().append(auth);
}
use of com.google.appengine.api.users.User in project java-docs-samples by GoogleCloudPlatform.
the class SendMessageServlet method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
// Parse POST request body received in the format :
// [{"message": "my-message", "socket_id": "1232.24", "channel": "presence-my-channel"}]
String body = CharStreams.readLines(request.getReader()).toString();
String json = body.replaceFirst("^\\[", "").replaceFirst("\\]$", "");
Map<String, String> data = gson.fromJson(json, typeReference.getType());
String message = data.get("message");
String socketId = data.get("socket_id");
String channelId = data.get("channel_id");
User user = UserServiceFactory.getUserService().getCurrentUser();
// user email prefix as display name for current logged in user
String displayName = user.getNickname().replaceFirst("@.*", "");
// Create a message including the user email prefix to display in the chat window
String taggedMessage = "<strong><" + displayName + "></strong> " + message;
Map<String, String> messageData = new HashMap<>();
messageData.put("message", taggedMessage);
// Send a message over the Pusher channel (maximum size of a message is 10KB)
Result result = PusherService.getDefaultInstance().trigger(channelId, // name of event
"new_message", messageData, // (optional) use client socket_id to exclude the sender from receiving the message
socketId);
// result.getStatus() == SUCCESS indicates successful transmission
messageData.put("status", result.getStatus().name());
response.getWriter().println(gson.toJson(messageData));
}
use of com.google.appengine.api.users.User in project java-docs-samples by GoogleCloudPlatform.
the class SignGuestbookServlet method doPost.
// Process the http POST of the form
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Greeting greeting;
UserService userService = UserServiceFactory.getUserService();
// Find out who the user is.
User user = userService.getCurrentUser();
String guestbookName = req.getParameter("guestbookName");
String content = req.getParameter("content");
if (user != null) {
greeting = new Greeting(guestbookName, content, user.getUserId(), user.getEmail());
} else {
greeting = new Greeting(guestbookName, content);
}
// Use Objectify to save the greeting and now() is used to make the call synchronously as we
// will immediately get a new page using redirect and we want the data to be present.
ObjectifyService.ofy().save().entity(greeting).now();
resp.sendRedirect("/guestbook.jsp?guestbookName=" + guestbookName);
}
use of com.google.appengine.api.users.User in project teammates by TEAMMATES.
the class GateKeeper method getCurrentUser.
public UserType getCurrentUser() {
User user = getCurrentGoogleUser();
if (user == null) {
return null;
}
UserType userType = new UserType(user);
if (isAdministrator()) {
userType.isAdmin = true;
}
if (isInstructor()) {
userType.isInstructor = true;
}
if (isStudent()) {
userType.isStudent = true;
}
return userType;
}
Aggregations