Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Warden Introduction

22,987 views

Published on

A introductory talk about Warden, a Ruby Rack authentication framework, to the Melbourne Ruby Users Group

Published in: Technology
  • on slide25, must be User.get(key)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Warden Introduction

  1. 1. Warden General Rack Authentication
  2. 2. Many Web Apps Need
  3. 3. 1. A User
  4. 4. 2. A way to associate it with a request
  5. 5. Many Solutions Exist • • Restful Authentication Basic Auth • • AuthLogic Digest • • Merbful Authentication LAPD • • Merb-Auth CAS • Home Grown • OpenID
  6. 6. Why Another One?
  7. 7. Rack
  8. 8. Rack Router
  9. 9. class SimpleRack include Rack::Router::Routable def initialize prepare do |r| r.map quot;/quot;, :to => router { |c| c.map quot;/helloquot;, :to => ChildApp } r.map quot;/helloquot;, :to => ParentApp r.map quot;/onequot;, :to => lambda { |env| do_one(env) } r.map quot;/twoquot;, :to => lambda { |env| do_two(env) } end end def do_one(env) # Stuff end def do_two(env) # Stuff end end
  10. 10. Mountable Apps
  11. 11. How Will Your Authentication Cope?
  12. 12. Apps Usually Need a “User”
  13. 13. Current Breed Will Conflict Between Apps
  14. 14. Warden • Injects a very lazy proxy into the request • Proxy follows around in the request • Does nothing until you ask it • Authenticates Requests for any kind of “User” • Provides a mechanism for Authentication • Available in all downstream Rack parts
  15. 15. Authenticating (Logging In) env['warden'].authenticate :password env['warden'].authenticate! :password, :basic, :open_id env['warden'].authenticated? :password
  16. 16. Accessing The User env['warden'].user
  17. 17. Logging Out env['warden'].logout
  18. 18. Authentication Logic • Strategy Based • Packagable • Sharable between discrete Apps • Simple
  19. 19. Strategy Warden::Strategies.add(:password) do def valid? params[:username] || params[:password] end def authenticate! u = User.authenticate(params[:username], params[:password]) u.nil? ? fail!(quot;Could not log inquot;) : success!(u) end end
  20. 20. Strategies • Multiple Strategies • Strategies Cascade env['warden'].authenticate! :password, :basic, :open_id
  21. 21. Failure throw(:warden) throw(:warden, :some => :option) Drops out to a “Failure Application”
  22. 22. Rack Setup Rack::Builder.new do use Rack::Session::Cookie use Warden::Manager do |manager| manager.default_strategies :password, :basic manager.failure_app = BadAuthenticationEndsUpHere end run SomeApp end
  23. 23. Session Integration Warden::Manager.serialize_into_session{ |user| user.id } Warden::Manager.serialize_from_session{ |key| User.get(id)}
  24. 24. Other Features • Callbacks • User Scopes - Multiple Users / session • Authenticated Session Data • Locks Session per user
  25. 25. Rails Integration config/initializers/warden.rb Rails.configuration.middleware.use Warden::Manager do |manager| manager.default_strategies :password manager.failure_app = LoginController end # Rails needs the action to be passed in with the params Warden::Manager.before_failure do |env, opts| request = env[quot;action_controller.rescue.requestquot;] request.params[quot;actionquot;] = quot;unauthenticatedquot; end # Session Serialization & Strategies
  26. 26. More Information • http://github.com/hassox/warden • http://wiki.github.com/hassox/warden

×