Skip to content
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

Add support for RFC7797 unencoded payloads #561

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

WriterZephos
Copy link

This adds support for RFC7797 and addresses #309

Here is the specification: https://www.rfc-editor.org/rfc/rfc7797

Happy to receive feedback and would love to get this released soon as I need it for my current project.

Bryant Morrill added 3 commits May 4, 2023 18:03
adds `encode_detached` to JWT, which produces a jwt with an empty payload segment
adds support for the `b64` header, which when set to false will prevent the payload from being base64 encoded
adds support for decoding JWTs with the `b64` header set to false
adds support for decoding and verifying JWTs with detached payloads
Copy link

@abiagini abiagini left a comment

Choose a reason for hiding this comment

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

In general LGTM, I made some minor comments.

@@ -22,7 +22,16 @@ def encode(payload, key, algorithm = 'HS256', header_fields = {})
Encode.new(payload: payload,
key: key,
algorithm: algorithm,
headers: header_fields).segments
headers: header_fields,
detached: false).segments

Choose a reason for hiding this comment

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

Maybe we can set detached as default false

@@ -15,11 +15,24 @@ def initialize(options)
@algorithm = resolve_algorithm(options[:algorithm])
@headers = options[:headers].transform_keys(&:to_s)
@headers[ALG_KEY] = @algorithm.alg
@detached = options[:detached]

Choose a reason for hiding this comment

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

Here we can set as default false

@detached = options[:detached] || false

def parse_and_decode(segment, decode: true)
if decode
JWT::JSON.parse(::JWT::Base64.url_decode(segment))
else

Choose a reason for hiding this comment

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

We can avoid else statement with a guard clause.

@abiagini
Copy link

Hi guys, this feature is quite common, will be good to have this supported.

@WriterZephos
Copy link
Author

Hi guys, this feature is quite common, will be good to have this supported.

Thanks for you suggestions. I will try to find time to push up some updates soon.

Copy link
Member

@anakinj anakinj left a comment

Choose a reason for hiding this comment

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

Finally got time to dig a bit into this. I think the direction is nice. A few comments related to maybe being more explicit on how to behave when encoding and decoding.

end

def decode_payload?
header['b64'].nil? || !!header['b64']
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if this is a bit problematic to change behavior because of user-input. Would it be too much of an inconvenience to have the caller decide to decode or not?

end

def parse_and_decode(segment)
JWT::JSON.parse(::JWT::Base64.url_decode(segment))
def parse_and_decode(segment, decode: true)
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to avoid the parameter if the caller would just call parse and decode in separate?

end

def encoded_payload
payload = encoded_detached_payload if !@detached_payload.nil? && @segments[1].empty?
Copy link
Member

Choose a reason for hiding this comment

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

I think it would make sense to raise an error if a payload is given and the token also contains a payload, instead of just ignoring the given payload.

@@ -51,7 +64,21 @@ def encode_header
end

def encode_payload
encode_data(@payload)
# if b64 header is present and false, do not encode payload as per RFC7797 proposed standard
Copy link
Member

@anakinj anakinj Jun 16, 2024

Choose a reason for hiding this comment

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

Would it make sense to not rely on header values but instead have an explicit parameter that controls the behavior of both header values and payload encoding?

Unencoded and detached payloads are often used hand in hand, such as in proof signatures of Verifiable Credentials. You may use the `b64` header in combination with `encode_detached` to combine both features.

```ruby
private_key = RbNaCl::Signatures::Ed25519::SigningKey.new('abcdefghijklmnopqrstuvwxyzABCDEF')
Copy link
Member

Choose a reason for hiding this comment

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

The example could be a more simple one than one that required the rbnacl dependency.

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.

3 participants