hassox-rails_warden 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ pkg
2
+ pkg/*
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Daniel Neighman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,34 @@
1
+ h1. Rails Warden
2
+
3
+ Provides authentication for Rails applications via the "Warden":http://github.com/hassox/warden Rack authentication framework.
4
+
5
+ Require the gem:
6
+
7
+ config/environment.rb
8
+
9
+ <pre><code>
10
+ config.gem "rails_warden"
11
+ </code></pre>
12
+
13
+ Setup an initializer:
14
+
15
+ config/initializers/warden.rb
16
+
17
+ <pre><code>
18
+ Rails.configuration.middleware.use RailsWarden::Manager do |manager|
19
+ manager.default_strategies :my_strategy
20
+ manager.failure_app = LoginController
21
+ end
22
+
23
+ # Setup Session Serialization
24
+ Warden::Manager.serialize_into_session{ |user| [user.class, user.id] }
25
+ Warden::Manager.serialize_from_session{ |klass, id| klass.find(id) }
26
+
27
+ # Declare your strategies here
28
+ #Warden::Strategies.add(:my_strategy) do
29
+ # def authenticate!
30
+ # # do stuff
31
+ # end
32
+ #end
33
+
34
+ </code></pre>
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'date'
3
+ require 'spec/rake/spectask'
4
+
5
+ GEM = "rails_warden"
6
+ AUTHORS = ["Daniel Neighman"]
7
+ EMAIL = "has.sox@gmail.com"
8
+ HOMEPAGE = "http://github.com/hassox/rails_warden"
9
+ SUMMARY = "A gem that provides authenitcation via the Warden framework"
10
+
11
+ begin
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ gem.name = GEM
15
+ gem.email = EMAIL
16
+ gem.has_rdoc = true
17
+ gem.extra_rdoc_files = ["README.textile", "LICENSE", 'TODO']
18
+ gem.summary = SUMMARY
19
+ gem.description = SUMMARY
20
+ gem.authors = AUTHORS
21
+ gem.email = EMAIL
22
+ gem.homepage = HOMEPAGE
23
+ gem.rubyforge_project = "warden"
24
+ gem.add_dependency "warden", "> 0.2"
25
+ end
26
+ rescue LoadError
27
+ puts "Jeweler (or a dependency) not available. Install with: sudo gem install jeweler"
28
+ end
29
+
30
+ task :default => :spec
31
+
32
+ desc "Run specs"
33
+ Spec::Rake::SpecTask.new do |t|
34
+ t.spec_files = FileList['spec/**/*_spec.rb']
35
+ t.spec_opts = %w(-fs --color)
36
+ end
data/TODO ADDED
@@ -0,0 +1 @@
1
+ TODO:
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
@@ -0,0 +1,51 @@
1
+ module RailsWarden
2
+ module Mixins
3
+ module HelperMethods
4
+ # The main accessor for the warden proxy instance
5
+ # :api: public
6
+ def warden
7
+ request.env['warden']
8
+ end
9
+
10
+ # Proxy to the authenticated? method on warden
11
+ # :api: public
12
+ def authenticated?(*args)
13
+ warden.authenticated?(*args)
14
+ end
15
+ alias_method :logged_in?, :authenticated?
16
+
17
+ # Access the currently logged in user
18
+ # :api: public
19
+ def user(*args)
20
+ warden.user(*args)
21
+ end
22
+ alias_method :current_user, :user
23
+
24
+ def user=(user)
25
+ warder.set_user user
26
+ end
27
+ alias_method :current_user=, :user=
28
+ end # Helper Methods
29
+
30
+ module ControllerOnlyMethods
31
+ # Logout the current user
32
+ # :api: public
33
+ def logout(*args)
34
+ warden.logout(*args)
35
+ end
36
+
37
+ # Proxy to the authenticate method on warden
38
+ # :api: public
39
+ def authenticate(*args)
40
+ warden.authenticate(*args)
41
+ end
42
+
43
+ # Proxy to the authenticate method on warden
44
+ # :api: public
45
+ def authenticate!(*args)
46
+ warden.authenticate!(*args)
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,30 @@
1
+ module RailsWarden
2
+ class Manager
3
+
4
+ def self.new(app, opts = {}, &block)
5
+ # Get the failure application
6
+ opts[:failure_app] = opts[:failure_app].to_s.classify.constantize if opts[:failure_app]
7
+ opts[:default_strategies] = [opts[:defaults]].flatten if opts[:defaults]
8
+
9
+ # Set the default user
10
+ if user = opts.delete(:default_user)
11
+ RailsWarden.default_user_class = user.to_s.classify.constantize
12
+ end
13
+
14
+ # Set the unauthenticated action if it's set
15
+ if ua = opts.delete(:unauthenticated_action)
16
+ RailsWarden.unauthenticated_action = ua
17
+ end
18
+
19
+ # Rails needs the action to be passed in with the params
20
+ Warden::Manager.before_failure do |env, opts|
21
+ if request = env["action_controller.rescue.request"]
22
+ request.params["action"] = RailsWarden.unauthenticated_action
23
+ end
24
+ end
25
+
26
+ Warden::Manager.new(app, opts, &block)
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,51 @@
1
+ module RailsWarden
2
+
3
+ # Set the default user class for the application
4
+ # :api: public
5
+ def self.default_user_class=(klass)
6
+ @default_user_class = klass
7
+ end
8
+
9
+ # Accessor for the default user class for the application
10
+ # :api: public
11
+ def self.default_user_class
12
+ @default_user_class ||= User
13
+ end
14
+
15
+ # Get the action called when there is an unauthenticated failure
16
+ # This is usually an action on a controller
17
+ # The action is called on the failure application. This would normally be
18
+ # A rails controller
19
+ #
20
+ # Example
21
+ # RailsWarden::Manager.new(@app, :failure_app => "login_controller",
22
+ # :defaults => :password,
23
+ # :unauthenticated_action => :bad_login
24
+ # )
25
+ #
26
+ # The unauthenticated_action is :bad_login
27
+ # The bad_login action will be called on the LoginController
28
+ # :api: public
29
+ def self.unauthenticated_action=(action)
30
+ action = action.to_s if action
31
+ @unauthenticated_action = action
32
+ end
33
+
34
+ # Get the action called when there is an unauthenticated failure
35
+ # This is usually an action on a controller
36
+ # The action is called on the failure application. This would normally be
37
+ # A rails controller
38
+ #
39
+ # Example
40
+ # RailsWarden::Manager.new(@app, :failure_app => "login_controller",
41
+ # :defaults => :password,
42
+ # :unauthenticated_action => :bad_login
43
+ # )
44
+ #
45
+ # The unauthenticated_action is :bad_login
46
+ # The bad_login action will be called on the LoginController
47
+ # :api: public
48
+ def self.unauthenticated_action
49
+ @unauthenticated_action ||= "unauthenticated"
50
+ end
51
+ end
@@ -0,0 +1,44 @@
1
+ here = File.dirname(__FILE__)
2
+
3
+ require 'rubygems'
4
+ require 'warden'
5
+ require 'active_support'
6
+ require "#{here}/rails_warden/manager"
7
+ require "#{here}/rails_warden/rails_settings"
8
+ require "#{here}/rails_warden/controller_mixin"
9
+
10
+ Warden::Manager.before_failure do |env, opts|
11
+ request = env["action_controller.rescue.request"]
12
+ request.params["action"] = RailsWarden.unauthenticated_action || "unauthenticated"
13
+ end
14
+
15
+ if defined?(Rails)
16
+ Rails.configuration.after_initialize do
17
+ class ActionController::Base
18
+ include RailsWarden::Mixins::HelperMethods
19
+ include RailsWarden::Mixins::ControllerOnlyMethods
20
+ end
21
+
22
+ module ApplicationHelper
23
+ include RailsWarden::Mixins::HelperMethods
24
+ end
25
+ end
26
+ end
27
+
28
+ # Session Serialization in. This block determines how the user will
29
+ # be stored in the session. If you're using a complex object like an
30
+ # ActiveRecord model, it is not a good idea to store the complete object.
31
+ # An ID is sufficient
32
+ Warden::Manager.serialize_into_session{ |user| [user.class, user.id] }
33
+
34
+ # Session Serialization out. This block gets the user out of the session.
35
+ # It should be the reverse of serializing the object into the session
36
+ Warden::Manager.serialize_from_session do |klass, id|
37
+ klass = case klass
38
+ when Class
39
+ klass
40
+ when String, Symbol
41
+ klass.to_s.classify.constantize
42
+ end
43
+ klass.find(id)
44
+ end
@@ -0,0 +1,59 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{rails_warden}
5
+ s.version = "0.2.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Daniel Neighman"]
9
+ s.date = %q{2009-07-12}
10
+ s.description = %q{A gem that provides authenitcation via the Warden framework}
11
+ s.email = %q{has.sox@gmail.com}
12
+ s.extra_rdoc_files = [
13
+ "LICENSE",
14
+ "README.textile",
15
+ "TODO"
16
+ ]
17
+ s.files = [
18
+ ".gitignore",
19
+ "LICENSE",
20
+ "README.textile",
21
+ "Rakefile",
22
+ "TODO",
23
+ "VERSION",
24
+ "lib/rails_warden.rb",
25
+ "lib/rails_warden/controller_mixin.rb",
26
+ "lib/rails_warden/manager.rb",
27
+ "lib/rails_warden/rails_settings.rb",
28
+ "rails_warden.gemspec",
29
+ "script/destroy",
30
+ "script/generate",
31
+ "spec/controller_mixin_spec.rb",
32
+ "spec/rails_warden_spec.rb",
33
+ "spec/spec_helper.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/hassox/rails_warden}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubyforge_project = %q{warden}
39
+ s.rubygems_version = %q{1.3.3}
40
+ s.summary = %q{A gem that provides authenitcation via the Warden framework}
41
+ s.test_files = [
42
+ "spec/controller_mixin_spec.rb",
43
+ "spec/rails_warden_spec.rb",
44
+ "spec/spec_helper.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
52
+ s.add_runtime_dependency(%q<warden>, ["> 0.2"])
53
+ else
54
+ s.add_dependency(%q<warden>, ["> 0.2"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<warden>, ["> 0.2"])
58
+ end
59
+ end
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:newgem_simple, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:newgem_simple, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,73 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+
4
+ describe "rails_warden controller mixin" do
5
+
6
+ before(:each) do
7
+ @app = lambda{|e| [200, {"Content-Type" => "text/plain"}, ["resonse"]]}
8
+ class FooFailure
9
+ end
10
+
11
+ class User
12
+ end
13
+
14
+ class MockController
15
+ include RailsWarden::Mixins::HelperMethods
16
+ include RailsWarden::Mixins::ControllerOnlyMethods
17
+ attr_accessor :env
18
+ def request
19
+ self
20
+ end
21
+ end
22
+
23
+ RailsWarden.default_user_class = nil
24
+ RailsWarden.unauthenticated_action = nil
25
+
26
+ @controller = MockController.new
27
+ @mock_warden = OpenStruct.new
28
+ @controller.env = {"warden" => @mock_warden }
29
+ end
30
+
31
+ it "should setup the spec" do
32
+ @controller.warden.should_not be_nil
33
+ end
34
+
35
+ it "should provide access to the warden instance" do
36
+ @controller.warden.should == @controller.env["warden"]
37
+ end
38
+
39
+ it "should run authenticate on warden" do
40
+ @mock_warden.should_receive(:authenticate).and_return(true)
41
+ @controller.authenticate
42
+ end
43
+
44
+ it "should run authenticate! on warden" do
45
+ @mock_warden.should_receive(:authenticate!).and_return(true)
46
+ @controller.authenticate!
47
+ end
48
+
49
+ it "should run authenticate? on warden" do
50
+ @mock_warden.should_receive(:authenticated?).and_return(true)
51
+ @controller.authenticated?
52
+ end
53
+
54
+ it "should proxy logged_in? to authenticated" do
55
+ @mock_warden.should_receive(:authenticated?).and_return(true)
56
+ @controller.logged_in?
57
+ end
58
+
59
+ it "should run user on warden" do
60
+ @mock_warden.should_receive(:user).and_return(true)
61
+ @controller.user
62
+ end
63
+
64
+ it "should run current_user on warden" do
65
+ @mock_warden.should_receive(:user).and_return(true)
66
+ @controller.current_user
67
+ end
68
+
69
+ it "should proxy logout to warden" do
70
+ @mock_warden.should_receive(:logout).and_return(true)
71
+ @controller.logout
72
+ end
73
+ end
@@ -0,0 +1,57 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "rails_warden" do
4
+
5
+ before(:each) do
6
+ @app = lambda{|e| [200, {"Content-Type" => "text/plain"}, ["resonse"]]}
7
+ class FooFailure
8
+ end
9
+
10
+ class FooUser
11
+ end
12
+
13
+ class User
14
+ end
15
+
16
+ RailsWarden.default_user_class = nil
17
+ RailsWarden.unauthenticated_action = nil
18
+ end
19
+
20
+ it "RailsWarden::Manager.new should return an instance of Warden::Manager" do
21
+ r = RailsWarden::Manager.new(@app, :failure_app => "foo_failure", :defaults => :password)
22
+ r.should be_an_instance_of(Warden::Manager)
23
+ end
24
+
25
+ it "should set the failure application to FooFailure" do
26
+ r = RailsWarden::Manager.new(@app, :failure_app => "foo_failure", :defaults => :password)
27
+ r.failure_app.should == FooFailure
28
+ end
29
+
30
+ it "should set the default user to FooUser if specified" do
31
+ r = RailsWarden::Manager.new(@app, :failure_app => "foo_failure",
32
+ :defaults => :password,
33
+ :default_user => "foo_user")
34
+ RailsWarden.default_user_class.should == FooUser
35
+ end
36
+
37
+ it "should set the default user to User if there is none specified" do
38
+ r = RailsWarden::Manager.new(@app, :failure_app => "foo_failure",
39
+ :defaults => :password)
40
+ RailsWarden.default_user_class.should == User
41
+ end
42
+
43
+ it "should set the failure action when specified" do
44
+ r = RailsWarden::Manager.new(@app, :failure_app => "foo_failure",
45
+ :defaults => :password,
46
+ :unauthenticated_action => :bad_login
47
+ )
48
+ RailsWarden.unauthenticated_action.should == "bad_login"
49
+ end
50
+
51
+ it "should set the failure action to unauthenticated when not specified" do
52
+ r = RailsWarden::Manager.new(@app, :failure_app => "foo_failure",
53
+ :defaults => :password
54
+ )
55
+ RailsWarden.unauthenticated_action.should == "unauthenticated"
56
+ end
57
+ end
@@ -0,0 +1,4 @@
1
+ $TESTING=true
2
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
3
+
4
+ require 'rails_warden'
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hassox-rails_warden
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Neighman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-12 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: warden
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: "0.2"
24
+ version:
25
+ description: A gem that provides authenitcation via the Warden framework
26
+ email: has.sox@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.textile
34
+ - TODO
35
+ files:
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.textile
39
+ - Rakefile
40
+ - TODO
41
+ - VERSION
42
+ - lib/rails_warden.rb
43
+ - lib/rails_warden/controller_mixin.rb
44
+ - lib/rails_warden/manager.rb
45
+ - lib/rails_warden/rails_settings.rb
46
+ - rails_warden.gemspec
47
+ - script/destroy
48
+ - script/generate
49
+ - spec/controller_mixin_spec.rb
50
+ - spec/rails_warden_spec.rb
51
+ - spec/spec_helper.rb
52
+ has_rdoc: false
53
+ homepage: http://github.com/hassox/rails_warden
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --charset=UTF-8
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project: warden
74
+ rubygems_version: 1.2.0
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: A gem that provides authenitcation via the Warden framework
78
+ test_files:
79
+ - spec/controller_mixin_spec.rb
80
+ - spec/rails_warden_spec.rb
81
+ - spec/spec_helper.rb