diff --git a/CHANGELOG.md b/CHANGELOG.md index 9800526..9769c7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +v0.5.1 - 6/7/2016 +------------ +### Fixed + - [Issue #14](https://github.com/uber/rides-java-sdk/issues/14) Adjust RefreshAuthenticator to ignore Invalid scope + + v0.5.0 - 6/2/2016 ------------------ This release sets up the Java SDK for the Uber Android SDK to utilize it as a third party dependency by adding common interfaces and removing heavier weight components. @@ -26,7 +32,7 @@ Replaces `UberRidesSyncService` and `UberRidesAsyncService` with a Retrofit 2 ba - Updated from Retrofit 1 to Retrofit 2 - Updated from OkHttp2 to OkHttp3 - Removed Gauva dependency - + ### Breaking - Removed `UberServices` in favor of `UberRidesApi` - Removed `UberRidesSyncService` and `UberRidesAsyncService` in favor of `RidesService` @@ -45,4 +51,3 @@ v0.2.0 - 2/25/2016 v0.1.0 - 9/23/2015 ------------------ - Initial version. - diff --git a/README.md b/README.md index 05ed3bb..bdb7d98 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Note: Using Android? Be sure to checkout the [Uber Android SDK](github.com/uber/ If using Gradle, add this to your project’s `build.gradle` file: ```gradle dependencies { - compile 'com.uber.sdk:rides:0.5.0' + compile 'com.uber.sdk:rides:0.5.1' } ``` @@ -24,7 +24,7 @@ If using Maven, add this to your project's `pom.xml` file: com.uber.sdk rides - 0.5.0 + 0.5.1 ``` diff --git a/gradle.properties b/gradle.properties index bc681e5..d93eb51 100644 --- a/gradle.properties +++ b/gradle.properties @@ -24,4 +24,4 @@ group=com.uber.sdk groupId=com.uber.sdk artifactId=rides githubDownloadPrefix=https://github.com/uber/rides-java-sdk/releases/download/ -version=0.5.1-SNAPSHOT +version=0.5.1 diff --git a/sdk/src/main/java/com/uber/sdk/rides/client/internal/ApiInterceptor.java b/sdk/src/main/java/com/uber/sdk/rides/client/internal/ApiInterceptor.java index 787d21c..214e59e 100644 --- a/sdk/src/main/java/com/uber/sdk/rides/client/internal/ApiInterceptor.java +++ b/sdk/src/main/java/com/uber/sdk/rides/client/internal/ApiInterceptor.java @@ -34,7 +34,7 @@ public class ApiInterceptor implements Interceptor { static final String HEADER_ACCESS_TOKEN = "Authorization"; - static final String LIB_VERSION = "0.5.0"; + static final String LIB_VERSION = "0.5.1"; static final String HEADER_ACCEPT_LANGUAGE = "Accept-Language"; static final String HEADER_USER_AGENT = "X-Uber-User-Agent"; diff --git a/sdk/src/main/java/com/uber/sdk/rides/client/internal/RefreshAuthenticator.java b/sdk/src/main/java/com/uber/sdk/rides/client/internal/RefreshAuthenticator.java index d44bc51..f933a86 100644 --- a/sdk/src/main/java/com/uber/sdk/rides/client/internal/RefreshAuthenticator.java +++ b/sdk/src/main/java/com/uber/sdk/rides/client/internal/RefreshAuthenticator.java @@ -32,6 +32,7 @@ public class RefreshAuthenticator implements okhttp3.Authenticator { + static final String HEADER_INVALID_SCOPES = "X-Uber-Missing-Scopes"; static final int MAX_RETRIES = 3; public final Authenticator authenticator; @@ -41,13 +42,24 @@ public RefreshAuthenticator(Authenticator authenticator) { @Override public Request authenticate(Route route, Response response) throws IOException { - if (authenticator.isRefreshable() && canRetry(response)) { + if (authenticator.isRefreshable() && canRefresh(response) && canRetry(response)) { return authenticator.refresh(response); } return null; } + /** + * The Uber API returns invalid scopes as 401's and will migrate to 403's in the future. + * This is a temporary measure and will be updated in the future. + * + * @param response to check for {@link RefreshAuthenticator#HEADER_INVALID_SCOPES} header. + * @return true if a true 401 and can refresh, otherwise false + */ + boolean canRefresh(Response response) { + return response.header(HEADER_INVALID_SCOPES) == null; + } + boolean canRetry(Response response) { int responseCount = 1; diff --git a/sdk/src/test/java/com/uber/sdk/rides/client/internal/RefreshAuthenticatorTest.java b/sdk/src/test/java/com/uber/sdk/rides/client/internal/RefreshAuthenticatorTest.java index b081536..c754eeb 100644 --- a/sdk/src/test/java/com/uber/sdk/rides/client/internal/RefreshAuthenticatorTest.java +++ b/sdk/src/test/java/com/uber/sdk/rides/client/internal/RefreshAuthenticatorTest.java @@ -51,29 +51,54 @@ public void testAuthenticate_canReAuthAndRetry_callsRefresh() throws Exception { doReturn(new Request.Builder().url("http://test").build()).when(authenticator).refresh(eq(response)); doReturn(true).when(authenticator).isRefreshable(); doReturn(true).when(refreshAuthenticator).canRetry(eq(response)); + doReturn(true).when(refreshAuthenticator).canRefresh(eq(response)); assertNotNull(refreshAuthenticator.authenticate(null, response)); verify(authenticator).refresh(eq(response)); } @Test - public void testAuthenticate_canReAuthButCannotRetry_callsRefresh() throws Exception { + public void testAuthenticate_canReAuthButCannotRetry_returnsNull() throws Exception { doReturn(true).when(authenticator).isRefreshable(); doReturn(false).when(refreshAuthenticator).canRetry(eq(response)); + doReturn(true).when(refreshAuthenticator).canRefresh(eq(response)); assertNull(refreshAuthenticator.authenticate(null, response)); verify(authenticator, never()).refresh(eq(response)); } @Test - public void testAuthenticate_cannotReAuthButCanRetry_callsRefresh() throws Exception { + public void testAuthenticate_cannotReAuthButCanRetry_returnsNull() throws Exception { doReturn(false).when(authenticator).isRefreshable(); doReturn(true).when(refreshAuthenticator).canRetry(eq(response)); + doReturn(true).when(refreshAuthenticator).canRefresh(eq(response)); assertNull(refreshAuthenticator.authenticate(null, response)); verify(authenticator, never()).refresh(eq(response)); } + @Test + public void testAuthenticate_canReAuthRetryButCannotRefresh_returnsNull() throws Exception { + doReturn(true).when(authenticator).isRefreshable(); + doReturn(true).when(refreshAuthenticator).canRetry(eq(response)); + doReturn(false).when(refreshAuthenticator).canRefresh(eq(response)); + + assertNull(refreshAuthenticator.authenticate(null, response)); + verify(authenticator, never()).refresh(eq(response)); + } + + @Test + public void testCanRefresh_whenContainsHeader_returnsFalse() { + Response cannotRefresh = response.newBuilder().header(RefreshAuthenticator.HEADER_INVALID_SCOPES, "true").build(); + assertFalse(refreshAuthenticator.canRefresh(cannotRefresh)); + } + + @Test + public void testCanRefresh_whenMissingHeader_returnsTrue() { + Response canRefresh = response.newBuilder().build(); + assertTrue(refreshAuthenticator.canRefresh(canRefresh)); + } + @Test public void testCanRetry_whenUnderMax_returnsTrue() throws Exception { Response underMax = response.newBuilder().priorResponse(response).build();