Skip to content

Commit aa4e618

Browse files
committed
some fixes
1 parent e01064d commit aa4e618

File tree

7 files changed

+30
-44
lines changed

7 files changed

+30
-44
lines changed

changelog

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[SNAPSHOT]
2-
* Add Naver API
2+
* Add Naver API (thanks to chooco)
3+
* handle OAuth2 error response for Issuing an Access Token (thanks to juherr)
34

45
[3.1.0]
56
* fix OdnoklassnikiServiceImpl signature, params for hash must be sorted in lexicographic order, see http://new.apiok.ru/dev/methods/

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ public static NaverApi instance() {
1717
return NaverApi.InstanceHolder.INSTANCE;
1818
}
1919

20+
@Override
2021
public String getAccessTokenEndpoint() {
2122
return "https://nid.naver.com/oauth2.0/token?grant_type=authorization_code";
2223
}
2324

25+
@Override
2426
protected String getAuthorizationBaseUrl() {
2527
return "https://nid.naver.com/oauth2.0/authorize";
2628
}

scribejava-core/src/main/java/com/github/scribejava/core/exceptions/OAuthRequestException.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.regex.Pattern;
77
import com.github.scribejava.core.exceptions.OAuthException;
88
import com.github.scribejava.core.model.OAuth2AccessToken;
9-
import com.github.scribejava.core.model.OAuth2ErrorResponse;
9+
import com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse;
1010
import com.github.scribejava.core.model.Response;
1111
import com.github.scribejava.core.utils.Preconditions;
1212

@@ -48,7 +48,9 @@ public OAuth2AccessToken extract(Response response) throws IOException {
4848
return createToken(body);
4949
}
5050

51-
// Related documentation: https://tools.ietf.org/html/rfc6749#section-5.2
51+
/**
52+
* Related documentation: https://tools.ietf.org/html/rfc6749#section-5.2
53+
*/
5254
private static void generateError(String response) {
5355
final String errorInString = extractParameter(response, ERROR_REGEX, true);
5456
final String errorDescription = extractParameter(response, ERROR_DESCRIPTION_REGEX, false);
@@ -60,8 +62,8 @@ private static void generateError(String response) {
6062
errorUri = null;
6163
}
6264

63-
throw new OAuth2ErrorResponse(OAuth2ErrorResponse.OAuthError.valueOf(errorInString), errorDescription,
64-
errorUri, response);
65+
throw new OAuth2AccessTokenErrorResponse(OAuth2AccessTokenErrorResponse.ErrorCode.valueOf(errorInString),
66+
errorDescription, errorUri, response);
6567
}
6668

6769
private OAuth2AccessToken createToken(String response) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ public interface TokenExtractor<T extends Token> {
1515
/**
1616
* Extracts the concrete type of token from the contents of an Http Response
1717
*
18-
* @param response the contents of the response
18+
* @param response the whole response
1919
* @return OAuth access token
20+
* @throws java.io.IOException in case of troubles while getting body from the response
2021
*/
2122
T extract(Response response) throws IOException, OAuthException;
2223
}

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,33 @@
77
/**
88
* Representing <a href="https://tools.ietf.org/html/rfc6749#section-5.2">"5.2. Error Response"</a>
99
*/
10-
public class OAuth2ErrorResponse extends OAuthException {
10+
public class OAuth2AccessTokenErrorResponse extends OAuthException {
1111

12-
public enum OAuthError {
12+
private static final long serialVersionUID = 2309424849700276816L;
13+
14+
public enum ErrorCode {
1315
invalid_request, invalid_client, invalid_grant, unauthorized_client, unsupported_grant_type, invalid_scope
1416
}
1517

16-
private final OAuthError error;
18+
private final ErrorCode errorCode;
1719
private final String errorDescription;
1820
private final URI errorUri;
1921
private final String rawResponse;
2022

21-
public OAuth2ErrorResponse(OAuthError error, String errorDescription, URI errorUri, String rawResponse) {
22-
super(generateMessage(error, errorDescription, errorUri, rawResponse));
23-
if (error == null) {
24-
throw new IllegalArgumentException("error must not be null");
23+
public OAuth2AccessTokenErrorResponse(ErrorCode errorCode, String errorDescription, URI errorUri,
24+
String rawResponse) {
25+
super(rawResponse);
26+
if (errorCode == null) {
27+
throw new IllegalArgumentException("errorCode must not be null");
2528
}
26-
this.error = error;
29+
this.errorCode = errorCode;
2730
this.errorDescription = errorDescription;
2831
this.errorUri = errorUri;
2932
this.rawResponse = rawResponse;
3033
}
3134

32-
private static String generateMessage(OAuthError error, String errorDescription, URI errorUri, String rawResponse) {
33-
return rawResponse;
34-
}
35-
36-
public OAuthError getError() {
37-
return error;
35+
public ErrorCode getErrorCode() {
36+
return errorCode;
3837
}
3938

4039
public String getErrorDescription() {

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.github.scribejava.core.extractors;
22

33
import com.github.scribejava.core.model.OAuth2AccessToken;
4-
import com.github.scribejava.core.model.OAuth2ErrorResponse;
4+
import com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse;
55
import com.github.scribejava.core.model.Response;
66
import org.junit.Test;
77

88
import java.io.IOException;
99
import java.util.Collections;
1010

1111
import static org.junit.Assert.assertEquals;
12-
import static org.junit.Assert.assertTrue;
12+
import static org.junit.Assert.fail;
1313

1414
public class OAuth2AccessTokenJsonExtractorTest {
1515

@@ -38,15 +38,13 @@ public void shouldThrowExceptionIfResponseIsError() throws IOException {
3838
"\"error_description\":\"unknown, invalid, or expired refresh token\"," +
3939
"\"error\":\"invalid_grant\"" +
4040
"}";
41-
boolean hadException = false;
4241
try {
4342
extractor.extract(error(body));
44-
} catch (OAuth2ErrorResponse oaer) {
45-
hadException = true;
46-
assertEquals(OAuth2ErrorResponse.OAuthError.invalid_grant, oaer.getError());
43+
fail();
44+
} catch (OAuth2AccessTokenErrorResponse oaer) {
45+
assertEquals(OAuth2AccessTokenErrorResponse.ErrorCode.invalid_grant, oaer.getErrorCode());
4746
assertEquals("unknown, invalid, or expired refresh token", oaer.getErrorDescription());
4847
}
49-
assertTrue(hadException);
5048
}
5149

5250
private static Response ok(String body) {

0 commit comments

Comments
 (0)