Skip to content

Capturing extra fields as a raw json value #1313

@paulpdaniels

Description

@paulpdaniels

The jwt-scala library has a number of integrations with json libraries to make it easy to decode JWT claims (which are in json). Since jsoniter doesn't have an AST this can't be achieved directly through their JwtJsonCommon class, instead an implementer needs to implement JwtCore[H, C]. One solution is to create a custom header and claim for your specific needs, but thats very cumbersome. The JwtClaim class is defined:

class JwtClaim(
    val content: String,
    val issuer: Option[String],
    val subject: Option[String],
    val audience: Option[Set[String]],
    val expiration: Option[Long],
    val notBefore: Option[Long],
    val issuedAt: Option[Long],
    val jwtId: Option[String]
)

where each field has a jwt associated well-defined field e.g. issuer => iss in json, except content which is simply a string encoded json object containing all the "extra" fields. That is, those fields not explicitly defined in the spec but added for specific usage by the user. What is is needed is a custom codec that will:

  1. populate the standard fields
  2. treat everything else as a "raw" field and add it to the stringified body content field.

As an example:

{
  "iss": "abc123",
  "sub": "efg456",
  "iat": 0,
  "https://example.com/roles": ["admin", "super_admin"],
  "https://sub.example.com/tenant_id": "zzzzzzzz"
}

We would expect the claim:

JwtClaim(
  issuer = Some("abc123"),
  subject = Some("efg456"),
  issuedAt = Some(0L),
  content = """{"https://example.com/roles": ["admin", "super_admin"],"https://sub.example.com/tenant_id": "zzzzzzzz"}"""
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions