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

Move signature logic to its own module #195

Merged
merged 4 commits into from
Feb 19, 2017

Conversation

EmilioCristalli
Copy link
Contributor

Not sure if this change was planned, but seemed aligned with the rest of modules the gem has.
Also I think this will help to start working on redefining the public API (I guess that is planned for version 2).

Let me know what you think, if you want me to make changes, or if it's just not the direction you guys wanted to go.

Thanks!

Removed secure_compare specs as it is not part of the public API anymore.
HMAC specs should already cover the functionality of secure_compare.
@excpt
Copy link
Member

excpt commented Feb 12, 2017

Thank you very much!

This is exactly the way to go. 👍


module JWT
module Signature
extend self
Copy link
Member

Choose a reason for hiding this comment

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

Please use module_function instead of extend self

Copy link
Contributor Author

Choose a reason for hiding this comment

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

what about private functions? I don't think it's possible to define private function with module_functions.
What are the benefits of module_functions?

Copy link
Member

@excpt excpt Feb 13, 2017

Choose a reason for hiding this comment

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

Using module_function is the preferred coding style.

You're alway able to define a function as private by using this syntax:

def my_fun(my_param) do
  puts my_param
end
private :my_fun

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think module_function respects the private visibility.

module A
  module_function

  def my_fun(my_param)
    puts my_param
  end
  private :my_fun
end

A.my_fun("calling private method")   #=>  "calling private method"

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.

Please merge the current master into your branch. There have been changes. The small issues are commented inline.

@EmilioCristalli
Copy link
Contributor Author

Thanks for the comments @excpt

This branch is already up-to-date with master, sorry I missed NAMED_CURVES was moved to the DefaultOptions module.

In my opinion the constants *_ALGORITHMS and NAMED_CURVES make more sense in the Signature module. This module is the only one that uses them, and they don't seem to be configurable like the other options, so I would rather remove NAMED_CURVES from the DefaultOptions module.

Do you still think it's worth moving the constants to DefaultOptions?

@excpt
Copy link
Member

excpt commented Feb 13, 2017

Agreed. Move the algorithms into the signature module. This makes way more sense. The rest is looking fine. After that change i'll merge your PR.

Thanks again. 🍻

@EmilioCristalli
Copy link
Contributor Author

@excpt I just removed NAMED_CURVES in this commit 8ed32d7.

Let me know what you want to do with the module_function vs extend self discussion

@excpt
Copy link
Member

excpt commented Feb 16, 2017

After looking through the last refactoring to move logic out of the jwt.rb monolith I think the better solution to that problem is to rewrite the module as a class. The Encoding and Verify refactorings took the same approach. This will give you full control about what's private logic and what should be exposed to the outside.

@EmilioCristalli
Copy link
Contributor Author

I think normally a class is meant to have both behavior and state. In this case, Signature doesn't really need state, it's more like two util methods that would let you generate or verify a signature (given an algorithm and key).

So, if you implement Signature as a class you would either use instance methods and you would have to create a new instance to use the methods (which shouldn't be necessary because it doesn't have any state) or you use class methods (with private class methods too), which basically is the same as the module with extend self, but it feels like a module better defines the purpose of the util functions, because it doesn't hold state.

And I think module with extend self gives you as much control of private methods as a class (actually I think to define private class methods is a little trickier because you have to either use class << self or private_class_method)

@excpt do you still prefer to implement this as a class? would you use instance or class methods?

@excpt
Copy link
Member

excpt commented Feb 19, 2017

I totally over-engineered the problem. Sorry.

Thank you very much for the contribution and your patience to withstand my pretty silly review. 👍

The code solves the problem and this is what counts.

@excpt excpt merged commit c97f96a into jwt:master Feb 19, 2017
@excpt
Copy link
Member

excpt commented Feb 19, 2017

@EmilioCristalli
Please take a look at your PR #196 contribution. After merging this PR your new refactor PR has now merge conflicts.

@EmilioCristalli EmilioCristalli deleted the refactor-signature branch February 19, 2017 15:03
@EmilioCristalli
Copy link
Contributor Author

no problem @excpt !

I just rebased and solved conflicts on #196

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

Successfully merging this pull request may close these issues.

2 participants