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

improving code quality of jwt module #266

Merged
merged 2 commits into from
May 6, 2018
Merged

improving code quality of jwt module #266

merged 2 commits into from
May 6, 2018

Conversation

ab320012
Copy link
Contributor

@ab320012 ab320012 commented May 4, 2018

I started looking into the #errors and #value object return for decode and wanted to clean up the logic before implementing

lib/jwt.rb Outdated
def verify_claims
Verify.verify_claims(@payload, @options)
end
def options_includes_algo_in_header?

Choose a reason for hiding this comment

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

Use empty lines between method definitions.

lib/jwt.rb Outdated
def verify?
@verify
end
def verify_claims

Choose a reason for hiding this comment

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

Use empty lines between method definitions.

lib/jwt.rb Outdated
def allowed_algorithms(options)
if options.key?(:algorithm)
[options[:algorithm]]
def allowed_algorithms

Choose a reason for hiding this comment

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

Use empty lines between method definitions.

@@ -22,42 +22,57 @@ def encode(payload, key, algorithm = 'HS256', header_fields = {})
encoder.segments
end

def decode(jwt, key = nil, verify = true, custom_options = {}, &keyfinder)
def decode(jwt, key = nil, verify = true, options = {}, &keyfinder)

Choose a reason for hiding this comment

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

JWT#decode has boolean parameter 'verify'

Read more about it here.

@ab320012 ab320012 force-pushed the master branch 2 times, most recently from db0be71 to 847c8a3 Compare May 4, 2018 16:48
@segments.first(2).join('.')
end

def parse_and_decode segment

Choose a reason for hiding this comment

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

Use def with parentheses when there are parameters.

def validate_segment_count
raise(JWT::DecodeError, 'Not enough or too many segments') unless
(@verify and @segments.length != 3) ||
(@segments.length != 3 or @segments.length != 2)

Choose a reason for hiding this comment

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

Use || instead of or.

private
def validate_segment_count
raise(JWT::DecodeError, 'Not enough or too many segments') unless
(@verify and @segments.length != 3) ||

Choose a reason for hiding this comment

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

Use && instead of and.

required_num_segments = @verify ? [3] : [2, 3]
raise(JWT::DecodeError, 'Not enough or too many segments') unless required_num_segments.include? segments.length
segments
private

Choose a reason for hiding this comment

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

Useless private access modifier.

def validate_segment_count
raise(JWT::DecodeError, 'Not enough or too many segments') unless
(@verify and @segments.length != 3) ||
(@segments.length != 3 or @segments.length != 2)

Choose a reason for hiding this comment

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

Use 4 (not 0) spaces for indenting a condition in an unless statement spanning multiple lines.

rescue JSON::ParserError
raise JWT::DecodeError, 'Invalid segment encoding'
end

Choose a reason for hiding this comment

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

Extra empty line detected at class body end.

required_num_segments = @verify ? [3] : [2, 3]
raise(JWT::DecodeError, 'Not enough or too many segments') unless required_num_segments.include? segments.length
segments
private

Choose a reason for hiding this comment

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

Keep a blank line before and after private.

private
def validate_segment_count
raise(JWT::DecodeError, 'Not enough or too many segments') unless
(@verify and @segments.length != 3) ||

Choose a reason for hiding this comment

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

JWT::Decode#validate_segment_count calls '@segments.length' 3 times

Read more about it here.

(@verify && segment_length != 3) ||
(segment_length != 3 || segment_length != 2)
end
def segment_length

Choose a reason for hiding this comment

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

Use empty lines between method definitions.

@sourcelevel-bot
Copy link

Ebert has finished reviewing this Pull Request and has found:

  • 1 fixed issue! 🎉

You can see more details about this review at https://ebertapp.io/github/jwt/ruby-jwt/pulls/266.

@ab320012
Copy link
Contributor Author

ab320012 commented May 4, 2018

@excpt I am thinking about implementing DecodedToken class that will return from JWT#decode. It will act as array with two elements for backward compatability. It should also probably implement payload and header attributes to access the data directly, as well as an errors method. My issue is not sure how to implement the backwards compatibility for errors. if we return an array from #errors method on DecodedToken instead of throwing errors it will break all existing implementations, let me know. #124

@excpt excpt self-requested a review May 6, 2018 19:06
Copy link
Member

@excpt excpt left a comment

Choose a reason for hiding this comment

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

Great work! 🥇

@excpt
Copy link
Member

excpt commented May 6, 2018

@ab320012 The introduction of Encode/Decode value objects should happen in a different PR.

Thanks for all your effort and time you invest!

@excpt excpt merged commit eef38b2 into jwt:master May 6, 2018
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