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

First idea of a extendable JWT decoder #434

Closed
wants to merge 1 commit into from

Conversation

anakinj
Copy link
Member

@anakinj anakinj commented Jul 15, 2021

An idea of how custom decoding/encoding of an JWT token could be handled. Related to #428

Was thinking that custom decode modules could be created by including the JWT module into them. This included module could then provide a set of DSL methods where different handlers could be defined. For example:

module MyCustomJWT
  include JWT

  algorithm 'HS256'

  decode_payload { |header, payload, signature| payload.reverse }
  encode_payload { |payload| payload.reverse }
end

payload = MyCustomJWT.encode({'pay' => 'load'}, key: 'secret')

decoded_data = MyCustomJWT.decode(payload, key: 'secret')

This approach would also make defining keyfinders and such much more elegant than passing everything to the ::JWT.decode method

In this PR only a super simple decode_payload logic has been implemented as an illustration how it could work.

@@ -0,0 +1,26 @@
module JWT
module Extension
module ClassMethods

Choose a reason for hiding this comment

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

JWT::Extension::ClassMethods has no descriptive comment

Read more about it here.

true,
create_decode_options(options)).decode_segments
{
header: segments.last,

Choose a reason for hiding this comment

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

JWT::Extension::ClassMethods#decode refers to 'segments' more than self (maybe move it to another class?)

Read more about it here.

end

def decode_and_parse_payload(raw_payload)
raw_payload = @options[:decode_payload_proc].call(header, raw_payload, signature) if @options[:decode_payload_proc]

Choose a reason for hiding this comment

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

JWT::Decode#decode_and_parse_payload calls '@options[:decode_payload_proc]' 2 times

Read more about it here.

@sourcelevel-bot
Copy link

SourceLevel has finished reviewing this Pull Request and has found:

  • 4 possible new issues (including those that may have been commented here).
  • 1 fixed issue! 🎉

See more details about this review.

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.

1 participant