Skip to content

Commit

Permalink
Tidies up some whitespace, and adds a hook for plugins to specify how…
Browse files Browse the repository at this point in the history
… they can clear the whole session
  • Loading branch information
Daniel Neighman committed Sep 5, 2009
1 parent 76d8ae7 commit c473bc6
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 134 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,2 +1,3 @@
pkg
pkg/*
pkg/*
*.gem
1 change: 1 addition & 0 deletions lib/warden.rb
@@ -1,3 +1,4 @@
# encoding: utf-8
require 'forwardable'
$:.unshift File.join(File.dirname(__FILE__))
require 'warden/mixins/common'
Expand Down
1 change: 1 addition & 0 deletions lib/warden/authentication/hooks.rb
@@ -1,3 +1,4 @@
# encoding: utf-8
module Warden
class Manager

Expand Down
1 change: 1 addition & 0 deletions lib/warden/authentication/strategies.rb
@@ -1,3 +1,4 @@
# encoding: utf-8
module Warden
module Strategies
class << self
Expand Down
1 change: 1 addition & 0 deletions lib/warden/authentication/strategy_base.rb
@@ -1,3 +1,4 @@
# encoding: utf-8
module Warden
module Strategies
class Base
Expand Down
1 change: 1 addition & 0 deletions lib/warden/errors.rb
@@ -1,3 +1,4 @@
# encoding: utf-8
module Warden
class Proxy
# :api: public
Expand Down
53 changes: 27 additions & 26 deletions lib/warden/manager.rb
@@ -1,26 +1,27 @@
# encoding: utf-8
module Warden
# The middleware for Rack Authentication
# The middlware requires that there is a session upstream
# The middleware injects an authentication object into
# The middleware injects an authentication object into
# the rack environment hash
class Manager
attr_accessor :config, :failure_app
attr_accessor :config, :failure_app

# initialize the middleware.
# Provide a :failure_app in the options to setup an application to run when there is a failure
# The manager is yielded when initialized with a block. This is useful when declaring it in Rack::Builder
# :api: public
# :api: public
def initialize(app, config = {})
@app = app
@config = config
yield self if block_given?

# should ensure there is a failure application defined.
@failure_app = config[:failure_app] if config[:failure_app]
raise "No Failure App provided" unless @failure_app
self
end
end

# Set the default strategies to use.
# :api: public
def default_strategies(*strategies)
Expand All @@ -30,12 +31,12 @@ def default_strategies(*strategies)
@config[:default_strategies] = strategies.flatten
end
end

# :api: private
def call(env) # :nodoc:
# if this is downstream from another warden instance, don't do anything.
return @app.call(env) unless env['warden'].nil?
return @app.call(env) unless env['warden'].nil?

env['warden'] = Proxy.new(env, @config)
result = catch(:warden) do
@app.call(env)
Expand All @@ -55,28 +56,28 @@ def call(env) # :nodoc:
end # case result
end
end
class << self

class << self


# Does the work of storing the user in the session
# :api: private
def _store_user(user, session, scope = :default) # :nodoc:
def _store_user(user, session, scope = :default) # :nodoc:
return nil if user.nil?
session["warden.user.#{scope}.key"] = serialize_into_session.call(user)
end

# Does the work of fetching the user from the session
# :api: private
def _fetch_user(session, scope = :default) # :nodoc:
key = session["warden.user.#{scope}.key"]
return nil if key.nil?
serialize_from_session.call(key)
end

# Prepares the user to serialize into the session.
# Any object that can be serialized into the session in some way can be used as a "user" object
# Generally however complex object should not be stored in the session.
# Generally however complex object should not be stored in the session.
# If possible store only a "key" of the user object that will allow you to reconstitute it.
#
# Example:
Expand All @@ -87,22 +88,22 @@ def serialize_into_session(&block)
@serialize_into_session = block if block_given?
@serialize_into_session ||= lambda{|user| user}
end

# Reconstitues the user from the session.
# Use the results of user_session_key to reconstitue the user from the session on requests after the initial login
#
#
# Example:
# Warden::Manager.serialize_from_session{ |id| User.get(id) }
#
# :api: public
def serialize_from_session(&blk)
@serialize_from_session = blk if block_given?
@serialize_from_session ||= lambda{|key| key}
end
end
end

private
# When a request is unauthentiated, here's where the processing occurs.
# When a request is unauthentiated, here's where the processing occurs.
# It looks at the result of the proxy to see if it's been executed and what action to take.
# :api: private
def process_unauthenticated(result, env)
Expand All @@ -117,7 +118,7 @@ def process_unauthenticated(result, env)
call_failure_app(env, result)
end # case env['warden'].result
end

# Calls the failure app.
# The before_failure hooks are run on each failure
# :api: private
Expand All @@ -127,10 +128,10 @@ def call_failure_app(env, opts = {})
else
env["PATH_INFO"] = "/#{opts[:action]}"
env["warden.options"] = opts

# Call the before failure callbacks
Warden::Manager._before_failure.each{|hook| hook.call(env,opts)}

@failure_app.call(env).to_a
end
end # call_failure_app
Expand Down
18 changes: 13 additions & 5 deletions lib/warden/mixins/common.rb
@@ -1,25 +1,33 @@
# encoding: utf-8
module Warden
module Mixins
module Common

# Convinience method to access the session
# :api: public
def session
@env['rack.session']
env['rack.session']
end # session

alias_method :raw_session, :session

# Convenience method to access the rack request
# :api: public
def request
@request ||= Rack::Request.new(@env)
end # request

# Convenience method to access the rack request params
# :api: public
def params
request.params
end # params


# Resets the session. By using this non-hash like sessions can
# be cleared by overwriting this method in a plugin
# @api overwritable
def reset_session!
raw_session.clear
end
end # Common
end # Mixins
end # Warden

0 comments on commit c473bc6

Please sign in to comment.