Skip to content

Commit caf7230

Browse files
committed
introduce custom nonstandard Facebook AccessTokenErrorResponse
1 parent bdb25f3 commit caf7230

5 files changed

Lines changed: 152 additions & 1 deletion

File tree

changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Remove OAuthRequestAsync, just OAuthRequest. Request should know about sync vs async. Move default Http engine to JDKHttpClient.
33
* introduce SignatureType for OAuth2.0 to implement Bearer signing for the requests
44
* switch Google, GitHub, Facebook OAuth2.0 oauth requests signing to more secured recommended variant (GET-param -> header Bearer)
5+
* introduce custom nonstandard Facebook AccessTokenErrorResponse
56

67
[3.4.1]
78
* Drop deprecated methods

scribejava-apis/src/main/java/com/github/scribejava/apis/FacebookApi.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.github.scribejava.apis;
22

3+
import com.github.scribejava.apis.facebook.FacebookAccessTokenJsonExtractor;
34
import com.github.scribejava.core.builder.api.DefaultApi20;
5+
import com.github.scribejava.core.extractors.TokenExtractor;
6+
import com.github.scribejava.core.model.OAuth2AccessToken;
47
import com.github.scribejava.core.model.Verb;
58

69
/**
@@ -39,4 +42,9 @@ public String getRefreshTokenEndpoint() {
3942
protected String getAuthorizationBaseUrl() {
4043
return "https://www.facebook.com/v2.8/dialog/oauth";
4144
}
45+
46+
@Override
47+
public TokenExtractor<OAuth2AccessToken> getAccessTokenExtractor() {
48+
return FacebookAccessTokenJsonExtractor.instance();
49+
}
4250
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.github.scribejava.apis.facebook;
2+
3+
import com.github.scribejava.core.exceptions.OAuthException;
4+
import java.util.Objects;
5+
6+
/**
7+
* non standard Facebook replace for {@link com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse}
8+
*
9+
* examples:<br>
10+
*
11+
* '{"error":{"message":"This authorization code has been
12+
* used.","type":"OAuthException","code":100,"fbtrace_id":"DtxvtGRaxbB"}}'<br>
13+
*
14+
* '{"error":{"message":"Error validating application. Invalid application
15+
* ID.","type":"OAuthException","code":101,"fbtrace_id":"CvDR+X4WWIx"}}'
16+
*/
17+
public class FacebookAccessTokenErrorResponse extends OAuthException {
18+
19+
private static final long serialVersionUID = -1277129766099856895L;
20+
21+
private final String type;
22+
private final String code;
23+
private final String fbtraceId;
24+
private final String rawResponse;
25+
26+
public FacebookAccessTokenErrorResponse(String message, String type, String code, String fbtraceId,
27+
String rawResponse) {
28+
super(message);
29+
this.type = type;
30+
this.code = code;
31+
this.fbtraceId = fbtraceId;
32+
this.rawResponse = rawResponse;
33+
}
34+
35+
public String getType() {
36+
return type;
37+
}
38+
39+
public String getCode() {
40+
return code;
41+
}
42+
43+
public String getFbtraceId() {
44+
return fbtraceId;
45+
}
46+
47+
public String getRawResponse() {
48+
return rawResponse;
49+
}
50+
51+
@Override
52+
public int hashCode() {
53+
int hash = 5;
54+
hash = 83 * hash + Objects.hashCode(rawResponse);
55+
hash = 83 * hash + Objects.hashCode(getMessage());
56+
hash = 83 * hash + Objects.hashCode(type);
57+
hash = 83 * hash + Objects.hashCode(code);
58+
hash = 83 * hash + Objects.hashCode(fbtraceId);
59+
return hash;
60+
}
61+
62+
@Override
63+
public boolean equals(Object obj) {
64+
if (this == obj) {
65+
return true;
66+
}
67+
if (obj == null) {
68+
return false;
69+
}
70+
if (getClass() != obj.getClass()) {
71+
return false;
72+
}
73+
final FacebookAccessTokenErrorResponse other = (FacebookAccessTokenErrorResponse) obj;
74+
if (!Objects.equals(rawResponse, other.getRawResponse())) {
75+
return false;
76+
}
77+
if (!Objects.equals(getMessage(), other.getMessage())) {
78+
return false;
79+
}
80+
if (!Objects.equals(type, other.getType())) {
81+
return false;
82+
}
83+
if (!Objects.equals(code, other.getCode())) {
84+
return false;
85+
}
86+
return Objects.equals(fbtraceId, other.getFbtraceId());
87+
}
88+
89+
@Override
90+
public String toString() {
91+
return "FacebookAccessTokenErrorResponse{'type'='" + type + "', 'code'='" + code
92+
+ "', 'fbtraceId'='" + fbtraceId + "', 'rawResponse'='" + rawResponse
93+
+ "', 'message'='" + getMessage() + "'}";
94+
}
95+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.github.scribejava.apis.facebook;
2+
3+
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
4+
5+
/**
6+
* non standard Facebook Extractor
7+
*/
8+
public class FacebookAccessTokenJsonExtractor extends OAuth2AccessTokenJsonExtractor {
9+
10+
private static final String MESSAGE_REGEX = "\"message\"\\s*:\\s*\"([^\"]*?)\"";
11+
private static final String TYPE_REGEX = "\"type\"\\s*:\\s*\"([^\"]*?)\"";
12+
private static final String CODE_REGEX = "\"code\"\\s*:\\s*\"?([^\",}]*?)[\",}]";
13+
private static final String FBTRACE_ID_REGEX = "\"fbtrace_id\"\\s*:\\s*\"([^\"]*?)\"";
14+
15+
protected FacebookAccessTokenJsonExtractor() {
16+
}
17+
18+
private static class InstanceHolder {
19+
20+
private static final FacebookAccessTokenJsonExtractor INSTANCE = new FacebookAccessTokenJsonExtractor();
21+
}
22+
23+
public static FacebookAccessTokenJsonExtractor instance() {
24+
return InstanceHolder.INSTANCE;
25+
}
26+
27+
/**
28+
* non standard. examples:<br>
29+
*
30+
* '{"error":{"message":"This authorization code has been
31+
* used.","type":"OAuthException","code":100,"fbtrace_id":"DtxvtGRaxbB"}}'<br>
32+
*
33+
* '{"error":{"message":"Error validating application. Invalid application
34+
* ID.","type":"OAuthException","code":101,"fbtrace_id":"CvDR+X4WWIx"}}'
35+
*/
36+
@Override
37+
protected void generateError(String response) {
38+
extractParameter(response, MESSAGE_REGEX, false);
39+
40+
throw new FacebookAccessTokenErrorResponse(extractParameter(response, MESSAGE_REGEX, false),
41+
extractParameter(response, TYPE_REGEX, false), extractParameter(response, CODE_REGEX, false),
42+
extractParameter(response, FBTRACE_ID_REGEX, false), response);
43+
}
44+
45+
}

scribejava-core/src/main/java/com/github/scribejava/core/extractors/OAuth2AccessTokenJsonExtractor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ public OAuth2AccessToken extract(Response response) throws IOException {
5050

5151
/**
5252
* Related documentation: https://tools.ietf.org/html/rfc6749#section-5.2
53+
*
54+
* @param response response
5355
*/
54-
private static void generateError(String response) {
56+
protected void generateError(String response) {
5557
final String errorInString = extractParameter(response, ERROR_REGEX, true);
5658
final String errorDescription = extractParameter(response, ERROR_DESCRIPTION_REGEX, false);
5759
final String errorUriInString = extractParameter(response, ERROR_URI_REGEX, false);

0 commit comments

Comments
 (0)