Skip to content

Instantly share code, notes, and snippets.

@wkrsz
Created January 9, 2013 11:48
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save wkrsz/4492569 to your computer and use it in GitHub Desktop.
Save wkrsz/4492569 to your computer and use it in GitHub Desktop.
Support for token HTTP header authentication in Devise
#config/initializers/devise.rb
config.warden do |manager|
manager.strategies.add :token_header_authenticable, TokenHeaderAuthenticable
manager.default_strategies(:scope => :user).unshift :token_header_authenticable
end
#lib/token_header_authenticable.rb
class TokenHeaderAuthenticable < ::Devise::Strategies::Base
def valid?
token_value.present?
end
def authenticate!
resource_scope = mapping.to
resource = resource_scope.find_for_token_authentication(auth_token: token_value)
if resource
success!(resource)
else
fail!
end
end
private
def token_value
if header && header =~ /^Token token="(.+)"$/
$~[1]
end
end
def header
request.headers["Authorization"]
end
end
@jancel
Copy link

jancel commented Jan 9, 2013

How did you test the authenticate! method here?

@wkrsz
Copy link
Author

wkrsz commented Jan 10, 2013

So far I did only integration tests on one controller using it:
response 200 for correct token
response 401 for incorrect token
response 401 for missing token

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