Skip to content

Commit

Permalink
Merge pull request #45 from auth0/refactoring
Browse files Browse the repository at this point in the history
Run linting / refactor
  • Loading branch information
lbalmaceda authored Mar 5, 2020
2 parents 683ee1f + f9f70f6 commit 906a1cd
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 133 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.android.tools.build:gradle:3.6.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
}
}
Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ org.gradle.jvmargs=-Xmx1536m
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

android.useAndroidX=true
5 changes: 4 additions & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ dependencies {

testImplementation 'junit:junit:4.13'
testImplementation 'org.hamcrest:java-hamcrest:2.0.0.0'
testImplementation 'org.robolectric:robolectric:4.3.1'
testImplementation('org.robolectric:robolectric:4.3.1') {
// https://github.com/robolectric/robolectric/issues/5245
exclude group: 'com.google.auto.service', module: 'auto-service'
}
testImplementation 'org.mockito:mockito-core:3.2.4'
}

Expand Down
1 change: 0 additions & 1 deletion lib/src/main/java/com/auth0/android/jwt/ClaimImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
/**
* The ClaimImpl class implements the Claim interface.
*/
@SuppressWarnings("WeakerAccess")
class ClaimImpl extends BaseClaim {

private final JsonElement value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.auth0.android.jwt;

@SuppressWarnings("WeakerAccess")
public class DecodeException extends RuntimeException {

DecodeException(String message) {
Expand Down
14 changes: 7 additions & 7 deletions lib/src/main/java/com/auth0/android/jwt/JWT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Base64;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.io.UnsupportedEncodingException;

import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand All @@ -21,7 +24,6 @@
public class JWT implements Parcelable {

private static final String TAG = JWT.class.getSimpleName();
private static final String ENCODING_UTF_8 = "UTF-8";
private final String token;

private Map<String, String> header;
Expand Down Expand Up @@ -161,8 +163,8 @@ public boolean isExpired(long leeway) {
throw new IllegalArgumentException("The leeway must be a positive value.");
}
long todayTime = (long) (Math.floor(new Date().getTime() / 1000) * 1000); //truncate millis
Date futureToday = new Date((todayTime + leeway * 1000));
Date pastToday = new Date((todayTime - leeway * 1000));
Date futureToday = new Date(todayTime + leeway * 1000);
Date pastToday = new Date(todayTime - leeway * 1000);
boolean expValid = payload.exp == null || !pastToday.after(payload.exp);
boolean iatValid = payload.iat == null || !futureToday.before(payload.iat);
return !expValid || !iatValid;
Expand Down Expand Up @@ -230,11 +232,9 @@ private String base64Decode(String string) {
String decoded;
try {
byte[] bytes = Base64.decode(string, Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING);
decoded = new String(bytes, ENCODING_UTF_8);
decoded = new String(bytes, Charset.defaultCharset());
} catch (IllegalArgumentException e) {
throw new DecodeException("Received bytes didn't correspond to a valid Base64 encoded string.", e);
} catch (UnsupportedEncodingException e) {
throw new DecodeException("Device doesn't support UTF-8 charset encoding.", e);
}
return decoded;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public JWTPayload deserialize(JsonElement json, Type typeOfT, JsonDeserializatio
return new JWTPayload(iss, sub, exp, nbf, iat, jti, aud, extra);
}

@SuppressWarnings("SameParameterValue")
private List<String> getStringOrArray(JsonObject obj, String claimName) {
List<String> list = Collections.emptyList();
if (obj.has(claimName)) {
Expand Down
22 changes: 11 additions & 11 deletions lib/src/test/java/com/auth0/android/jwt/BaseClaimTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,64 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsArrayWithSize.emptyArray;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertThat;

public class BaseClaimTest {

private BaseClaim claim;

@Before
public void setUp() throws Exception {
public void setUp() {
claim = new BaseClaim();
}

@Test
public void shouldGetAsBoolean() throws Exception {
public void shouldGetAsBoolean() {
assertThat(claim.asBoolean(), is(nullValue()));
}

@Test
public void shouldGetAsInt() throws Exception {
public void shouldGetAsInt() {
assertThat(claim.asInt(), is(nullValue()));
}

@Test
public void shouldGetAsLong() throws Exception {
public void shouldGetAsLong() {
assertThat(claim.asLong(), is(nullValue()));
}

@Test
public void shouldGetAsDouble() throws Exception {
public void shouldGetAsDouble() {
assertThat(claim.asDouble(), is(nullValue()));
}

@Test
public void shouldGetAsString() throws Exception {
public void shouldGetAsString() {
assertThat(claim.asString(), is(nullValue()));
}

@Test
public void shouldGetAsDate() throws Exception {
public void shouldGetAsDate() {
assertThat(claim.asDate(), is(nullValue()));
}

@Test
public void shouldGetAsArray() throws Exception {
public void shouldGetAsArray() {
assertThat(claim.asArray(Object.class), is(notNullValue()));
assertThat(claim.asArray(Object.class), is(emptyArray()));
}

@Test
public void shouldGetAsList() throws Exception {
public void shouldGetAsList() {
assertThat(claim.asList(Object.class), is(notNullValue()));
assertThat(claim.asList(Object.class), is(empty()));
}

@Test
public void shouldGetAsObject() throws Exception {
public void shouldGetAsObject() {
assertThat(claim.asObject(Object.class), is(nullValue()));
}
}
Loading

0 comments on commit 906a1cd

Please sign in to comment.