Skip to content

Conversation

ebihara99999
Copy link
Contributor

@ebihara99999 ebihara99999 commented Jan 12, 2019

This is from #70.
There are some reviews with no response and the PR is stale for a long time (though the PR is great!!).

Thank @wilddima for implementing them firstly!!

wilddima and others added 18 commits June 5, 2017 17:44
There is a suggestion that a message in the case not authenticated could be changeable
in the discussion: Sorcery#70 (comment)

But I decide to make use of a message sent from `JWT::DecodeError` because
it's difficult to make the message flexible because of the design.

Changes:

- `#jwt_require_auth` only rescues `JWT::DecodeError` because JWT::VerificationError is a subclass.
- `#jwt_decode` raises `JWT::DecodeError` or the error of the subclasses, and
does not rescue in it.
- `#jwt_not_authenticated` receives an argument

`JWT::DecodeError` are from here: https://github.com/jwt/ruby-jwt/blob/master/lib/jwt/error.rb
@ebihara99999 ebihara99999 mentioned this pull request Jan 12, 2019
`JWT::ExpiredSignature: Signature has expired` is expected when a token is
expired.

Review comment is: Sorcery#70 (comment)
@ebihara99999
Copy link
Contributor Author

TODO

@ebihara99999 ebihara99999 changed the title [WIP] JWT authentication again JWT authentication ( after the commit b06aa7a ) Jan 15, 2019
Copy link
Member

@joshbuker joshbuker left a comment

Choose a reason for hiding this comment

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

Looks like there's still some conflicts with the latest changes on master, can you merge master into this branch and resolve any file conflicts?

@ebihara99999
Copy link
Contributor Author

@athix Thanks, I've fixed them just now

@joshbuker joshbuker self-requested a review January 18, 2019 09:19
sorcery.gemspec Outdated
s.add_dependency 'bcrypt', '~> 3.1'
s.add_dependency 'oauth', '~> 0.4', '>= 0.4.4'
s.add_dependency 'oauth2', '~> 1.0', '>= 0.8.0'
s.add_dependency 'bcrypt', '~> 3.1'
Copy link
Member

Choose a reason for hiding this comment

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

@ebihara99999 Duplicate bcrypt dependency, likely from a merge. I would double check anything that changed on your merge commits.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@athix I'm sorry to add the wrong dependency. I've fixed it just now

@joshbuker joshbuker added the help wanted Community assistance requested label Mar 16, 2019
@joshbuker
Copy link
Member

If anyone in the community would like to help with this, I think JWT support would be a great addition to Sorcery.

# A flag that specifies whether to perform a database query to set the current_user
# Default: true
#
# config.jwt_set_user =
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need this flag? Is there ever a case when current_user should not be an instance of a user model?


# To be used as a before_action.
def jwt_require_auth
binding.pry
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this is here by accident? :)


module InstanceMethods
# This method return generated token if user can be authenticated
def jwt_auth(*credentials)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think slightly more appropriate name for the method would be jwt_authenticate. Simply to stay consistent with User.authenticate.

Copy link
Contributor

Choose a reason for hiding this comment

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

Method should also allow users to get login failure reason, similar to what login method does.

binding.pry
@current_user = Config.jwt_set_user ? User.find(jwt_user_id) : jwt_user_data
rescue JWT::DecodeError => e
jwt_not_authenticated(message: e.message) && return
Copy link
Contributor

Choose a reason for hiding this comment

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

Is trailing return call needed here?

Copy link
Contributor

Choose a reason for hiding this comment

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

jwt_not_authenticated is currently only called when JWT is not decoded properly. We should also handle cases when JWT is properly decoded, but the user record is missing.

# Take token from header, by key defined in config
# With memoization
def jwt_from_header
@jwt_header_token ||= request.headers[Config.jwt_headers_key]
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that more sensible default here would be to expect header value in one of the following formats:Bearer <jwt-token> or Token <jwt-token>. Same as what ActionController's authentication does – https://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Token.html

Further more, does it makes sense to allow users to define how should token be extracted from request?

now = Time.current
default_payload = {
sub: user.id,
exp: (now + 3.days).to_i,
Copy link
Contributor

Choose a reason for hiding this comment

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

Token duration should be configurable.

@joshbuker joshbuker added the to be implemented in v1 This issue or pull request will be resolved in the v1 rework, but has not yet been completed. label Apr 5, 2021
@joshbuker
Copy link
Member

Closing this out to reduce noise, see #239 and Sorcery/sorcery-rework#9 for the latest on JWT in Sorcery.

@joshbuker joshbuker closed this Apr 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Community assistance requested to be implemented in v1 This issue or pull request will be resolved in the v1 rework, but has not yet been completed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants