|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.appengine.pusher; |
| 18 | + |
| 19 | +import com.google.appengine.api.users.User; |
| 20 | +import com.google.appengine.api.users.UserServiceFactory; |
| 21 | +import com.google.common.io.CharStreams; |
| 22 | +import com.pusher.rest.Pusher; |
| 23 | +import com.pusher.rest.data.PresenceUser; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.UnsupportedEncodingException; |
| 26 | +import java.net.URLDecoder; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | +import javax.servlet.http.HttpServlet; |
| 30 | +import javax.servlet.http.HttpServletRequest; |
| 31 | +import javax.servlet.http.HttpServletResponse; |
| 32 | + |
| 33 | +/** |
| 34 | + * Authorization endpoint that is automatically triggered on `Pusher.subscribe` for private, |
| 35 | + * presence channels. Successful authentication returns valid authorization token with user |
| 36 | + * information. |
| 37 | + * |
| 38 | + * @see <a href="https://pusher.com/docs/authenticating_users">Pusher Authentication Docs</a> |
| 39 | + */ |
| 40 | +// [START pusher_authorize] |
| 41 | +public class AuthorizeServlet extends HttpServlet { |
| 42 | + |
| 43 | + @Override |
| 44 | + public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { |
| 45 | + |
| 46 | + // Instantiate a pusher connection |
| 47 | + Pusher pusher = PusherService.getDefaultInstance(); |
| 48 | + // Get current logged in user credentials |
| 49 | + User user = UserServiceFactory.getUserService().getCurrentUser(); |
| 50 | + |
| 51 | + // redirect to homepage if user is not authorized |
| 52 | + if (user == null) { |
| 53 | + response.sendRedirect("/"); |
| 54 | + return; |
| 55 | + } |
| 56 | + String currentUserId = user.getUserId(); |
| 57 | + String displayName = user.getNickname().replaceFirst("@.*", ""); |
| 58 | + |
| 59 | + String query = CharStreams.toString(request.getReader()); |
| 60 | + // socket_id, channel_name parameters are automatically set in the POST body of the request |
| 61 | + // eg.socket_id=1232.12&channel_name=presence-my-channel |
| 62 | + Map<String, String> data = splitQuery(query); |
| 63 | + String socketId = data.get("socket_id"); |
| 64 | + String channelId = data.get("channel_name"); |
| 65 | + |
| 66 | + // Presence channels (presence-*) require user identification for authentication |
| 67 | + Map<String, String> userInfo = new HashMap<>(); |
| 68 | + userInfo.put("displayName", displayName); |
| 69 | + |
| 70 | + // Inject custom authentication code for your application here to allow /deny current request |
| 71 | + |
| 72 | + String auth = |
| 73 | + pusher.authenticate(socketId, channelId, new PresenceUser(currentUserId, userInfo)); |
| 74 | + // if successful, returns authorization in the format |
| 75 | + // { |
| 76 | + // "auth":"49e26cb8e9dde3dfc009:a8cf1d3deefbb1bdc6a9d1547640d49d94b4b512320e2597c257a740edd1788f", |
| 77 | + // "channel_data":"{\"user_id\":\"23423435252\",\"user_info\":{\"displayName\":\"John Doe\"}}" |
| 78 | + // } |
| 79 | + |
| 80 | + response.getWriter().append(auth); |
| 81 | + } |
| 82 | + |
| 83 | + private static Map<String, String> splitQuery(String query) throws UnsupportedEncodingException { |
| 84 | + Map<String, String> query_pairs = new HashMap<>(); |
| 85 | + String[] pairs = query.split("&"); |
| 86 | + for (String pair : pairs) { |
| 87 | + int idx = pair.indexOf("="); |
| 88 | + query_pairs.put( |
| 89 | + URLDecoder.decode(pair.substring(0, idx), "UTF-8"), |
| 90 | + URLDecoder.decode(pair.substring(idx + 1), "UTF-8")); |
| 91 | + } |
| 92 | + return query_pairs; |
| 93 | + } |
| 94 | +} |
| 95 | +// [END pusher_authorize] |
0 commit comments