Skip to content

Commit

Permalink
first pass at converting to devise strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
jbender committed Jun 11, 2014
1 parent 3a4f9b4 commit f72f6e5
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 185 deletions.
21 changes: 17 additions & 4 deletions lib/simple_token_authentication.rb
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.

Copy link
@gonzalo-bulnes

gonzalo-bulnes Jun 14, 2014

Owner

Hi @jbender, since lib/simple_token_authentication/acts_as_token_authentication_handler was removed, this line should be removed too.

This comment has been minimized.

Copy link
@gonzalo-bulnes

gonzalo-bulnes Jun 14, 2014

Owner

Ok, I've seen you fixed it in 7e9adce. Why don't you open a pull request so we can discuss the details there? That would be great!

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 lib/simple_token_authentication/acts_as_token_authenticatable.rb

This file was deleted.

This file was deleted.

15 changes: 0 additions & 15 deletions lib/simple_token_authentication/configuration.rb

This file was deleted.

33 changes: 33 additions & 0 deletions lib/simple_token_authentication/model.rb
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
61 changes: 61 additions & 0 deletions lib/simple_token_authentication/strategy.rb
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)

0 comments on commit f72f6e5

Please sign in to comment.