Skip to content
This repository was archived by the owner on Apr 15, 2023. It is now read-only.

Commit 5aed233

Browse files
force logout if refresh token is not valid
1 parent 051747d commit 5aed233

File tree

5 files changed

+30
-33
lines changed

5 files changed

+30
-33
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7-
## Unreleased
8-
7+
## [1.3.1] - (2020-07-26)
8+
### Fixed
9+
- force logout if refresh token is not valid
910

1011
## [1.3.0] - (2020-07-19)
1112
### Added
@@ -54,6 +55,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
5455
### Added
5556
- added methods to manage TagMyCode API
5657

58+
[1.3.1]: https://github.com/massimozappino/tagmycode-java-sdk/compare/v1.3.0...v1.3.1
5759
[1.3.0]: https://github.com/massimozappino/tagmycode-java-sdk/compare/v1.2.1...v1.3.0
5860
[1.2.1]: https://github.com/massimozappino/tagmycode-java-sdk/compare/v1.2.0...v1.2.1
5961
[1.2.0]: https://github.com/massimozappino/tagmycode-java-sdk/compare/v1.1.1...v1.2.0

pom.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.tagmycode</groupId>
55
<artifactId>tagmycode-sdk</artifactId>
66
<packaging>jar</packaging>
7-
<version>1.3.1-SNAPSHOT</version>
7+
<version>1.3.1</version>
88
<name>TagMyCode Java SDK</name>
99
<description>Java SDK for TagMyCode REST API</description>
1010
<url>http://tagmycode.com</url>
@@ -151,6 +151,9 @@
151151
<groupId>org.apache.maven.plugins</groupId>
152152
<artifactId>maven-javadoc-plugin</artifactId>
153153
<version>2.9.1</version>
154+
<configuration>
155+
<source>8</source>
156+
</configuration>
154157
<executions>
155158
<execution>
156159
<id>attach-javadocs</id>

src/main/java/com/tagmycode/sdk/Client.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.scribe.model.Verifier;
1414
import org.slf4j.Logger;
1515
import org.slf4j.LoggerFactory;
16+
1617
import java.util.Map;
1718

1819
public class Client {
@@ -62,8 +63,9 @@ public void refreshOauthToken() throws TagMyCodeException {
6263
try {
6364
fetchAndSetRefreshToken(service.getAccessTokenFromRefreshToken(oauthToken.getRefreshToken()));
6465
} catch (OAuthException e) {
65-
String message = "Error fetching refresh token: " + e.getMessage();
66-
if (e.getMessage().contains("refresh_token")) {
66+
String exceptionMessage = e.getMessage();
67+
String message = "Error fetching refresh token: " + exceptionMessage;
68+
if (responseIsUnauthorized(exceptionMessage)) {
6769
throw new TagMyCodeUnauthorizedException(message);
6870
} else {
6971
throw new TagMyCodeException(message);
@@ -190,6 +192,10 @@ public void revokeAccess() throws TagMyCodeException {
190192
wallet.deleteOauthToken();
191193
}
192194

195+
public boolean responseIsUnauthorized(String responseMessage) {
196+
return responseMessage.contains("refresh_token") || responseMessage.contains("invalid_grant");
197+
}
198+
193199
private void logRequest(OAuthRequest request) {
194200
logger.debug(request.getVerb() + " " + request.getUrl() + "?" + request.getQueryStringParams().asFormUrlEncodedString());
195201
logger.debug("\tHEADERS: " + request.getHeaders().entrySet());

src/main/java/com/tagmycode/sdk/DateParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public class DateParser {
1313

1414
private TimeZone timezone;
15-
private Date date;
15+
private final Date date;
1616

1717
public DateParser(Date date) {
1818
this.date = date;

src/test/java/com/tagmycode/sdk/ClientTest.java

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ public void fetchTokensShouldWorkCorrectly() throws TagMyCodeException {
154154
@Test
155155
public void refreshOauthTokenReceiveNewAccessToken() throws Exception {
156156
stubFor(post(urlMatching("/oauth2/token.*"))
157-
.withRequestBody((matching(".*refresh_token.*")))
158157
.willReturn(aResponse()
159158
.withStatus(200)
160159
.withHeader("Content-Type", "text/plain")
@@ -168,22 +167,24 @@ public void refreshOauthTokenReceiveNewAccessToken() throws Exception {
168167
}
169168

170169
@Test
171-
public void refreshOauthTokenWithInvalidTokenThrowsException() throws Exception {
170+
public void failedRefreshTokenThrownTagMyCodeUnauthorizedException() {
172171
stubFor(post(urlMatching("/oauth2/token.*"))
173-
.withRequestBody((matching(".*refresh_token.*")))
174172
.willReturn(aResponse()
175173
.withStatus(401)
176174
.withHeader("Content-Type", "text/plain")
177175
.withBody("{\"error\":\"invalid_grant\",\"error_description\":\"Invalid refresh token\"}"
178176
)));
179-
assertEquals(new OauthToken("xxx", "yyy"), client.getOauthToken());
177+
Client spyClient = spy(client);
178+
assertEquals(new OauthToken("xxx", "yyy"), spyClient.getOauthToken());
180179

181180
try {
182-
client.refreshOauthToken();
181+
spyClient.refreshOauthToken();
183182
fail("Expected exception");
184-
} catch (TagMyCodeException ignore) {
183+
} catch (TagMyCodeException e) {
184+
assertTrue(e instanceof TagMyCodeUnauthorizedException);
185185
}
186-
assertEquals(new OauthToken("xxx", "yyy"), client.getOauthToken());
186+
assertEquals(new OauthToken("xxx", "yyy"), spyClient.getOauthToken());
187+
verify(spyClient, times(1)).responseIsUnauthorized(anyString());
187188
}
188189

189190
@Test
@@ -196,7 +197,6 @@ public void expiredAccessTokenShouldBeRefreshed() throws Exception {
196197
)));
197198

198199
stubFor(post(urlMatching("/oauth2/token.*"))
199-
.withRequestBody((matching(".*refresh_token.*")))
200200
.willReturn(aResponse()
201201
.withStatus(200)
202202
.withHeader("Content-Type", "text/plain")
@@ -213,26 +213,12 @@ public void expiredAccessTokenShouldBeRefreshed() throws Exception {
213213
}
214214

215215
@Test
216-
public void failedRefreshTokenThrownTagMyCodeUnauthorizedException() throws TagMyCodeException {
217-
stubFor(get(urlMatching("/account.*"))
218-
.willReturn(aResponse()
219-
.withStatus(401)
220-
.withHeader("Content-Type", "text/plain")
221-
.withBody(""
222-
)));
216+
public void testResponseIsUnauthorized() {
217+
assertTrue(client.responseIsUnauthorized("{\"error\":\"invalid_grant\",\"error_description\":\"Invalid refresh token\"}"));
218+
assertTrue(client.responseIsUnauthorized("message contains refresh_token "));
223219

224-
stubFor(post(urlMatching("/oauth2/token.*"))
225-
.withRequestBody((matching(".*refresh_token.*")))
226-
.willReturn(aResponse()
227-
.withStatus(401)
228-
.withHeader("Content-Type", "text/plain")
229-
.withBody("{}"
230-
)));
231-
try {
232-
new TagMyCode(client).fetchAccount();
233-
fail("Expected exception");
234-
} catch (TagMyCodeException ignored) {
235-
}
220+
assertFalse(client.responseIsUnauthorized(""));
221+
assertFalse(client.responseIsUnauthorized("{}"));
236222
}
237223

238224
@Test

0 commit comments

Comments
 (0)