-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first pass at converting to devise strategy
- Loading branch information
Showing
6 changed files
with
111 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
require 'simple_token_authentication/acts_as_token_authenticatable' | ||
require 'devise' | ||
|
||
require 'simple_token_authentication/model' | ||
require 'simple_token_authentication/acts_as_token_authentication_handler' | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
gonzalo-bulnes
Owner
|
||
require 'simple_token_authentication/configuration' | ||
|
||
module SimpleTokenAuthentication | ||
extend Configuration | ||
module Devise | ||
mattr_accessor :token_header_names | ||
@@header_names = {} | ||
|
||
mattr_accessor :sign_in_token | ||
@@sign_in_token = false | ||
end | ||
|
||
Devise.add_module( | ||
:simple_token_authentication, | ||
route: :session, | ||
strategy: true, | ||
controller: :session, | ||
model: 'simple_token_authentication/model' | ||
) |
33 changes: 0 additions & 33 deletions
33
lib/simple_token_authentication/acts_as_token_authenticatable.rb
This file was deleted.
Oops, something went wrong.
133 changes: 0 additions & 133 deletions
133
lib/simple_token_authentication/acts_as_token_authentication_handler.rb
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'simple_token_authentication/strategy' | ||
|
||
module Devise | ||
module Models | ||
module SimpleTokenAuthentication | ||
extend ActiveSupport::Concern | ||
|
||
# Please see https://gist.github.com/josevalim/fb706b1e933ef01e4fb6 | ||
# before editing this file, the discussion is very interesting. | ||
|
||
included do | ||
private :generate_authentication_token | ||
end | ||
|
||
def ensure_authentication_token | ||
if authentication_token.blank? | ||
self.authentication_token = generate_authentication_token | ||
end | ||
end | ||
|
||
def generate_authentication_token | ||
loop do | ||
token = Devise.friendly_token | ||
break token unless self.class.exists?(authentication_token: token) | ||
end | ||
end | ||
|
||
module ClassMethods | ||
# before_save :ensure_authentication_token | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'devise/strategies/authenticatable' | ||
|
||
module Devise | ||
module Strategies | ||
class SimpleTokenAuthentication < Authenticatable | ||
|
||
def valid? | ||
auth_key.present? | ||
end | ||
|
||
def authenticate! | ||
resource = mapping.to.find_for_authentication(auth_key) | ||
|
||
if resource && validate(resource) { Devise.secure_compare(resource.authentication_token, token) } | ||
success!(resource) | ||
else | ||
return fail(:invalid) | ||
end | ||
|
||
end | ||
|
||
private | ||
|
||
def snake_resource_name | ||
mapping.to.name.underscore | ||
end | ||
|
||
def login_with | ||
@login_with ||= Devise.mappings.find {|k,v| v.class_name == self.class.name}.last.to.authentication_keys.first | ||
self[@login_with] | ||
end | ||
|
||
# Pass in auth key as resource_name_key e.g. user_email or | ||
def auth_key | ||
params["#{snake_resource_name}_#{login_with}"] || lookup_header | ||
end | ||
|
||
def token | ||
params["#{snake_resource_name}_token"] || token_header | ||
end | ||
|
||
def configured_headings | ||
::Devise.token_header_names[.to_sym] | ||
end | ||
|
||
def token_header | ||
configured_key = configured_headings[:authentication_token] | ||
token_key = configured_key.presence ? configured_key : "X-#{mapping.to.name}-Token" | ||
return request.headers[token_key] | ||
end | ||
|
||
def lookup_header | ||
configured_key = configured_headings[login_with.to_sym] | ||
lookup_key = configured_key.presence ? configured_key : "X-#{mapping.to.name}-#{login_with.camelize}" | ||
return request.headers[lookup_key] | ||
end | ||
end | ||
end | ||
end | ||
|
||
Warden::Strategies.add(:simple_token_authenticatable, Devise::Strategies::SimpleTokenAuthentication) |
Hi @jbender, since
lib/simple_token_authentication/acts_as_token_authentication_handler
was removed, this line should be removed too.