-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
DefaultFacebookClient.java
909 lines (784 loc) · 34.5 KB
/
DefaultFacebookClient.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
/*
* Copyright (c) 2010-2020 Mark Allen, Norbert Bartels.
*
* 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.
*/
package com.restfb;
import static com.restfb.logging.RestFBLogger.CLIENT_LOGGER;
import static com.restfb.util.EncodingUtils.decodeBase64;
import static com.restfb.util.ObjectUtil.verifyParameterPresence;
import static com.restfb.util.StringUtils.*;
import static com.restfb.util.UrlUtils.urlEncode;
import static java.lang.String.format;
import static java.net.HttpURLConnection.*;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import com.restfb.WebRequestor.Response;
import com.restfb.batch.BatchRequest;
import com.restfb.batch.BatchResponse;
import com.restfb.exception.*;
import com.restfb.exception.devicetoken.*;
import com.restfb.exception.generator.DefaultFacebookExceptionGenerator;
import com.restfb.exception.generator.FacebookExceptionGenerator;
import com.restfb.json.*;
import com.restfb.scope.ScopeBuilder;
import com.restfb.types.DeviceCode;
import com.restfb.util.EncodingUtils;
import com.restfb.util.ObjectUtil;
import com.restfb.util.StringUtils;
/**
* Default implementation of a <a href="http://developers.facebook.com/docs/api">Facebook Graph API</a> client.
*
* @author <a href="http://restfb.com">Mark Allen</a>
*/
public class DefaultFacebookClient extends BaseFacebookClient implements FacebookClient {
public static final String CLIENT_ID = "client_id";
public static final String APP_ID = "appId";
public static final String APP_SECRET = "appSecret";
public static final String SCOPE = "scope";
public static final String CANNOT_EXTRACT_ACCESS_TOKEN_MESSAGE = "Unable to extract access token from response.";
public static final String PARAM_CLIENT_SECRET = "client_secret";
/**
* Graph API access token.
*/
protected String accessToken;
/**
* Graph API app secret.
*/
private String appSecret;
/**
* facebook exception generator to convert Facebook error json into java exceptions
*/
private FacebookExceptionGenerator graphFacebookExceptionGenerator;
/**
* holds the Facebook endpoint urls
*/
private FacebookEndpoints facebookEndpointUrls = new FacebookEndpoints() {};
/**
* Reserved "multiple IDs" parameter name.
*/
protected static final String IDS_PARAM_NAME = "ids";
/**
* Version of API endpoint.
*/
protected Version apiVersion = Version.UNVERSIONED;
/**
* By default this is <code>false</code>, so real http DELETE is used
*/
protected boolean httpDeleteFallback;
protected DefaultFacebookClient() {
this(Version.LATEST);
}
/**
* Creates a Facebook Graph API client with the given {@code apiVersion}.
*
* @param apiVersion
* Version of the api endpoint
*/
public DefaultFacebookClient(Version apiVersion) {
this(null, null, new DefaultWebRequestor(), new DefaultJsonMapper(), apiVersion);
}
/**
* Creates a Facebook Graph API client with the given {@code accessToken}.
*
* @param accessToken
* A Facebook OAuth access token.
* @param apiVersion
* Version of the api endpoint
* @since 1.6.14
*/
public DefaultFacebookClient(String accessToken, Version apiVersion) {
this(accessToken, null, new DefaultWebRequestor(), new DefaultJsonMapper(), apiVersion);
}
/**
* Creates a Facebook Graph API client with the given {@code accessToken}.
*
* @param accessToken
* A Facebook OAuth access token.
* @param appSecret
* A Facebook application secret.
* @param apiVersion
* Version of the api endpoint
* @since 1.6.14
*/
public DefaultFacebookClient(String accessToken, String appSecret, Version apiVersion) {
this(accessToken, appSecret, new DefaultWebRequestor(), new DefaultJsonMapper(), apiVersion);
}
/**
* Creates a Facebook Graph API client with the given {@code accessToken}.
*
* @param accessToken
* A Facebook OAuth access token.
* @param webRequestor
* The {@link WebRequestor} implementation to use for sending requests to the API endpoint.
* @param jsonMapper
* The {@link JsonMapper} implementation to use for mapping API response JSON to Java objects.
* @param apiVersion
* Version of the api endpoint
* @throws NullPointerException
* If {@code jsonMapper} or {@code webRequestor} is {@code null}.
* @since 1.6.14
*/
public DefaultFacebookClient(String accessToken, WebRequestor webRequestor, JsonMapper jsonMapper,
Version apiVersion) {
this(accessToken, null, webRequestor, jsonMapper, apiVersion);
}
/**
* Creates a Facebook Graph API client with the given {@code accessToken}, {@code webRequestor}, and
* {@code jsonMapper}.
*
* @param accessToken
* A Facebook OAuth access token.
* @param appSecret
* A Facebook application secret.
* @param webRequestor
* The {@link WebRequestor} implementation to use for sending requests to the API endpoint.
* @param jsonMapper
* The {@link JsonMapper} implementation to use for mapping API response JSON to Java objects.
* @param apiVersion
* Version of the api endpoint
* @throws NullPointerException
* If {@code jsonMapper} or {@code webRequestor} is {@code null}.
*/
public DefaultFacebookClient(String accessToken, String appSecret, WebRequestor webRequestor, JsonMapper jsonMapper,
Version apiVersion) {
super();
verifyParameterPresence("jsonMapper", jsonMapper);
verifyParameterPresence("webRequestor", webRequestor);
this.accessToken = trimToNull(accessToken);
this.appSecret = trimToNull(appSecret);
this.webRequestor = webRequestor;
this.jsonMapper = jsonMapper;
this.jsonMapper.setFacebookClient(this);
this.apiVersion = Optional.ofNullable(apiVersion).orElse(Version.UNVERSIONED);
graphFacebookExceptionGenerator = new DefaultFacebookExceptionGenerator();
}
/**
* override the default facebook exception generator to provide a custom handling for the facebook error objects
*
* @param exceptionGenerator
* the custom exception generator implementing the {@link FacebookExceptionGenerator} interface
*/
public void setFacebookExceptionGenerator(FacebookExceptionGenerator exceptionGenerator) {
graphFacebookExceptionGenerator = exceptionGenerator;
}
/**
* fetch the current facebook exception generator implementing the {@link FacebookExceptionGenerator} interface
*
* @return the current facebook exception generator
*/
public FacebookExceptionGenerator getFacebookExceptionGenerator() {
return graphFacebookExceptionGenerator;
}
@Override
public boolean deleteObject(String object, Parameter... parameters) {
verifyParameterPresence("object", object);
String responseString = makeRequest(object, true, true, null, parameters);
try {
JsonValue jObj = Json.parse(responseString);
boolean success = false;
if (jObj.isObject()) {
if (jObj.asObject().contains("success")) {
success = jObj.asObject().get("success").asBoolean();
}
if (jObj.asObject().contains("result")) {
success = jObj.asObject().get("result").asString().contains("Successfully deleted");
}
} else {
success = jObj.asBoolean();
}
return success;
} catch (ParseException jex) {
CLIENT_LOGGER.trace("no valid JSON returned while deleting a object, using returned String instead", jex);
return "true".equals(responseString);
}
}
/**
* @see com.restfb.FacebookClient#fetchConnection(java.lang.String, java.lang.Class, com.restfb.Parameter[])
*/
@Override
public <T> Connection<T> fetchConnection(String connection, Class<T> connectionType, Parameter... parameters) {
verifyParameterPresence("connection", connection);
verifyParameterPresence("connectionType", connectionType);
return new Connection<>(this, makeRequest(connection, parameters), connectionType);
}
/**
* @see com.restfb.FacebookClient#fetchConnectionPage(java.lang.String, java.lang.Class)
*/
@Override
public <T> Connection<T> fetchConnectionPage(final String connectionPageUrl, Class<T> connectionType) {
String connectionJson;
if (!isBlank(accessToken) && !isBlank(appSecret)) {
connectionJson = makeRequestAndProcessResponse(() -> webRequestor.executeGet(String.format("%s&%s=%s", connectionPageUrl,
urlEncode(APP_SECRET_PROOF_PARAM_NAME), obtainAppSecretProof(accessToken, appSecret))));
} else {
connectionJson = makeRequestAndProcessResponse(() -> webRequestor.executeGet(connectionPageUrl));
}
return new Connection<>(this, connectionJson, connectionType);
}
/**
* @see com.restfb.FacebookClient#fetchObject(java.lang.String, java.lang.Class, com.restfb.Parameter[])
*/
@Override
public <T> T fetchObject(String object, Class<T> objectType, Parameter... parameters) {
verifyParameterPresence("object", object);
verifyParameterPresence("objectType", objectType);
return jsonMapper.toJavaObject(makeRequest(object, parameters), objectType);
}
@Override
public FacebookClient createClientWithAccessToken(String accessToken) {
return new DefaultFacebookClient(accessToken, this.appSecret, this.apiVersion);
}
/**
* @see com.restfb.FacebookClient#fetchObjects(java.util.List, java.lang.Class, com.restfb.Parameter[])
*/
@Override
@SuppressWarnings("unchecked")
public <T> T fetchObjects(List<String> ids, Class<T> objectType, Parameter... parameters) {
verifyParameterPresence("ids", ids);
verifyParameterPresence("connectionType", objectType);
if (ids.isEmpty()) {
throw new IllegalArgumentException("The list of IDs cannot be empty.");
}
if (Stream.of(parameters).anyMatch(p -> IDS_PARAM_NAME.equals(p.name))) {
throw new IllegalArgumentException("You cannot specify the '" + IDS_PARAM_NAME + "' URL parameter yourself - "
+ "RestFB will populate this for you with the list of IDs you passed to this method.");
}
JsonArray idArray = new JsonArray();
// Normalize the IDs
for (String id : ids) {
throwIAEonBlankId(id);
idArray.add(id.trim());
}
try {
String jsonString = makeRequest("",
parametersWithAdditionalParameter(Parameter.with(IDS_PARAM_NAME, idArray.toString()), parameters));
return jsonMapper.toJavaObject(jsonString, objectType);
} catch (ParseException e) {
throw new FacebookJsonMappingException("Unable to map connection JSON to Java objects", e);
}
}
private void throwIAEonBlankId(String id) {
if (StringUtils.isBlank(id)) {
throw new IllegalArgumentException("The list of IDs cannot contain blank strings.");
}
}
/**
* @see com.restfb.FacebookClient#publish(java.lang.String, java.lang.Class, com.restfb.BinaryAttachment,
* com.restfb.Parameter[])
*/
@Override
public <T> T publish(String connection, Class<T> objectType, List<BinaryAttachment> binaryAttachments,
Parameter... parameters) {
verifyParameterPresence("connection", connection);
return jsonMapper.toJavaObject(makeRequest(connection, true, false, binaryAttachments, parameters), objectType);
}
/**
* @see com.restfb.FacebookClient#publish(java.lang.String, java.lang.Class, com.restfb.BinaryAttachment,
* com.restfb.Parameter[])
*/
@Override
public <T> T publish(String connection, Class<T> objectType, BinaryAttachment binaryAttachment,
Parameter... parameters) {
List<BinaryAttachment> attachments = Optional.ofNullable(binaryAttachment).map(Collections::singletonList).orElse(null);
return publish(connection, objectType, attachments, parameters);
}
/**
* @see com.restfb.FacebookClient#publish(java.lang.String, java.lang.Class, com.restfb.Parameter[])
*/
@Override
public <T> T publish(String connection, Class<T> objectType, Parameter... parameters) {
return publish(connection, objectType, (List<BinaryAttachment>) null, parameters);
}
@Override
public String getLogoutUrl(String next) {
String parameterString;
if (next != null) {
Parameter p = Parameter.with("next", next);
parameterString = toParameterString(false, p);
} else {
parameterString = toParameterString(false);
}
final String fullEndPoint = createEndpointForApiCall("logout.php", false);
return fullEndPoint + "?" + parameterString;
}
/**
* @see com.restfb.FacebookClient#executeBatch(com.restfb.batch.BatchRequest[])
*/
@Override
public List<BatchResponse> executeBatch(BatchRequest... batchRequests) {
return executeBatch(asList(batchRequests), Collections.emptyList());
}
/**
* @see com.restfb.FacebookClient#executeBatch(java.util.List)
*/
@Override
public List<BatchResponse> executeBatch(List<BatchRequest> batchRequests) {
return executeBatch(batchRequests, Collections.emptyList());
}
/**
* @see com.restfb.FacebookClient#executeBatch(java.util.List, java.util.List)
*/
@Override
public List<BatchResponse> executeBatch(List<BatchRequest> batchRequests, List<BinaryAttachment> binaryAttachments) {
verifyParameterPresence("binaryAttachments", binaryAttachments);
if (batchRequests == null || batchRequests.isEmpty()) {
throw new IllegalArgumentException("You must specify at least one batch request.");
}
return jsonMapper.toJavaList(
makeRequest("", true, false, binaryAttachments, Parameter.with("batch", jsonMapper.toJson(batchRequests, true))),
BatchResponse.class);
}
/**
* @see com.restfb.FacebookClient#convertSessionKeysToAccessTokens(java.lang.String, java.lang.String,
* java.lang.String[])
*/
@Override
public List<AccessToken> convertSessionKeysToAccessTokens(String appId, String secretKey, String... sessionKeys) {
verifyParameterPresence(APP_ID, appId);
verifyParameterPresence("secretKey", secretKey);
if (sessionKeys == null || sessionKeys.length == 0) {
return emptyList();
}
String json = makeRequest("/oauth/exchange_sessions", true, false, null, Parameter.with(CLIENT_ID, appId),
Parameter.with(PARAM_CLIENT_SECRET, secretKey), Parameter.with("sessions", String.join(",", sessionKeys)));
return jsonMapper.toJavaList(json, AccessToken.class);
}
/**
* @see com.restfb.FacebookClient#obtainAppAccessToken(java.lang.String, java.lang.String)
*/
@Override
public AccessToken obtainAppAccessToken(String appId, String appSecret) {
verifyParameterPresence(APP_ID, appId);
verifyParameterPresence(APP_SECRET, appSecret);
String response = makeRequest("oauth/access_token", Parameter.with("grant_type", "client_credentials"),
Parameter.with(CLIENT_ID, appId), Parameter.with(PARAM_CLIENT_SECRET, appSecret));
try {
return getAccessTokenFromResponse(response);
} catch (Exception t) {
throw new FacebookResponseContentException(CANNOT_EXTRACT_ACCESS_TOKEN_MESSAGE, t);
}
}
@Override
public DeviceCode fetchDeviceCode(ScopeBuilder scope) {
verifyParameterPresence(SCOPE, scope);
ObjectUtil.requireNotNull(accessToken, () -> new IllegalStateException("access token is required to fetch a device access token"));
String response = makeRequest("device/login", true, false, null, Parameter.with("type", "device_code"),
Parameter.with(SCOPE, scope.toString()));
return jsonMapper.toJavaObject(response, DeviceCode.class);
}
@Override
public AccessToken obtainDeviceAccessToken(String code) throws FacebookDeviceTokenCodeExpiredException,
FacebookDeviceTokenPendingException, FacebookDeviceTokenDeclinedException, FacebookDeviceTokenSlowdownException {
verifyParameterPresence("code", code);
ObjectUtil.requireNotNull(accessToken, () -> new IllegalStateException("access token is required to fetch a device access token"));
try {
String response = makeRequest("device/login_status", true, false, null, Parameter.with("type", "device_token"),
Parameter.with("code", code));
return getAccessTokenFromResponse(response);
} catch (FacebookOAuthException foae) {
DeviceTokenExceptionFactory.createFrom(foae);
return null;
}
}
/**
* @see com.restfb.FacebookClient#obtainUserAccessToken(java.lang.String, java.lang.String, java.lang.String,
* java.lang.String)
*/
@Override
public AccessToken obtainUserAccessToken(String appId, String appSecret, String redirectUri,
String verificationCode) {
verifyParameterPresence(APP_ID, appId);
verifyParameterPresence(APP_SECRET, appSecret);
verifyParameterPresence("verificationCode", verificationCode);
String response = makeRequest("oauth/access_token", Parameter.with(CLIENT_ID, appId),
Parameter.with(PARAM_CLIENT_SECRET, appSecret), Parameter.with("code", verificationCode),
Parameter.with("redirect_uri", redirectUri));
try {
return getAccessTokenFromResponse(response);
} catch (Exception t) {
throw new FacebookResponseContentException(CANNOT_EXTRACT_ACCESS_TOKEN_MESSAGE, t);
}
}
/**
* @see com.restfb.FacebookClient#obtainExtendedAccessToken(java.lang.String, java.lang.String)
*/
@Override
public AccessToken obtainExtendedAccessToken(String appId, String appSecret) {
ObjectUtil.requireNotNull(accessToken,
() -> new IllegalStateException(
format("You cannot call this method because you did not construct this instance of %s with an access token.",
getClass().getSimpleName())));
return obtainExtendedAccessToken(appId, appSecret, accessToken);
}
/**
* @see com.restfb.FacebookClient#obtainExtendedAccessToken(java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public AccessToken obtainExtendedAccessToken(String appId, String appSecret, String accessToken) {
verifyParameterPresence(APP_ID, appId);
verifyParameterPresence(APP_SECRET, appSecret);
verifyParameterPresence("accessToken", accessToken);
String response = makeRequest("/oauth/access_token", false, false, null, Parameter.with(CLIENT_ID, appId),
Parameter.with(PARAM_CLIENT_SECRET, appSecret), Parameter.with("grant_type", "fb_exchange_token"),
Parameter.with("fb_exchange_token", accessToken));
try {
return getAccessTokenFromResponse(response);
} catch (Exception t) {
throw new FacebookResponseContentException(CANNOT_EXTRACT_ACCESS_TOKEN_MESSAGE, t);
}
}
private AccessToken getAccessTokenFromResponse(String response) {
AccessToken token;
try {
token = getJsonMapper().toJavaObject(response, AccessToken.class);
} catch (FacebookJsonMappingException fjme) {
CLIENT_LOGGER.trace("could not map response to access token class try to fetch directly from String", fjme);
token = AccessToken.fromQueryString(response);
}
token.setClient(createClientWithAccessToken(token.getAccessToken()));
return token;
}
@Override
@SuppressWarnings("unchecked")
public <T> T parseSignedRequest(String signedRequest, String appSecret, Class<T> objectType) {
verifyParameterPresence("signedRequest", signedRequest);
verifyParameterPresence(APP_SECRET, appSecret);
verifyParameterPresence("objectType", objectType);
String[] signedRequestTokens = signedRequest.split("[.]");
if (signedRequestTokens.length != 2) {
throw new FacebookSignedRequestParsingException(format(
"Signed request '%s' is expected to be signature and payload strings separated by a '.'", signedRequest));
}
String encodedSignature = signedRequestTokens[0];
String urlDecodedSignature = urlDecodeSignedRequestToken(encodedSignature);
byte[] signature = decodeBase64(urlDecodedSignature);
String encodedPayload = signedRequestTokens[1];
String urlDecodedPayload = urlDecodeSignedRequestToken(encodedPayload);
String payload = StringUtils.toString(decodeBase64(urlDecodedPayload));
// Convert payload to a JsonObject so we can pull algorithm data out of it
JsonObject payloadObject = getJsonMapper().toJavaObject(payload, JsonObject.class);
if (!payloadObject.contains("algorithm")) {
throw new FacebookSignedRequestParsingException("Unable to detect algorithm used for signed request");
}
String algorithm = payloadObject.getString("algorithm", null);
if (!verifySignedRequest(appSecret, algorithm, encodedPayload, signature)) {
throw new FacebookSignedRequestVerificationException(
"Signed request verification failed. Are you sure the request was made for the app identified by the app secret you provided?");
}
// Marshal to the user's preferred type.
// If the user asked for a JsonObject, send back the one we already parsed.
return objectType.equals(JsonObject.class) ? (T) payloadObject : getJsonMapper().toJavaObject(payload, objectType);
}
/**
* Decodes a component of a signed request received from Facebook using FB's special URL-encoding strategy.
*
* @param signedRequestToken
* Token to decode.
* @return The decoded token.
*/
protected String urlDecodeSignedRequestToken(String signedRequestToken) {
verifyParameterPresence("signedRequestToken", signedRequestToken);
return signedRequestToken.replace("-", "+").replace("_", "/").trim();
}
@Override
public String getLoginDialogUrl(String appId, String redirectUri, ScopeBuilder scope, Parameter... parameters) {
verifyParameterPresence(APP_ID, appId);
verifyParameterPresence("redirectUri", redirectUri);
verifyParameterPresence(SCOPE, scope);
String dialogUrl = getFacebookEndpointUrls().getFacebookEndpoint() + "/dialog/oauth";
List<Parameter> parameterList = new ArrayList<>();
parameterList.add(Parameter.with(CLIENT_ID, appId));
parameterList.add(Parameter.with("redirect_uri", redirectUri));
parameterList.add(Parameter.with(SCOPE, scope.toString()));
// add optional parameters
Collections.addAll(parameterList, parameters);
return dialogUrl + "?" + toParameterString(false, parameterList.toArray(new Parameter[0]));
}
/**
* Verifies that the signed request is really from Facebook.
*
* @param appSecret
* The secret for the app that can verify this signed request.
* @param algorithm
* Signature algorithm specified by FB in the decoded payload.
* @param encodedPayload
* The encoded payload used to generate a signature for comparison against the provided {@code signature}.
* @param signature
* The decoded signature extracted from the signed request. Compared against a signature generated from
* {@code encodedPayload}.
* @return {@code true} if the signed request is verified, {@code false} if not.
*/
protected boolean verifySignedRequest(String appSecret, String algorithm, String encodedPayload, byte[] signature) {
verifyParameterPresence(APP_SECRET, appSecret);
verifyParameterPresence("algorithm", algorithm);
verifyParameterPresence("encodedPayload", encodedPayload);
verifyParameterPresence("signature", signature);
// Normalize algorithm name...FB calls it differently than Java does
if ("HMAC-SHA256".equalsIgnoreCase(algorithm)) {
algorithm = "HMACSHA256";
}
try {
Mac mac = Mac.getInstance(algorithm);
mac.init(new SecretKeySpec(toBytes(appSecret), algorithm));
byte[] payloadSignature = mac.doFinal(toBytes(encodedPayload));
return Arrays.equals(signature, payloadSignature);
} catch (Exception e) {
throw new FacebookSignedRequestVerificationException("Unable to perform signed request verification", e);
}
}
/**
* @see com.restfb.FacebookClient#debugToken(java.lang.String)
*/
@Override
public DebugTokenInfo debugToken(String inputToken) {
verifyParameterPresence("inputToken", inputToken);
String response = makeRequest("/debug_token", Parameter.with("input_token", inputToken));
try {
JsonObject json = Json.parse(response).asObject();
JsonObject data = json.get("data").asObject();
return getJsonMapper().toJavaObject(data.toString(), DebugTokenInfo.class);
} catch (Exception t) {
throw new FacebookResponseContentException("Unable to parse JSON from response.", t);
}
}
/**
* @see com.restfb.FacebookClient#getJsonMapper()
*/
@Override
public JsonMapper getJsonMapper() {
return jsonMapper;
}
/**
* @see com.restfb.FacebookClient#getWebRequestor()
*/
@Override
public WebRequestor getWebRequestor() {
return webRequestor;
}
/**
* Coordinates the process of executing the API request GET/POST and processing the response we receive from the
* endpoint.
*
* @param endpoint
* Facebook Graph API endpoint.
* @param parameters
* Arbitrary number of parameters to send along to Facebook as part of the API call.
* @return The JSON returned by Facebook for the API call.
* @throws FacebookException
* If an error occurs while making the Facebook API POST or processing the response.
*/
protected String makeRequest(String endpoint, Parameter... parameters) {
return makeRequest(endpoint, false, false, null, parameters);
}
/**
* Coordinates the process of executing the API request GET/POST and processing the response we receive from the
* endpoint.
*
* @param endpoint
* Facebook Graph API endpoint.
* @param executeAsPost
* {@code true} to execute the web request as a {@code POST}, {@code false} to execute as a {@code GET}.
* @param executeAsDelete
* {@code true} to add a special 'treat this request as a {@code DELETE}' parameter.
* @param binaryAttachments
* A list of binary files to include in a {@code POST} request. Pass {@code null} if no attachment should be
* sent.
* @param parameters
* Arbitrary number of parameters to send along to Facebook as part of the API call.
* @return The JSON returned by Facebook for the API call.
* @throws FacebookException
* If an error occurs while making the Facebook API POST or processing the response.
*/
protected String makeRequest(String endpoint, final boolean executeAsPost, final boolean executeAsDelete,
final List<BinaryAttachment> binaryAttachments, Parameter... parameters) {
verifyParameterLegality(parameters);
if (executeAsDelete && isHttpDeleteFallback()) {
parameters = parametersWithAdditionalParameter(Parameter.with(METHOD_PARAM_NAME, "delete"), parameters);
}
if (!endpoint.startsWith("/")) {
endpoint = "/" + endpoint;
}
final String fullEndpoint =
createEndpointForApiCall(endpoint, binaryAttachments != null && !binaryAttachments.isEmpty());
final String parameterString = toParameterString(parameters);
return makeRequestAndProcessResponse(() -> {
if (executeAsDelete && !isHttpDeleteFallback()) {
return webRequestor.executeDelete(fullEndpoint + "?" + parameterString);
}
if (executeAsPost) {
return webRequestor.executePost(fullEndpoint, parameterString, binaryAttachments);
}
return webRequestor.executeGet(fullEndpoint + "?" + parameterString);
});
}
/**
* @see com.restfb.FacebookClient#obtainAppSecretProof(java.lang.String, java.lang.String)
*/
@Override
public String obtainAppSecretProof(String accessToken, String appSecret) {
verifyParameterPresence("accessToken", accessToken);
verifyParameterPresence(APP_SECRET, appSecret);
return EncodingUtils.encodeAppSecretProof(appSecret, accessToken);
}
/**
* returns if the fallback post method (<code>true</code>) is used or the http delete (<code>false</code>)
*
* @return
*/
public boolean isHttpDeleteFallback() {
return httpDeleteFallback;
}
/**
* Set to <code>true</code> if the facebook http delete fallback should be used. Facebook allows to use the http POST
* with the parameter "method=delete" to override the post and use delete instead. This feature allow http client that
* do not support the whole http method set, to delete objects from facebook
*
* @param httpDeleteFallback
* <code>true</code> if the the http Delete Fallback is used
*/
public void setHttpDeleteFallback(boolean httpDeleteFallback) {
this.httpDeleteFallback = httpDeleteFallback;
}
protected interface Requestor {
Response makeRequest() throws IOException;
}
protected String makeRequestAndProcessResponse(Requestor requestor) {
Response response;
// Perform a GET or POST to the API endpoint
try {
response = requestor.makeRequest();
} catch (Exception t) {
throw new FacebookNetworkException(t);
}
// If we get any HTTP response code other than a 200 OK or 400 Bad Request
// or 401 Not Authorized or 403 Forbidden or 404 Not Found or 500 Internal
// Server Error or 302 Not Modified
// throw an exception.
if (HTTP_OK != response.getStatusCode() && HTTP_BAD_REQUEST != response.getStatusCode()
&& HTTP_UNAUTHORIZED != response.getStatusCode() && HTTP_NOT_FOUND != response.getStatusCode()
&& HTTP_INTERNAL_ERROR != response.getStatusCode() && HTTP_FORBIDDEN != response.getStatusCode()
&& HTTP_NOT_MODIFIED != response.getStatusCode()) {
throw new FacebookNetworkException(response.getStatusCode());
}
String json = response.getBody();
try {
// If the response contained an error code, throw an exception.
getFacebookExceptionGenerator().throwFacebookResponseStatusExceptionIfNecessary(json, response.getStatusCode());
} catch (FacebookErrorMessageException feme) {
Optional.ofNullable(getWebRequestor()).map(WebRequestor::getDebugHeaderInfo).ifPresent(feme::setDebugHeaderInfo);
throw feme;
}
// If there was no response error information and this was a 500 or 401
// error, something weird happened on Facebook's end. Bail.
if (HTTP_INTERNAL_ERROR == response.getStatusCode() || HTTP_UNAUTHORIZED == response.getStatusCode()) {
throw new FacebookNetworkException(response.getStatusCode());
}
return json;
}
/**
* Generate the parameter string to be included in the Facebook API request.
*
* @param parameters
* Arbitrary number of extra parameters to include in the request.
* @return The parameter string to include in the Facebook API request.
* @throws FacebookJsonMappingException
* If an error occurs when building the parameter string.
*/
protected String toParameterString(Parameter... parameters) {
return toParameterString(true, parameters);
}
/**
* Generate the parameter string to be included in the Facebook API request.
*
* @param withJsonParameter
* add additional parameter format with type json
* @param parameters
* Arbitrary number of extra parameters to include in the request.
* @return The parameter string to include in the Facebook API request.
* @throws FacebookJsonMappingException
* If an error occurs when building the parameter string.
*/
protected String toParameterString(boolean withJsonParameter, Parameter... parameters) {
if (!isBlank(accessToken)) {
parameters = parametersWithAdditionalParameter(Parameter.with(ACCESS_TOKEN_PARAM_NAME, accessToken), parameters);
}
if (!isBlank(accessToken) && !isBlank(appSecret)) {
parameters = parametersWithAdditionalParameter(
Parameter.with(APP_SECRET_PROOF_PARAM_NAME, obtainAppSecretProof(accessToken, appSecret)), parameters);
}
if (withJsonParameter) {
parameters = parametersWithAdditionalParameter(Parameter.with(FORMAT_PARAM_NAME, "json"), parameters);
}
return Stream.of(parameters).map(p -> urlEncode(p.name) + "=" + urlEncodedValueForParameterName(p.name, p.value))
.collect(Collectors.joining("&"));
}
/**
* @see com.restfb.BaseFacebookClient#createEndpointForApiCall(java.lang.String,boolean)
*/
@Override
protected String createEndpointForApiCall(String apiCall, boolean hasAttachment) {
while (apiCall.startsWith("/"))
apiCall = apiCall.substring(1);
String baseUrl = getFacebookGraphEndpointUrl();
if (hasAttachment && (apiCall.endsWith("/videos") || apiCall.endsWith("/advideos"))) {
baseUrl = getFacebookGraphVideoEndpointUrl();
} else if (apiCall.endsWith("logout.php")) {
baseUrl = getFacebookEndpointUrls().getFacebookEndpoint();
}
return format("%s/%s", baseUrl, apiCall);
}
/**
* Returns the base endpoint URL for the Graph API.
*
* @return The base endpoint URL for the Graph API.
*/
protected String getFacebookGraphEndpointUrl() {
if (apiVersion.isUrlElementRequired()) {
return getFacebookEndpointUrls().getGraphEndpoint() + '/' + apiVersion.getUrlElement();
} else {
return getFacebookEndpointUrls().getGraphEndpoint();
}
}
/**
* Returns the base endpoint URL for the Graph API's video upload functionality.
*
* @return The base endpoint URL for the Graph API's video upload functionality.
* @since 1.6.5
*/
protected String getFacebookGraphVideoEndpointUrl() {
if (apiVersion.isUrlElementRequired()) {
return getFacebookEndpointUrls().getGraphVideoEndpoint() + '/' + apiVersion.getUrlElement();
} else {
return getFacebookEndpointUrls().getGraphVideoEndpoint();
}
}
public FacebookEndpoints getFacebookEndpointUrls() {
return facebookEndpointUrls;
}
public void setFacebookEndpointUrls(FacebookEndpoints facebookEndpointUrls) {
this.facebookEndpointUrls = facebookEndpointUrls;
}
}