This repository has been archived by the owner on May 13, 2019. It is now read-only.
/
Signin.java
343 lines (313 loc) · 13.7 KB
/
Signin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.plus.samples.quickstart;
import com.google.api.client.auth.oauth2.TokenResponseException;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.plus.Plus;
import com.google.api.services.plus.model.PeopleFeed;
import com.google.gson.Gson;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.ServletHandler;
import org.mortbay.jetty.servlet.SessionHandler;
import org.apache.log4j.BasicConfigurator;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Scanner;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Simple server to demonstrate how to use Google+ Sign-In and make a request
* via your own server.
*
* @author joannasmith@google.com (Joanna Smith)
* @author vicfryzel@google.com (Vic Fryzel)
*/
public class Signin {
/*
* Default HTTP transport to use to make HTTP requests.
*/
private static final HttpTransport TRANSPORT = new NetHttpTransport();
/*
* Default JSON factory to use to deserialize JSON.
*/
private static final JacksonFactory JSON_FACTORY = new JacksonFactory();
/*
* Gson object to serialize JSON responses to requests to this servlet.
*/
private static final Gson GSON = new Gson();
/*
* Creates a client secrets object from the client_secrets.json file.
*/
private static GoogleClientSecrets clientSecrets;
static {
try {
Reader reader = new FileReader("client_secrets.json");
clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, reader);
} catch (IOException e) {
throw new Error("No client_secrets.json found", e);
}
}
/*
* This is the Client ID that you generated in the API Console.
*/
private static final String CLIENT_ID = clientSecrets.getWeb().getClientId();
/*
* This is the Client Secret that you generated in the API Console.
*/
private static final String CLIENT_SECRET = clientSecrets.getWeb().getClientSecret();
/*
* Optionally replace this with your application's name.
*/
private static final String APPLICATION_NAME = "Google+ Java Quickstart";
/**
* Register all endpoints that we'll handle in our server.
* @param args Command-line arguments.
* @throws Exception from Jetty if the component fails to start
*/
public static void main(String[] args) throws Exception {
BasicConfigurator.configure();
Server server = new Server(4567);
ServletHandler servletHandler = new ServletHandler();
SessionHandler sessionHandler = new SessionHandler();
sessionHandler.setHandler(servletHandler);
server.setHandler(sessionHandler);
servletHandler.addServletWithMapping(ConnectServlet.class, "/connect");
servletHandler.addServletWithMapping(DisconnectServlet.class, "/disconnect");
servletHandler.addServletWithMapping(PeopleServlet.class, "/people");
servletHandler.addServletWithMapping(MainServlet.class, "/");
server.start();
server.join();
}
/**
* Initialize a session for the current user, and render index.html.
*/
public static class MainServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// This check serves the signin button image
if ("/signin_button.png".equals(request.getServletPath())) {
File staticFile = new File("./static/signin_button.png");
FileInputStream fileStream = new FileInputStream(staticFile);
byte []buf = new byte[(int)staticFile.length()];
fileStream.read(buf);
response.setContentType("image/png");
response.getOutputStream().write(buf);
response.setStatus(HttpServletResponse.SC_OK);
return;
}
// This check prevents the "/" handler from handling all requests by default
if (!"/".equals(request.getServletPath())) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
response.setContentType("text/html");
try {
// Create a state token to prevent request forgery.
// Store it in the session for later validation.
String state = new BigInteger(130, new SecureRandom()).toString(32);
request.getSession().setAttribute("state", state);
// Fancy way to read index.html into memory, and set the client ID
// and state values in the HTML before serving it.
response.getWriter().print(new Scanner(new File("index.html"), "UTF-8")
.useDelimiter("\\A").next()
.replaceAll("[{]{2}\\s*CLIENT_ID\\s*[}]{2}", CLIENT_ID)
.replaceAll("[{]{2}\\s*STATE\\s*[}]{2}", state)
.replaceAll("[{]{2}\\s*APPLICATION_NAME\\s*[}]{2}",
APPLICATION_NAME)
.toString());
response.setStatus(HttpServletResponse.SC_OK);
} catch (FileNotFoundException e) {
// When running the quickstart, there was some path issue in finding
// index.html. Double check the quickstart guide.
e.printStackTrace();
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
response.getWriter().print(e.toString());
}
}
}
/**
* Upgrade given auth code to token, and store it in the session.
* POST body of request should be the authorization code.
* Example URI: /connect?state=...&gplus_id=...
*/
public static class ConnectServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
// Only connect a user that is not already connected.
String tokenData = (String) request.getSession().getAttribute("token");
if (tokenData != null) {
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().print(GSON.toJson("Current user is already connected."));
return;
}
// Ensure that this is no request forgery going on, and that the user
// sending us this connect request is the user that was supposed to.
if (!request.getParameter("state").equals(request.getSession().getAttribute("state"))) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getWriter().print(GSON.toJson("Invalid state parameter."));
return;
}
// Normally the state would be a one-time use token, however in our
// simple case, we want a user to be able to connect and disconnect
// without reloading the page. Thus, for demonstration, we don't
// implement this best practice.
//request.getSession().removeAttribute("state");
ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
getContent(request.getInputStream(), resultStream);
String code = new String(resultStream.toByteArray(), "UTF-8");
try {
// Upgrade the authorization code into an access and refresh token.
GoogleTokenResponse tokenResponse =
new GoogleAuthorizationCodeTokenRequest(TRANSPORT, JSON_FACTORY,
CLIENT_ID, CLIENT_SECRET, code, "postmessage").execute();
// You can read the Google user ID in the ID token.
// This sample does not use the user ID.
GoogleIdToken idToken = tokenResponse.parseIdToken();
String gplusId = idToken.getPayload().getSubject();
// Store the token in the session for later use.
request.getSession().setAttribute("token", tokenResponse.toString());
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().print(GSON.toJson("Successfully connected user."));
} catch (TokenResponseException e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().print(GSON.toJson("Failed to upgrade the authorization code."));
} catch (IOException e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().print(GSON.toJson("Failed to read token data from Google. " +
e.getMessage()));
}
}
/*
* Read the content of an InputStream.
*
* @param inputStream the InputStream to be read.
* @return the content of the InputStream as a ByteArrayOutputStream.
* @throws IOException
*/
static void getContent(InputStream inputStream, ByteArrayOutputStream outputStream)
throws IOException {
// Read the response into a buffered stream
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
int readChar;
while ((readChar = reader.read()) != -1) {
outputStream.write(readChar);
}
reader.close();
}
}
/**
* Revoke current user's token and reset their session.
*/
public static class DisconnectServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
// Only disconnect a connected user.
String tokenData = (String) request.getSession().getAttribute("token");
if (tokenData == null) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getWriter().print(GSON.toJson("Current user not connected."));
return;
}
try {
// Build credential from stored token data.
GoogleCredential credential = new GoogleCredential.Builder()
.setJsonFactory(JSON_FACTORY)
.setTransport(TRANSPORT)
.setClientSecrets(CLIENT_ID, CLIENT_SECRET).build()
.setFromTokenResponse(JSON_FACTORY.fromString(
tokenData, GoogleTokenResponse.class));
// Execute HTTP GET request to revoke current token.
HttpResponse revokeResponse = TRANSPORT.createRequestFactory()
.buildGetRequest(new GenericUrl(
String.format(
"https://accounts.google.com/o/oauth2/revoke?token=%s",
credential.getAccessToken()))).execute();
// Reset the user's session.
request.getSession().removeAttribute("token");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().print(GSON.toJson("Successfully disconnected."));
} catch (IOException e) {
// For whatever reason, the given token was invalid.
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getWriter().print(GSON.toJson("Failed to revoke token for given user."));
}
}
}
/**
* Get list of people user has shared with this app.
*/
public static class PeopleServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
// Only fetch a list of people for connected users.
String tokenData = (String) request.getSession().getAttribute("token");
if (tokenData == null) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getWriter().print(GSON.toJson("Current user not connected."));
return;
}
try {
// Build credential from stored token data.
GoogleCredential credential = new GoogleCredential.Builder()
.setJsonFactory(JSON_FACTORY)
.setTransport(TRANSPORT)
.setClientSecrets(CLIENT_ID, CLIENT_SECRET).build()
.setFromTokenResponse(JSON_FACTORY.fromString(
tokenData, GoogleTokenResponse.class));
// Create a new authorized API client.
Plus service = new Plus.Builder(TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
// Get a list of people that this user has shared with this app.
PeopleFeed people = service.people().list("me", "visible").execute();
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().print(GSON.toJson(people));
} catch (IOException e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().print(GSON.toJson("Failed to read data from Google. " +
e.getMessage()));
}
}
}
}