Skip to content

Implements Nested JWT functionality as defined in RFC 7519 Section 5.2, 7.1, 7.2, and Appendix A.2.#712

Open
ydah wants to merge 1 commit intojwt:mainfrom
ydah:nested-jwt
Open

Implements Nested JWT functionality as defined in RFC 7519 Section 5.2, 7.1, 7.2, and Appendix A.2.#712
ydah wants to merge 1 commit intojwt:mainfrom
ydah:nested-jwt

Conversation

@ydah
Copy link
Contributor

@ydah ydah commented Jan 29, 2026

Description

Implements Nested JWT functionality as defined in RFC 7519 Section 5.2, 7.1, 7.2, and Appendix A.2.

A Nested JWT is a JWT used as the payload of another JWT, allowing multiple layers of signing with different keys/algorithms.

see: https://datatracker.ietf.org/doc/html/rfc7519#section-5.2
see: https://datatracker.ietf.org/doc/html/rfc7519#section-7.1
see: https://datatracker.ietf.org/doc/html/rfc7519#section-7.2

Checklist

Before the PR can be merged be sure the following are checked:

  • There are tests for the fix or feature added/changed
  • A description of the changes and a reference to the PR has been added to CHANGELOG.md. More details in the CONTRIBUTING.md

@ydah ydah force-pushed the nested-jwt branch 4 times, most recently from b43c683 to 62bbb58 Compare January 29, 2026 12:59
…2, 7.1, 7.2, and Appendix A.2.

Implements Nested JWT functionality as defined in RFC 7519 Section 5.2, 7.1, 7.2, and Appendix A.2.

A Nested JWT is a JWT used as the payload of another JWT, allowing multiple layers of signing with different keys/algorithms.
@ydah ydah changed the title Add support for Nested JWTs per RFC 7519 Implements Nested JWT functionality as defined in RFC 7519 Section 5.2, 7.1, 7.2, and Appendix A.2. Jan 29, 2026
@anakinj
Copy link
Member

anakinj commented Feb 3, 2026

Thanks for the addition @ydah, looks interesting. I need to take a look at this with a fresh mind. But its on my todo list.

# encoded_token.verify_signature!(algorithm: 'HS256', key: 'secret')
# encoded_token.payload # => {'pay' => 'load'}
class EncodedToken
class EncodedToken # rubocop:disable Metrics/ClassLength
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do the refactoring sketched out in #713 in a separate PR to deal with the class length?

tokens = [self]
current = self

while current.nested?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we limit the depth of the nesting to for example 10? There could be some security issue with having no limit.

# @return [String] the JWT token as a string.
alias to_s jwt

class << self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be easier to follow if there would be only one way to create these for now. Fancying smaller iterations over different variations how to do things.

# The content type header value for nested JWTs as per RFC 7519
CTY_JWT = 'JWT'

class << self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we represent such a NestedToken as an instance instead of having static methods doing everything (signing, claim/signature, verification)?

Now for example there is no way to control what claims are being verified.

# )
def sign(inner_jwt, algorithm:, key:, header: {})
outer_header = header.merge('cty' => CTY_JWT)
token = Token.new(payload: inner_jwt, header: outer_header)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we will be creating invalid tokens this way, as Token is always doing the JSON encoding on the payload. To my understanding the nested tokens should only be base64 encoded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants