-
Notifications
You must be signed in to change notification settings - Fork 3
/
HandlerLoginFacebook.java
390 lines (317 loc) · 12.6 KB
/
HandlerLoginFacebook.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**
* "First, solve the problem. Then, write the code. -John Johnson"
* "Or use Vangav M"
* www.vangav.com
* */
/**
* MIT License
*
* Copyright (c) 2016 Vangav
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
* */
/**
* Community
* Facebook Group: Vangav Open Source - Backend
* fb.com/groups/575834775932682/
* Facebook Page: Vangav
* fb.com/vangav.f
*
* Third party communities for Vangav Backend
* - play framework
* - cassandra
* - datastax
*
* Tag your question online (e.g.: stack overflow, etc ...) with
* #vangav_backend
* to easier find questions/answers online
* */
package com.vangav.vos_instagram.controllers.login_facebook;
import java.net.HttpURLConnection;
import java.util.Calendar;
import java.util.Map;
import java.util.UUID;
import com.datastax.driver.core.BatchStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.BatchStatement.Type;
import com.vangav.backend.cassandra.Cassandra;
import com.vangav.backend.cassandra.formatting.CalendarFormatterInl;
import com.vangav.backend.content.formatting.EncodingInl;
import com.vangav.backend.data_structures_and_algorithms.tuple.Pair;
import com.vangav.backend.exceptions.CodeException;
import com.vangav.backend.exceptions.VangavException.ExceptionClass;
import com.vangav.backend.metrics.time.TimeUnitType;
import com.vangav.backend.networks.rest_client.RestResponseJson;
import com.vangav.backend.play_framework.request.Request;
import com.vangav.backend.play_framework.request.RequestJsonBody;
import com.vangav.backend.play_framework.request.RequestState;
import com.vangav.backend.play_framework.request.response.ResponseBody;
import com.vangav.backend.public_apis.facebook.FacebookGraph;
import com.vangav.backend.public_apis.facebook.json.fields.FacebookGraphApiFieldType;
import com.vangav.backend.public_apis.facebook.json.fields.Name;
import com.vangav.backend.security.authentication.facebook.FacebookAuthInl;
import com.vangav.backend.security.authentication.o_auth_2.OAuth2Tokens;
import com.vangav.vos_instagram.cassandra_keyspaces.ig_analytics.AnnualRegionalCounters;
import com.vangav.vos_instagram.cassandra_keyspaces.ig_analytics.DailyRegionalCounters;
import com.vangav.vos_instagram.cassandra_keyspaces.ig_app_data.UsersIndex;
import com.vangav.vos_instagram.cassandra_keyspaces.ig_app_data.UsersInfo;
import com.vangav.vos_instagram.cassandra_keyspaces.ig_auth.AuthCodes;
import com.vangav.vos_instagram.cassandra_keyspaces.ig_auth.FacebookIds;
import com.vangav.vos_instagram.cassandra_keyspaces.ig_auth.UsersCredIds;
import com.vangav.vos_instagram.cassandra_keyspaces.ig_blobs.ProfilePicturesBlobs;
import com.vangav.vos_instagram.common.Constants;
import com.vangav.vos_instagram.controllers.CommonPlayHandler;
/**
* GENERATED using ControllersGeneratorMain.java
*/
/**
* HandlerLoginFacebook
* handles request-to-response processing
* also handles after response processing (if any)
* */
public class HandlerLoginFacebook extends CommonPlayHandler {
private static final String kName = "LoginFacebook";
@Override
protected String getName () {
return kName;
}
@Override
protected RequestJsonBody getRequestJson () {
return new RequestLoginFacebook();
}
@Override
protected ResponseBody getResponseBody () {
return new ResponseLoginFacebook();
}
@Override
protected void authenticateRequest (
final Request request) throws Exception {
// get request's body
RequestLoginFacebook requestLoginFacebook =
(RequestLoginFacebook)request.getRequestJsonBody();
// authenticate using Facebook Graph API
FacebookAuthInl.validateFacebookAccessToken(
requestLoginFacebook.fb_access_token,
Constants.kFacebookAppId);
}
private boolean isSignup = false;
@Override
protected void processRequest (final Request request) throws Exception {
// use the following request Object to process the request and set
// the response to be returned
RequestLoginFacebook requestLoginFacebook =
(RequestLoginFacebook)request.getRequestJsonBody();
// get user's facebook id
FacebookGraph facebookGraph =
new FacebookGraph(requestLoginFacebook.fb_access_token);
String facebookId = facebookGraph.getUserId();
// check if that's a login or signup request
ResultSet resultSet =
FacebookIds.i().executeSyncSelect(facebookId);
if (resultSet.isExhausted() == false) {
// LOGIN
// get user_id
UUID userId =
resultSet.one().getUUID(FacebookIds.kUserIdColumnName);
// set user's id
request.setUserId(userId);
// generate new authentication tokens
OAuth2Tokens oAuth2Tokens = new OAuth2Tokens();
// insert into ig_auth.auth_codes
AuthCodes.i().executeSyncInsert(
userId,
requestLoginFacebook.device_token,
oAuth2Tokens.getAuthorizationCode(),
oAuth2Tokens.getAccessToken(),
oAuth2Tokens.getRefreshToken(),
((int)Constants.kAuthCodeLifeTime.getAs(
TimeUnitType.SECOND).getValue() ) );
// set response and return
((ResponseLoginFacebook)request.getResponseBody() ).set(
requestLoginFacebook.request_tracking_id,
userId.toString(),
oAuth2Tokens.getAuthorizationCode(),
false);
return;
} else {
// SIGN UP
// get user's info from facebook
// query Facebook Graph API for name
Map<
FacebookGraphApiFieldType,
Pair<Integer, RestResponseJson> > facebookGraphApiResponse =
facebookGraph.getFieldsSync(FacebookGraphApiFieldType.NAME);
// error communicating with Facebook Graph API?
if (facebookGraphApiResponse.get(
FacebookGraphApiFieldType.NAME).getFirst() !=
HttpURLConnection.HTTP_OK) {
throw new CodeException(
422,
1,
"Couldn't get user's name from Facebook Graph API. "
+ "Facebook access token ["
+ requestLoginFacebook.fb_access_token
+ "], request issued from device_token ["
+ requestLoginFacebook.device_token
+ "]. Http Status code ["
+ facebookGraphApiResponse.get(
FacebookGraphApiFieldType.NAME).getFirst()
+ "], response ["
+ facebookGraphApiResponse.get(
FacebookGraphApiFieldType.NAME).getSecond().toString()
+ "]",
ExceptionClass.COMMUNICATION);
}
// extract name field
Name nameField =
((Name)facebookGraphApiResponse.get(
FacebookGraphApiFieldType.NAME).getSecond() );
// get name
String name = nameField.name;
// get user's Facebook profile picture
String profilePicture =
facebookGraph.getProfilePictureSync(
Constants.kFacebookProfilePictureDimension);
// new user_id
UUID userId = UUID.randomUUID();
// set user's id
request.setUserId(userId);
// generate new authentication tokens
OAuth2Tokens oAuth2Tokens = new OAuth2Tokens();
// database queries
// insert into ig_blobs.profile_pictures_blobs
ProfilePicturesBlobs.i().executeSyncInsert(
userId,
EncodingInl.encodeStringIntoByteBuffer(profilePicture) );
// all queries must succeed - ig_auth
BatchStatement batchStatement = new BatchStatement(Type.LOGGED);
// insert into ig_auth.auth_codes
batchStatement.add(
AuthCodes.i().getBoundStatementInsert(
userId,
requestLoginFacebook.device_token,
oAuth2Tokens.getAuthorizationCode(),
oAuth2Tokens.getAccessToken(),
oAuth2Tokens.getRefreshToken(),
((int)Constants.kAuthCodeLifeTime.getAs(
TimeUnitType.SECOND).getValue() ) ) );
// insert into ig_auth.facebook_ids
batchStatement.add(
FacebookIds.i().getBoundStatementInsert(
facebookId,
userId) );
// set facebook_id in ig_auth.users_cred_ids
batchStatement.add(
UsersCredIds.i().getBoundStatementSetFacebookId(
facebookId,
userId) );
try {
// execute batch statement
Cassandra.i().executeSync(batchStatement);
} catch (Exception e) {
// failed, undo successful queries and re-throw
ProfilePicturesBlobs.i().executeSyncDelete(userId);
throw e;
}
// all queries must succeed - ig_app_data
batchStatement = new BatchStatement(Type.LOGGED);
// insert into ig_app_data.users_index
batchStatement.add(
UsersIndex.i().getBoundStatementInsert(
CalendarFormatterInl.concatCalendarFields(
request.getStartCalendar(),
Calendar.YEAR,
Calendar.MONTH,
Calendar.DAY_OF_MONTH),
request.getStartTime(),
userId) );
// insert into ig_app_data.users_info
batchStatement.add(
UsersInfo.i().getBoundStatementInsert(
userId,
name,
request.getStartTime(),
request.getStartTime() ) );
// set profile_picture_id in ig_app_data.users_info
batchStatement.add(
UsersInfo.i().getBoundStatementSetProfilePictureId(
userId,
userId) );
try {
// execute batch statement
Cassandra.i().executeSync(batchStatement);
} catch (Exception e) {
// failed, undo successful queries and re-throw
ProfilePicturesBlobs.i().executeSyncDelete(userId);
batchStatement = new BatchStatement(Type.LOGGED);
batchStatement.add(
AuthCodes.i().getBoundStatementDelete(
userId,
requestLoginFacebook.device_token) );
batchStatement.add(
FacebookIds.i().getBoundStatementDelete(
facebookId) );
batchStatement.add(
UsersCredIds.i().getBoundStatementDelete(
userId) );
// execute batch statement
Cassandra.i().executeSync(batchStatement);
throw e;
}
// set response and return
((ResponseLoginFacebook)request.getResponseBody() ).set(
requestLoginFacebook.request_tracking_id,
userId.toString(),
oAuth2Tokens.getAuthorizationCode(),
true);
this.isSignup = true;
return;
} // SIGN UP
}
@Override
protected void dispatchAnalysis (
final Request request) throws Exception {
// only process this method if user's request was a success
if (request.getState() != RequestState.OK) {
return;
}
// only increment new users count if this was a sign up
if (this.isSignup == false) {
return;
}
// ig_analytics.annual_regional_counters.increment_new_users
request.getDispatcher().addDispatchMessage(
AnnualRegionalCounters.i().getQueryDispatchableIncrementNewUsers(
CalendarFormatterInl.concatCalendarFields(
request.getStartCalendar(),
Calendar.YEAR)
+ Constants.kCassandraIdConcat
+ Constants.kWorldRegion) );
// ig_analytics.daily_regional_counters.increment_new_users
request.getDispatcher().addDispatchMessage(
DailyRegionalCounters.i().getQueryDispatchableIncrementNewUsers(
CalendarFormatterInl.concatCalendarFields(
request.getStartCalendar(),
Calendar.YEAR,
Calendar.MONTH,
Calendar.DAY_OF_MONTH)
+ Constants.kCassandraIdConcat
+ Constants.kWorldRegion) );
}
}