Skip to content

Commit 030d768

Browse files
committed
add all OAuth error codes from supported RFCs (incl. "invalid_token") (thanks to https://github.com/echorebel)
1 parent bc796e4 commit 030d768

File tree

7 files changed

+146
-10
lines changed

7 files changed

+146
-10
lines changed

changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[SNAPSHOT]
22
* add PMD checks on compile
3+
* add all OAuth error codes from supported RFCs (incl. "invalid_token") (thanks to https://github.com/echorebel)
34

45
[6.5.1]
56
* cleanup deprecates methods

scribejava-apis/src/main/java/com/github/scribejava/apis/fitbit/FitBitJsonTokenExtractor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
44
import com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse;
5+
import com.github.scribejava.core.oauth2.OAuth2Error;
56

67
import java.util.regex.Pattern;
78

@@ -37,9 +38,9 @@ public void generateError(String response) {
3738
final String errorInString = extractParameter(response, ERROR_REGEX_PATTERN, true);
3839
final String errorDescription = extractParameter(response, ERROR_DESCRIPTION_REGEX_PATTERN, false);
3940

40-
OAuth2AccessTokenErrorResponse.ErrorCode errorCode;
41+
OAuth2Error errorCode;
4142
try {
42-
errorCode = OAuth2AccessTokenErrorResponse.ErrorCode.parseFrom(errorInString);
43+
errorCode = OAuth2Error.parseFrom(errorInString);
4344
} catch (IllegalArgumentException iaE) {
4445
//non oauth standard error code
4546
errorCode = null;

scribejava-apis/src/test/java/com/github/scribejava/apis/fitbit/FitBitJsonTokenExtractorTest.java

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

33
import com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse;
4-
import com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse.ErrorCode;
4+
import com.github.scribejava.core.oauth2.OAuth2Error;
55

66
import org.hamcrest.FeatureMatcher;
77
import org.junit.Rule;
@@ -28,21 +28,21 @@ public void testErrorExtraction() {
2828
final FitBitJsonTokenExtractor extractor = new FitBitJsonTokenExtractor();
2929

3030
thrown.expect(OAuth2AccessTokenErrorResponse.class);
31-
thrown.expect(new ErrorCodeFeatureMatcher(ErrorCode.INVALID_GRANT));
31+
thrown.expect(new ErrorCodeFeatureMatcher(OAuth2Error.INVALID_GRANT));
3232
thrown.expect(new ErrorDescriptionFeatureMatcher(ERROR_DESCRIPTION));
3333

3434
extractor.generateError(ERROR_JSON);
3535
}
3636

37-
private static class ErrorCodeFeatureMatcher extends FeatureMatcher<OAuth2AccessTokenErrorResponse, ErrorCode> {
37+
private static class ErrorCodeFeatureMatcher extends FeatureMatcher<OAuth2AccessTokenErrorResponse, OAuth2Error> {
3838

39-
private ErrorCodeFeatureMatcher(ErrorCode expected) {
39+
private ErrorCodeFeatureMatcher(OAuth2Error expected) {
4040
super(equalTo(expected), "a response with errorCode", "errorCode");
4141
}
4242

4343
@Override
44-
protected ErrorCode featureValueOf(OAuth2AccessTokenErrorResponse actual) {
45-
return actual.getErrorCode();
44+
protected OAuth2Error featureValueOf(OAuth2AccessTokenErrorResponse actual) {
45+
return actual.getError();
4646
}
4747
}
4848

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.github.scribejava.core.model.OAuth2AccessToken;
99
import com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse;
1010
import com.github.scribejava.core.model.Response;
11+
import com.github.scribejava.core.oauth2.OAuth2Error;
1112
import com.github.scribejava.core.utils.Preconditions;
1213

1314
/**
@@ -64,9 +65,9 @@ public void generateError(String response) {
6465
errorUri = null;
6566
}
6667

67-
OAuth2AccessTokenErrorResponse.ErrorCode errorCode;
68+
OAuth2Error errorCode;
6869
try {
69-
errorCode = OAuth2AccessTokenErrorResponse.ErrorCode.parseFrom(errorInString);
70+
errorCode = OAuth2Error.parseFrom(errorInString);
7071
} catch (IllegalArgumentException iaE) {
7172
//non oauth standard error code
7273
errorCode = null;

scribejava-core/src/main/java/com/github/scribejava/core/model/OAuth2AccessTokenErrorResponse.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.scribejava.core.model;
22

33
import com.github.scribejava.core.exceptions.OAuthException;
4+
import com.github.scribejava.core.oauth2.OAuth2Error;
45

56
import java.net.URI;
67

@@ -11,6 +12,10 @@ public class OAuth2AccessTokenErrorResponse extends OAuthException {
1112

1213
private static final long serialVersionUID = 2309424849700276816L;
1314

15+
/**
16+
* @deprecated use {@link com.github.scribejava.core.oauth2.OAuth2Error}
17+
*/
18+
@Deprecated
1419
public enum ErrorCode {
1520
INVALID_REQUEST("invalid_request"),
1621
INVALID_CLIENT("invalid_client"),
@@ -40,19 +45,55 @@ public static ErrorCode parseFrom(String errorCodeString) {
4045
}
4146

4247
private final ErrorCode errorCode;
48+
private final OAuth2Error error;
4349
private final String errorDescription;
4450
private final URI errorUri;
4551
private final String rawResponse;
4652

53+
/**
54+
* @param errorCode errorCode
55+
* @param errorDescription errorDescription
56+
* @param errorUri errorUri
57+
* @param rawResponse rawResponse
58+
* @deprecated use {@link #OAuth2AccessTokenErrorResponse(com.github.scribejava.core.oauth2.OAuth2Error,
59+
* java.lang.String, java.net.URI, java.lang.String)}
60+
*/
61+
@Deprecated
4762
public OAuth2AccessTokenErrorResponse(ErrorCode errorCode, String errorDescription, URI errorUri,
4863
String rawResponse) {
4964
super(rawResponse);
5065
this.errorCode = errorCode;
66+
this.error = OAuth2Error.parseFrom(errorCode.errorCodeString);
5167
this.errorDescription = errorDescription;
5268
this.errorUri = errorUri;
5369
this.rawResponse = rawResponse;
5470
}
5571

72+
public OAuth2AccessTokenErrorResponse(OAuth2Error error, String errorDescription, URI errorUri,
73+
String rawResponse) {
74+
super(rawResponse);
75+
ErrorCode oldErrorCode;
76+
try {
77+
oldErrorCode = ErrorCode.parseFrom(error.getErrorString());
78+
} catch (IllegalArgumentException iaE) {
79+
oldErrorCode = null;
80+
}
81+
this.errorCode = oldErrorCode;
82+
this.error = error;
83+
this.errorDescription = errorDescription;
84+
this.errorUri = errorUri;
85+
this.rawResponse = rawResponse;
86+
}
87+
88+
public OAuth2Error getError() {
89+
return error;
90+
}
91+
92+
/**
93+
* @return error code
94+
* @deprecated use {@link #getError() }
95+
*/
96+
@Deprecated
5697
public ErrorCode getErrorCode() {
5798
return errorCode;
5899
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.github.scribejava.core.oauth2;
2+
3+
public enum OAuth2Error {
4+
/**
5+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-4.1.2.1">RFC 6749, 4.1.2.1 Error Response</a>
6+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-4.2.2.1">RFC 6749, 4.2.2.1 Error Response</a>
7+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-5.2">RFC 6749, 5.2 Error Response</a>
8+
* @see <a href="https://tools.ietf.org/html/rfc6750#section-6.2">RFC 6750, 6.2. OAuth Extensions Error
9+
* Registration</a>
10+
*/
11+
INVALID_REQUEST("invalid_request"),
12+
/**
13+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-4.1.2.1">RFC 6749, 4.1.2.1 Error Response</a>
14+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-4.2.2.1">RFC 6749, 4.2.2.1 Error Response</a>
15+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-5.2">RFC 6749, 5.2 Error Response</a>
16+
*/
17+
UNAUTHORIZED_CLIENT("unauthorized_client"),
18+
/**
19+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-4.1.2.1">RFC 6749, 4.1.2.1 Error Response</a>
20+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-4.2.2.1">RFC 6749, 4.2.2.1 Error Response</a>
21+
*/
22+
ACCESS_DENIED("access_denied"),
23+
/**
24+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-4.1.2.1">RFC 6749, 4.1.2.1 Error Response</a>
25+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-4.2.2.1">RFC 6749, 4.2.2.1 Error Response</a>
26+
*/
27+
UNSUPPORTED_RESPONSE_TYPE("unsupported_response_type"),
28+
/**
29+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-4.1.2.1">RFC 6749, 4.1.2.1 Error Response</a>
30+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-4.2.2.1">RFC 6749, 4.2.2.1 Error Response</a>
31+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-5.2">RFC 6749, 5.2 Error Response</a>
32+
*/
33+
INVALID_SCOPE("invalid_scope"),
34+
/**
35+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-4.1.2.1">RFC 6749, 4.1.2.1 Error Response</a>
36+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-4.2.2.1">RFC 6749, 4.2.2.1 Error Response</a>
37+
*/
38+
SERVER_ERROR("server_error"),
39+
/**
40+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-4.1.2.1">RFC 6749, 4.1.2.1 Error Response</a>
41+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-4.2.2.1">RFC 6749, 4.2.2.1 Error Response</a>
42+
*/
43+
TEMPORARILY_UNAVAILABLE("temporarily_unavailable"),
44+
/**
45+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-5.2">RFC 6749, 5.2 Error Response</a>
46+
*/
47+
INVALID_CLIENT("invalid_client"),
48+
/**
49+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-5.2">RFC 6749, 5.2 Error Response</a>
50+
*/
51+
INVALID_GRANT("invalid_grant"),
52+
/**
53+
* @see <a href="https://tools.ietf.org/html/rfc6749#section-5.2">RFC 6749, 5.2 Error Response</a>
54+
*/
55+
UNSUPPORTED_GRANT_TYPE("unsupported_grant_type"),
56+
/**
57+
* @see <a href="https://tools.ietf.org/html/rfc6750#section-6.2">RFC 6750, 6.2. OAuth Extensions Error
58+
* Registration</a>
59+
*/
60+
INVALID_TOKEN("invalid_token"),
61+
/**
62+
* @see <a href="https://tools.ietf.org/html/rfc6750#section-6.2">RFC 6750, 6.2. OAuth Extensions Error
63+
* Registration</a>
64+
*/
65+
INSUFFICIENT_SCOPE("insufficient_scope"),
66+
/**
67+
* @see <a href="https://tools.ietf.org/html/rfc7009#section-4.1">RFC 7009, 4.1. OAuth Extensions Error
68+
* Registration</a>
69+
*/
70+
UNSUPPORTED_TOKEN_TYPE("unsupported_token_type");
71+
72+
private final String errorString;
73+
74+
OAuth2Error(String errorString) {
75+
this.errorString = errorString;
76+
}
77+
78+
public static OAuth2Error parseFrom(String errorString) {
79+
for (OAuth2Error error : OAuth2Error.values()) {
80+
if (error.errorString.equals(errorString)) {
81+
return error;
82+
}
83+
}
84+
throw new IllegalArgumentException("there is no knowlege about '" + errorString + "' Error");
85+
}
86+
87+
public String getErrorString() {
88+
return errorString;
89+
}
90+
}

scribejava-core/src/test/java/com/github/scribejava/core/extractors/OAuth2AccessTokenJsonExtractorTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.github.scribejava.core.model.OAuth2AccessToken;
44
import com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse;
55
import com.github.scribejava.core.model.Response;
6+
import com.github.scribejava.core.oauth2.OAuth2Error;
67
import org.junit.Test;
78

89
import java.io.IOException;
@@ -66,6 +67,7 @@ public void shouldThrowExceptionIfResponseIsError() throws IOException {
6667
fail();
6768
} catch (OAuth2AccessTokenErrorResponse oaer) {
6869
assertEquals(OAuth2AccessTokenErrorResponse.ErrorCode.INVALID_GRANT, oaer.getErrorCode());
70+
assertEquals(OAuth2Error.INVALID_GRANT, oaer.getError());
6971
assertEquals("unknown, invalid, or expired refresh token", oaer.getErrorDescription());
7072
}
7173
}

0 commit comments

Comments
 (0)