rails_warden 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.textile +34 -0
- data/Rakefile +57 -0
- data/TODO +1 -0
- data/lib/rails_warden.rb +37 -0
- data/lib/rails_warden/controller_mixin.rb +48 -0
- data/lib/rails_warden/manager.rb +30 -0
- data/lib/rails_warden/rails_settings.rb +51 -0
- data/spec/controller_mixin_spec.rb +72 -0
- data/spec/rails_warden_spec.rb +57 -0
- data/spec/spec_helper.rb +4 -0
- metadata +76 -0
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,57 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rubygems/specification'
|
4
|
+
require 'date'
|
5
|
+
require 'spec/rake/spectask'
|
6
|
+
|
7
|
+
GEM = "rails_warden"
|
8
|
+
GEM_VERSION = "0.1.1"
|
9
|
+
AUTHOR = "Daniel Neighman"
|
10
|
+
EMAIL = "has.sox@gmail.com"
|
11
|
+
HOMEPAGE = "http://github.com/hassox/rails_warden"
|
12
|
+
SUMMARY = "A gem that provides authenitcation via the Warden framework"
|
13
|
+
|
14
|
+
spec = Gem::Specification.new do |s|
|
15
|
+
s.name = GEM
|
16
|
+
s.version = GEM_VERSION
|
17
|
+
s.platform = Gem::Platform::RUBY
|
18
|
+
s.has_rdoc = true
|
19
|
+
s.extra_rdoc_files = ["README.textile", "LICENSE", 'TODO']
|
20
|
+
s.summary = SUMMARY
|
21
|
+
s.description = s.summary
|
22
|
+
s.author = AUTHOR
|
23
|
+
s.email = EMAIL
|
24
|
+
s.homepage = HOMEPAGE
|
25
|
+
|
26
|
+
# Uncomment this to add a dependency
|
27
|
+
s.add_dependency "warden"
|
28
|
+
|
29
|
+
s.require_path = 'lib'
|
30
|
+
s.autorequire = GEM
|
31
|
+
s.files = %w(LICENSE README.textile Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
|
32
|
+
end
|
33
|
+
|
34
|
+
task :default => :spec
|
35
|
+
|
36
|
+
desc "Run specs"
|
37
|
+
Spec::Rake::SpecTask.new do |t|
|
38
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
39
|
+
t.spec_opts = %w(-fs --color)
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
44
|
+
pkg.gem_spec = spec
|
45
|
+
end
|
46
|
+
|
47
|
+
desc "install the gem locally"
|
48
|
+
task :install => [:package] do
|
49
|
+
sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
|
50
|
+
end
|
51
|
+
|
52
|
+
desc "create a gemspec file"
|
53
|
+
task :make_spec do
|
54
|
+
File.open("#{GEM}.gemspec", "w") do |file|
|
55
|
+
file.puts spec.to_ruby
|
56
|
+
end
|
57
|
+
end
|
data/lib/rails_warden.rb
ADDED
@@ -0,0 +1,37 @@
|
|
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
|
+
ActionController::Base.class_eval{ include RailsWarden::ControllerMixin }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Session Serialization in. This block determines how the user will
|
22
|
+
# be stored in the session. If you're using a complex object like an
|
23
|
+
# ActiveRecord model, it is not a good idea to store the complete object.
|
24
|
+
# An ID is sufficient
|
25
|
+
Warden::Manager.serialize_into_session{ |user| [user.class, user.id] }
|
26
|
+
|
27
|
+
# Session Serialization out. This block gets the user out of the session.
|
28
|
+
# It should be the reverse of serializing the object into the session
|
29
|
+
Warden::Manager.serialize_from_session do |klass, id|
|
30
|
+
klass = case klass
|
31
|
+
when Class
|
32
|
+
klass
|
33
|
+
when String, Symbol
|
34
|
+
klass.to_s.classify.constantize
|
35
|
+
end
|
36
|
+
klass.find(id)
|
37
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module RailsWarden
|
2
|
+
module ControllerMixin
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.send(:include, InstanceMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module InstanceMethods
|
9
|
+
# The main accessor for the warden proxy instance
|
10
|
+
# :api: public
|
11
|
+
def warden
|
12
|
+
request.env['warden']
|
13
|
+
end
|
14
|
+
|
15
|
+
# Proxy to the authenticate method on warden
|
16
|
+
# :api: public
|
17
|
+
def authenticate(*args)
|
18
|
+
warden.authenticate(*args)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Proxy to the authenticate method on warden
|
22
|
+
# :api: public
|
23
|
+
def authenticate!(*args)
|
24
|
+
warden.authenticate!(*args)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Proxy to the authenticated? method on warden
|
28
|
+
# :api: public
|
29
|
+
def authenticated?(*args)
|
30
|
+
warden.authenticated?(*args)
|
31
|
+
end
|
32
|
+
alias_method :logged_in?, :authenticated?
|
33
|
+
|
34
|
+
# Access the currently logged in user
|
35
|
+
# :api: public
|
36
|
+
def user(*args)
|
37
|
+
warden.user(*args)
|
38
|
+
end
|
39
|
+
alias_method :current_user, :user
|
40
|
+
|
41
|
+
# Logout the current user
|
42
|
+
# :api: public
|
43
|
+
def logout(*args)
|
44
|
+
warden.logout(*args)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
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[:defaults] = [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,72 @@
|
|
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::ControllerMixin
|
16
|
+
attr_accessor :env
|
17
|
+
def request
|
18
|
+
self
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
RailsWarden.default_user_class = nil
|
23
|
+
RailsWarden.unauthenticated_action = nil
|
24
|
+
|
25
|
+
@controller = MockController.new
|
26
|
+
@mock_warden = OpenStruct.new
|
27
|
+
@controller.env = {"warden" => @mock_warden }
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should setup the spec" do
|
31
|
+
@controller.warden.should_not be_nil
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should provide access to the warden instance" do
|
35
|
+
@controller.warden.should == @controller.env["warden"]
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should run authenticate on warden" do
|
39
|
+
@mock_warden.should_receive(:authenticate).and_return(true)
|
40
|
+
@controller.authenticate
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should run authenticate! on warden" do
|
44
|
+
@mock_warden.should_receive(:authenticate!).and_return(true)
|
45
|
+
@controller.authenticate!
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should run authenticate? on warden" do
|
49
|
+
@mock_warden.should_receive(:authenticated?).and_return(true)
|
50
|
+
@controller.authenticated?
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should proxy logged_in? to authenticated" do
|
54
|
+
@mock_warden.should_receive(:authenticated?).and_return(true)
|
55
|
+
@controller.logged_in?
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should run user on warden" do
|
59
|
+
@mock_warden.should_receive(:user).and_return(true)
|
60
|
+
@controller.user
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should run current_user on warden" do
|
64
|
+
@mock_warden.should_receive(:user).and_return(true)
|
65
|
+
@controller.current_user
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should proxy logout to warden" do
|
69
|
+
@mock_warden.should_receive(:logout).and_return(true)
|
70
|
+
@controller.logout
|
71
|
+
end
|
72
|
+
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
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_warden
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Neighman
|
8
|
+
autorequire: rails_warden
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-10 00:00:00 +10: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"
|
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
|
+
- README.textile
|
33
|
+
- LICENSE
|
34
|
+
- TODO
|
35
|
+
files:
|
36
|
+
- LICENSE
|
37
|
+
- README.textile
|
38
|
+
- Rakefile
|
39
|
+
- TODO
|
40
|
+
- lib/rails_warden/controller_mixin.rb
|
41
|
+
- lib/rails_warden/manager.rb
|
42
|
+
- lib/rails_warden/rails_settings.rb
|
43
|
+
- lib/rails_warden.rb
|
44
|
+
- spec/controller_mixin_spec.rb
|
45
|
+
- spec/rails_warden_spec.rb
|
46
|
+
- spec/spec_helper.rb
|
47
|
+
has_rdoc: true
|
48
|
+
homepage: http://github.com/hassox/rails_warden
|
49
|
+
licenses: []
|
50
|
+
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.3.3
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: A gem that provides authenitcation via the Warden framework
|
75
|
+
test_files: []
|
76
|
+
|