Search in sources :

Example 1 with User

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) User(com.google.appengine.api.users.User) GaeUser(samples.gae.users.GaeUser) AuthenticationException(org.springframework.security.core.AuthenticationException) Authentication(org.springframework.security.core.Authentication) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 2 with User

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);
}
Also used : User(com.google.appengine.api.users.User) PresenceUser(com.pusher.rest.data.PresenceUser) HashMap(java.util.HashMap) PresenceUser(com.pusher.rest.data.PresenceUser) Pusher(com.pusher.rest.Pusher)

Example 3 with User

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>&lt;" + displayName + "&gt;</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));
}
Also used : User(com.google.appengine.api.users.User) HashMap(java.util.HashMap) Result(com.pusher.rest.data.Result)

Example 4 with User

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);
}
Also used : User(com.google.appengine.api.users.User) UserService(com.google.appengine.api.users.UserService)

Example 5 with User

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;
}
Also used : User(com.google.appengine.api.users.User) UserType(teammates.common.datatransfer.UserType)

Aggregations

User (com.google.appengine.api.users.User)24 UserService (com.google.appengine.api.users.UserService)7 HashMap (java.util.HashMap)4 Date (java.util.Date)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Test (org.junit.Test)3 Entity (com.google.appengine.api.datastore.Entity)2 Pusher (com.pusher.rest.Pusher)2 PresenceUser (com.pusher.rest.data.PresenceUser)2 Result (com.pusher.rest.data.Result)2 PrintWriter (java.io.PrintWriter)2 GaeUserCredentials (org.pac4j.gae.credentials.GaeUserCredentials)2 GaeUserServiceProfile (org.pac4j.gae.profile.GaeUserServiceProfile)2 GaeUser (samples.gae.users.GaeUser)2 DatastoreService (com.google.appengine.api.datastore.DatastoreService)1 Key (com.google.appengine.api.datastore.Key)1 Query (com.google.appengine.api.datastore.Query)1 OAuthRequestException (com.google.appengine.api.oauth.OAuthRequestException)1 OAuthService (com.google.appengine.api.oauth.OAuthService)1