Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace gson with Android's org.json.* #77

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ android {

dependencies {
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'com.google.code.gson:gson:2.8.9'

testImplementation 'junit:junit:4.13'
testImplementation 'org.hamcrest:java-hamcrest:2.0.0.0'
Expand All @@ -44,6 +43,7 @@ dependencies {
exclude group: 'com.google.auto.service', module: 'auto-service'
}
testImplementation 'org.mockito:mockito-core:3.2.4'
testImplementation project(':jwtdecode')
}

tasks.withType(Test) {
Expand Down
18 changes: 7 additions & 11 deletions lib/src/main/java/com/auth0/android/jwt/BaseClaim.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.auth0.android.jwt;

import androidx.annotation.Nullable;
import java.lang.reflect.Array;
import java.util.Collections;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Date;
import java.util.List;

/**
* The BaseClaim class is a Claim implementation that returns null when any of it's methods it's called.
Expand Down Expand Up @@ -47,18 +48,13 @@ public Date asDate() {

@SuppressWarnings("unchecked")
@Override
public <T> T[] asArray(Class<T> tClazz) throws DecodeException {
return (T[]) Array.newInstance(tClazz, 0);
}

@Override
public <T> List<T> asList(Class<T> tClazz) throws DecodeException {
return Collections.emptyList();
public JSONArray asArray() {
return null;
}

@Nullable
@Override
public <T> T asObject(Class<T> tClazz) throws DecodeException {
public JSONObject asObject() {
return null;
}
}
22 changes: 8 additions & 14 deletions lib/src/main/java/com/auth0/android/jwt/Claim.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.auth0.android.jwt;

import androidx.annotation.Nullable;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Date;
import java.util.List;

Expand Down Expand Up @@ -69,26 +73,16 @@ public interface Claim {
* If the value isn't an Array, an empty Array will be returned.
*
* @return the value as an Array or an empty Array.
* @throws DecodeException if the values inside the Array can't be converted to a class T.
*/
<T> T[] asArray(Class<T> tClazz) throws DecodeException;

/**
* Get this Claim as a List of type T.
* If the value isn't an Array, an empty List will be returned.
*
* @return the value as a List or an empty List.
* @throws DecodeException if the values inside the List can't be converted to a class T.
*/
<T> List<T> asList(Class<T> tClazz) throws DecodeException;
@Nullable
JSONArray asArray();

/**
* Get this Claim as a Object of type T.
* If the value isn't of type Object, null will be returned.
*
* @return the value as a Object of type T or null.
* @throws DecodeException if the value can't be converted to a class T.
* @return the value as a Object of type T or null..
*/
@Nullable
<T> T asObject(Class<T> tClazz) throws DecodeException;
JSONObject asObject();
}
128 changes: 0 additions & 128 deletions lib/src/main/java/com/auth0/android/jwt/ClaimImpl.java

This file was deleted.

99 changes: 99 additions & 0 deletions lib/src/main/java/com/auth0/android/jwt/JSONObjectClaim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.auth0.android.jwt;

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

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

final class JSONObjectClaim implements Claim {
@NonNull
private final String name;
@NonNull
private final JSONObject object;

JSONObjectClaim(@NonNull JSONObject object, @NonNull String name) {
this.object = object;
this.name = name;
}

@Nullable
@Override
public Boolean asBoolean() {
try {
return object.getBoolean(name);
} catch (JSONException exception) {
return null;
}
}

@Nullable
@Override
public Integer asInt() {
try {
return object.getInt(name);
} catch (JSONException exception) {
return null;
}
}

@Nullable
@Override
public Long asLong() {
try {
return object.getLong(name);
} catch (JSONException exception) {
return null;
}
}

@Nullable
@Override
public Double asDouble() {
try {
return object.getDouble(name);
} catch (JSONException exception) {
return null;
}
}

@Nullable
@Override
public String asString() {
if (object.isNull(name) || object.optJSONArray(name) != null || object.optJSONObject(name) != null) {
return null;
}

try {
return object.getString(name);
} catch (JSONException exception) {
return null;
}
}

@Nullable
@Override
public Date asDate() {
final Long value = asLong();
if (value != null) {
return new Date(value * 1000);
}

return null;
}

@Nullable
@Override
public JSONArray asArray() {
return object.optJSONArray(name);
}

@Nullable
@Override
public JSONObject asObject() {
return object.optJSONObject(name);
}
}
27 changes: 3 additions & 24 deletions lib/src/main/java/com/auth0/android/jwt/JWT.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
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.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -208,10 +203,8 @@ public JWT[] newArray(int size) {

private void decode(String token) {
final String[] parts = splitToken(token);
Type mapType = new TypeToken<Map<String, String>>() {
}.getType();
header = parseJson(base64Decode(parts[0]), mapType);
payload = parseJson(base64Decode(parts[1]), JWTPayload.class);
header = decoder.decodeHeader(base64Decode(parts[0]));
payload = decoder.decodePayload(base64Decode(parts[1]));
signature = parts[2];
}

Expand Down Expand Up @@ -239,19 +232,5 @@ private String base64Decode(String string) {
return decoded;
}

private <T> T parseJson(String json, Type typeOfT) {
T payload;
try {
payload = getGson().fromJson(json, typeOfT);
} catch (Exception e) {
throw new DecodeException("The token's payload had an invalid JSON format.", e);
}
return payload;
}

static Gson getGson() {
return new GsonBuilder()
.registerTypeAdapter(JWTPayload.class, new JWTDeserializer())
.create();
}
private static JWTDecoder decoder = new JWTDecoder();
}
Loading
Loading