Created
May 26, 2015 00:21
-
-
Save ouranos/5819e9b144c8e7ac7a59 to your computer and use it in GitHub Desktop.
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
# config/application.rb | |
config.middleware.insert_after ActionDispatch::Flash, Warden::Manager do |manager| | |
manager.default_strategies :authentication_token, :basic_auth | |
manager.failure_app = UnauthorizedController | |
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
#lib/strategies/basic_auth_strategy.rb | |
class BasicAuthStrategy < ::Warden::Strategies::Base | |
def auth | |
@auth ||= Rack::Auth::Basic::Request.new(env) | |
end | |
def valid? | |
auth.provided? && auth.basic? && auth.credentials | |
end | |
def authenticate! | |
user = User.find_by_username(auth.credentials[0]) | |
if user && user.authenticate(auth.credentials[1]) | |
success!(user) | |
else | |
fail!('strategies.basic_auth.failed') | |
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
# config/initializers/warden.rb | |
require Rails.root.join('lib/strategies/authentication_token_strategy') | |
require Rails.root.join('lib/strategies/basic_auth_strategy') | |
Warden::Strategies.add(:authentication_token, AuthenticationTokenStrategy) | |
Warden::Strategies.add(:basic_auth, BasicAuthStrategy) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment