-
Notifications
You must be signed in to change notification settings - Fork 80
Fix: SDK not parsing headers with claims other than String #127
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,11 +9,14 @@ | |
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.GsonBuilder; | ||
| import com.google.gson.reflect.TypeToken; | ||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonObject; | ||
|
|
||
| import java.lang.reflect.Type; | ||
| import java.nio.charset.Charset; | ||
| import java.util.Collections; | ||
| import java.util.Date; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
|
|
@@ -27,6 +30,8 @@ public class JWT implements Parcelable { | |
| private final String token; | ||
|
|
||
| private Map<String, String> header; | ||
|
|
||
| private Map<String, Claim> headerTree; | ||
| private JWTPayload payload; | ||
| private String signature; | ||
|
|
||
|
|
@@ -43,14 +48,43 @@ public JWT(@NonNull String token) { | |
|
|
||
| /** | ||
| * Get the Header values from this JWT as a Map of Strings. | ||
| * <p> | ||
| * Structured header parameters (e.g. the "x5c" certificate chain array or a nested | ||
| * "jwk" object) are returned as their JSON text representation. To read header | ||
| * parameters as typed values, prefer {@link #getHeaderClaim(String)}. | ||
| * | ||
| * @return the Header values of the JWT. | ||
| * @deprecated Use {@link #getHeaderClaim(String)} or {@link #getHeaderClaims()} instead, | ||
| * which support non-string header parameters such as "x5c" and "jwk". | ||
| */ | ||
| @Deprecated | ||
| @NonNull | ||
| public Map<String, String> getHeader() { | ||
| return header; | ||
| } | ||
|
|
||
| /** | ||
| * Get a header Claim given its name. If the Claim wasn't specified in the JWT header, a BaseClaim will be returned. | ||
| * | ||
| * @param name the name of the header Claim to retrieve. | ||
| * @return a valid Claim. | ||
| */ | ||
| @NonNull | ||
| public Claim getHeaderClaim(@NonNull String name) { | ||
| final Claim claim = headerTree.get(name); | ||
| return claim != null ? claim : new BaseClaim(); | ||
| } | ||
|
|
||
| /** | ||
| * Get all the header Claims. | ||
| * | ||
| * @return a valid Map of header Claims. | ||
| */ | ||
| @NonNull | ||
| public Map<String, Claim> getHeaderClaims() { | ||
| return headerTree; | ||
| } | ||
|
|
||
| /** | ||
| * Get the Signature from this JWT as a Base64 encoded String. | ||
| * | ||
|
|
@@ -229,13 +263,46 @@ 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); | ||
| parseHeader(base64Decode(parts[0])); | ||
| payload = parseJson(base64Decode(parts[1]), JWTPayload.class); | ||
| signature = parts[2]; | ||
| } | ||
|
|
||
| private void parseHeader(String json) { | ||
| final JsonElement element; | ||
| try { | ||
| element = new Gson().fromJson(json, JsonElement.class); | ||
| } catch (Exception e) { | ||
| throw new DecodeException("The token's header had an invalid JSON format.", e); | ||
| } | ||
| if (element == null || !element.isJsonObject()) { | ||
| throw new DecodeException("The token's header had an invalid JSON format."); | ||
| } | ||
| final JsonObject object = element.getAsJsonObject(); | ||
|
|
||
| Map<String, String> stringHeader = new HashMap<>(); | ||
| Map<String, Claim> tree = new HashMap<>(); | ||
| for (Map.Entry<String, JsonElement> entry : object.entrySet()) { | ||
| JsonElement value = entry.getValue(); | ||
| tree.put(entry.getKey(), new ClaimImpl(value)); | ||
| stringHeader.put(entry.getKey(), stringifyHeaderValue(value)); | ||
| } | ||
| //Kept mutable to preserve the behaviour of the Map that Gson used to return. | ||
| header = stringHeader; | ||
| headerTree = Collections.unmodifiableMap(tree); | ||
| } | ||
|
|
||
| @Nullable | ||
| private String stringifyHeaderValue(JsonElement value) { | ||
| if (value.isJsonNull()) { | ||
| return null; | ||
| } | ||
| if (value.isJsonPrimitive()) { | ||
| return value.getAsString(); | ||
| } | ||
| return value.toString(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A JSON null header value (e.g. {"kid":null}) isn't a primitive, so this returns the literal string "null" instead of null. The old Map<String,String> deserialization stored an actual null. Add an isJsonNull() check
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| } | ||
|
|
||
| private String[] splitToken(String token) { | ||
| String[] parts = token.split("\\."); | ||
| if (parts.length == 2 && token.endsWith(".")) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The catch (DecodeException e) { throw e; } then catch (Exception e) re-wrap pattern works but is a bit convoluted. Simpler: validate isJsonObject() outside the try, since fromJson(..., JsonElement.class) only throws on malformed JSON.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.