-
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
base: master
Are you sure you want to change the base?
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,43 @@ 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 JsonObject object; | ||
| try { | ||
| JsonElement element = getGson().fromJson(json, JsonElement.class); | ||
|
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. getGson() registers the JWTDeserializer for JWTPayload, which is irrelevant for parsing the header into a raw JsonElement. A plain new Gson() (or reused static instance) is clearer and avoids the unnecessary type-adapter setup on every decode. |
||
| if (element == null || !element.isJsonObject()) { | ||
| throw new DecodeException("The token's header had an invalid JSON format."); | ||
| } | ||
| object = element.getAsJsonObject(); | ||
| } catch (DecodeException e) { | ||
| throw e; | ||
| } catch (Exception e) { | ||
| throw new DecodeException("The token's header had an invalid JSON format.", e); | ||
| } | ||
|
|
||
| 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)); | ||
| } | ||
| header = Collections.unmodifiableMap(stringHeader); | ||
|
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. Behavior change: getHeader() previously returned Gson's mutable map; it's now unmodifiable. Any caller that mutates the returned map will now hit UnsupportedOperationException. Since getHeader() is public API, call this out in the changelog/@deprecated note, or keep it mutable to stay source-compatible. |
||
| headerTree = Collections.unmodifiableMap(tree); | ||
| } | ||
|
|
||
| private String stringifyHeaderValue(JsonElement value) { | ||
| 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 |
||
| } | ||
|
|
||
| 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.