Repository navigation
fix: read typed JWT claims from the raw claim value - #2029
Conversation
Typed getters parsed getString, which the runtime only fills for string claims, so numeric, boolean, object and array claims were always empty. This made expirationTime, issuedAt and notBefore empty for standard tokens. getString now returns the JSON encoding for non-string claims, as documented. getBoolean also always returned empty.
johanandren
left a comment
There was a problem hiding this comment.
LGTM
with some 🤖 feedback:
Should fix
-
String-encoded objects and lists stop working. In the old code, the only input that worked for
getObjectandget*Listwas a string claim whose content was JSON, such as"roles": "[\"a\",\"b\"]". That was becausegetStringClaimonly returns JSON strings. NowgetObjectfilters onisObject, andlistClaimtries to read the JSON string itself as a list, so both return empty for that input.scalarClaimkeeps string-encoded values on purpose, so the PR is also inconsistent here. Either:- parse the text of a textual node as JSON in
claimNodeandlistClaim, or - drop the support on purpose and mention it in the release notes.
- parse the text of a textual node as JSON in
-
A JSON
nullclaim makes the list getters throw an NPE. For{"x": null},readValue("null", listType)returnsnull, andSome(null).toJavathen throws. The old code had the same bug, butlistClaimis now the single place to fix it:Option(JsonSupport.getObjectMapper.readValue(...)). -
Fractional NumericDates still return empty. RFC 7519 section 2 allows non-integer values, such as
"exp": 1760000000.5. Jackson reads this as aDoubleNodewithasText"1.7600000005E9", soLong.parseLongfails. GivegetNumericDateits own path, for exampleclaimNode(name).filter(_.isNumber).map(n => Instant.ofEpochSecond(n.longValue)). -
Document the behaviour change in the release notes.
getStringon a non-string claim now returns its JSON text where it used to return empty. That includesaudience()whenaudis an array: it returns["a","b"]. It also includesnullclaims, which return"null". Consider a note in theaudience()javadoc as well.
Nits
-
Mark
JwtClaimsImplas@InternalApi private[akka] final class, to match the otherimpl/httpclasses. OnlySdkRunnerandHttpRequestContextImplconstruct it. -
Move the private helpers (
claimNode,scalarClaim,listClaim) to the bottom of the class, instead of betweengetStringandhasClaims. -
Keep Scala
Optionthroughout the internal helpers and call.toJavaonce in each getter. Right nowclaimNodereturnsOption, whilescalarClaimandlistClaimreturnOptional. -
Simplify the scalar getters with the standard library. This removes four
try/catchblocks, andtoBooleanOptionignores case like the oldequalsIgnoreCasecheck:scalarClaim(name).flatMap(_.toIntOption).map(Int.box).toJava -
listClaimcould reuseclaimNodeand callconvertValueon the node instead of parsing the raw text again. This would give one place for the fixes in should-fix items 1 and 2. -
Missing tests:
nbf- a string-encoded
"42"read bygetInteger(the case thescalarClaimcomment exists for) getBooleanreturningfalsegetObjecton a non-object claim- a
nullclaim
The test's
Map.ofcall already has 10 entries, which is the maximum. AddingnbfneedsMap.ofEntries.
…ms as absent BREAKING CHANGE: JwtClaims.getString returns the JSON text of a non-string claim instead of empty. This includes audience() when aud is an array. All getters treat a JSON null claim as absent.
|
Applied should-fix 1, 3 and 4 and all nits in 369056f. The PR title now has Not addressed: should-fix 2. A JSON |
Problem
JwtClaims.expirationTime(),issuedAt()andnotBefore()return empty for standard tokens.Every typed getter in
JwtClaimsImplparsesgetString(name).getStringcalls the runtimegetStringClaim, which returns a value only for JSON strings. Numeric, boolean, object and array claims therefore return empty.Example payload:
{"iss":"my-issuer-123","sub":"user-1","iat":1760000000,"exp":4102444800}asMap()returnsexp: "4102444800".expirationTime()returnsOptional.empty.getBooleanalso always returns empty, because it ignores the result of itsif/else.The
getStringjavadoc says non-string claims return their JSON encoding. The runtime does not do this.Fix
getStringreturns the string value for string claims and the JSON encoding for other claims, as its javadoc says.getRawClaim) instead ofgetString.getBooleanreturns the parsed value.Reproduced on SDK 3.6.6 with runtime 1.6.17.
Behaviour change
getStringon a non-string claim returns its JSON text instead of empty. This includesaudience()whenaudis an array, which returns["a","b"]. A JSONnullclaim is treated as absent by all getters.