Fix bug where JSON-initialized claims don't emit proper B64 when stringified.

This commit is contained in:
Jonathan Bernard 2021-12-25 23:01:51 -06:00
parent 60f52cb22f
commit 20de51281e
3 changed files with 13 additions and 2 deletions

View File

@ -59,7 +59,7 @@ proc initJwtClaims*(n: JsonNode): JwtClaims =
## Create a JwtClaims from a given set of claims as a JsonNOde ## Create a JwtClaims from a given set of claims as a JsonNOde
return JwtClaims( return JwtClaims(
rawB64: $n, rawB64: b64UrlEncode($n),
json: n, json: n,
iss: n.optStrVal("iss"), iss: n.optStrVal("iss"),
sub: n.optStrVal("sub"), sub: n.optStrVal("sub"),

View File

@ -22,6 +22,17 @@ suite "jwt_full/claims":
sampleClaims["name"].isSome sampleClaims["name"].isSome
sampleClaims["name"].get.getStr == "John Doe" sampleClaims["name"].get.getStr == "John Doe"
test "initClaims(json)":
let jsonClaims = initJwtClaims(parseJson(sampleJwtClaimsDecoded))
check:
jsonClaims.sub.isSome
jsonClaims.sub.get == "abc"
jsonClaims["name"].isSome
jsonClaims["name"].get.getStr == "John Doe"
jsonClaims.iss.isNone
$jsonClaims == "eyJzdWIiOiJhYmMiLCJuYW1lIjoiSm9obiBEb2UiLCJpYXQiOjE1MTYyMzkwMjJ9"
test "round trip": test "round trip":
let rfcClaims1 = initJwtClaims(rfc7519_S31_ClaimsB64) let rfcClaims1 = initJwtClaims(rfc7519_S31_ClaimsB64)
let rfcClaims2 = initJwtClaims($rfcClaims1) let rfcClaims2 = initJwtClaims($rfcClaims1)

View File

@ -24,7 +24,7 @@ suite "jwt_full/jws":
check sampleJwt == $sign( check sampleJwt == $sign(
header = initJoseHeader(parseJson(sampleJwtHeaderDecoded)), header = initJoseHeader(parseJson(sampleJwtHeaderDecoded)),
payload = $initJwtClaims(parseJson(sampleJwtClaimsDecoded)), payload = sampleJwtClaimsDecoded,
key = initJwk(parseJson(sampleJwtKey))) key = initJwk(parseJson(sampleJwtKey)))
test "HS256 - verify": test "HS256 - verify":