-
-
Save rfunduk/1239474 to your computer and use it in GitHub Desktop.
Code snippets for http://ryanfunduk.com/shared-auth-for-rack-apps
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
namespace :admin do | |
mount Resque::Server, :at => '/resque' | |
# ... | |
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
gem 'warden' | |
gem 'rails_warden' |
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
Rails.configuration.middleware.use RailsWarden::Manager do |manager| | |
manager.default_strategies :admin | |
manager.failure_app = Admin::SessionsController.action(:new) | |
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
Warden::Manager.serialize_into_session do |user| | |
user.id.to_s | |
end | |
Warden::Manager.serialize_from_session do |id| | |
Administrator.find( id ) | |
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
Warden::Strategies.add( :admin ) do | |
def valid? | |
params[:email] || params[:password] | |
end | |
def authenticate! | |
begin | |
email = params[:email].downcase | |
admin = Administrator.where( email: email ).first | |
unless admin.authenticate( params[:password] ) | |
raise StandardError | |
end | |
success! admin | |
rescue | |
admin = nil | |
fail! | |
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
def require_admin | |
# get admin id from session and look up | |
# logged in user by hand and assign to @admin | |
redirect_to new_admin_session_path unless @admin | |
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
def require_admin | |
warden.authenticate! :admin | |
@admin = warden.user | |
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
class MountedAdminAppAuth | |
def initialize( app ) | |
@app = app | |
end | |
def call( env ) | |
env['rack.session.options'] = { | |
key: 'YOUR_SESSION_KEY', | |
secret: YourApp::Application.config.secret_token | |
} | |
env['warden'].authenticate! | |
@app.call( env ) | |
end | |
end | |
Route53::Web.use MountedAdminAppAuth |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment