alex-sinatra_warden 0.3.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +24 -0
- data/Gemfile +22 -0
- data/LICENSE +20 -0
- data/README.rdoc +49 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/alex-sinatra_warden.gemspec +67 -0
- data/lib/sinatra_warden/sinatra.rb +131 -0
- data/lib/sinatra_warden.rb +9 -0
- data/spec/fixtures/basic_strategy.rb +15 -0
- data/spec/fixtures/testing_login.rb +55 -0
- data/spec/fixtures/user.rb +13 -0
- data/spec/fixtures/views/login.haml +0 -0
- data/spec/sinatra_warden_spec.rb +215 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +40 -0
- metadata +112 -0
data/.document
ADDED
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
group :runtime do
|
4
|
+
gem 'sinatra', '>= 0.9.4'
|
5
|
+
gem 'warden', '>= 0.10.3'
|
6
|
+
end
|
7
|
+
|
8
|
+
group :test do
|
9
|
+
gem 'rake'
|
10
|
+
gem 'jeweler', '~> 1.3.0'
|
11
|
+
gem 'bundler', '~> 0.9.7'
|
12
|
+
gem 'rspec', '~> 1.2.9', :require => 'spec'
|
13
|
+
gem 'yard', '>= 0.5.4'
|
14
|
+
gem 'rack-test', '~> 0.5.0', :require => 'rack/test'
|
15
|
+
gem 'rcov'
|
16
|
+
|
17
|
+
gem 'do_sqlite3', '~> 0.10.0'
|
18
|
+
gem 'dm-core', '~> 0.10.1'
|
19
|
+
gem 'bcrypt-ruby', :require => 'bcrypt'
|
20
|
+
gem 'haml'
|
21
|
+
gem 'rack-flash', '~> 0.1.1', :require => 'rack-flash'
|
22
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Justin Smestad
|
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.rdoc
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
= Sinatra::Warden
|
2
|
+
|
3
|
+
A Sinatra (http://github.com/sinatra/sinatra) module that provides authentication for your Sinatra application through Warden (http://github.com/hassox/warden).
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
require 'sinatra'
|
8
|
+
require 'sinatra_warden'
|
9
|
+
|
10
|
+
class Application < Sinatra::Base
|
11
|
+
register Sinatra::Warden
|
12
|
+
|
13
|
+
get '/admin' do
|
14
|
+
authorize!('/login') # require session, redirect to '/login' instead of work
|
15
|
+
haml :admin
|
16
|
+
end
|
17
|
+
|
18
|
+
get '/dashboard' do
|
19
|
+
authorize! # require a session for this action
|
20
|
+
haml :dashboard
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
== More Information
|
25
|
+
|
26
|
+
Please read the wiki (http://wiki.github.com/jsmestad/sinatra_warden) for more information on more advanced configurations.
|
27
|
+
|
28
|
+
== Note on Patches/Pull Requests
|
29
|
+
|
30
|
+
$ git clone git://github.com/jsmestad/sinatra_warden.git
|
31
|
+
$ cd sinatra_warden
|
32
|
+
$ bundle install
|
33
|
+
$ bundle exec rake
|
34
|
+
|
35
|
+
* Fork the project.
|
36
|
+
* Make your feature addition or bug fix.
|
37
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
38
|
+
* Commit, do not mess with rakefile, version, or history.
|
39
|
+
* Send me a pull request. Bonus points for topic branches.
|
40
|
+
|
41
|
+
== Contributors
|
42
|
+
|
43
|
+
* Justin Smestad (http://github.com/jsmestad)
|
44
|
+
* Daniel Neighman (http://github.com/hassox)
|
45
|
+
* Shane Hanna (http://github.com/shanna)
|
46
|
+
|
47
|
+
== Copyright
|
48
|
+
|
49
|
+
Copyright (c) 2009 Justin Smestad. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'bundler'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "alex-sinatra_warden"
|
8
|
+
gem.summary = %Q{authentication system for using warden with sinatra}
|
9
|
+
gem.description = %Q{basic helpers and authentication methods for using warden with sinatra also providing some hooks into Rack::Flash}
|
10
|
+
gem.email = "justin.smestad@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/crhym3/sinatra_warden"
|
12
|
+
gem.authors = ["Justin Smestad", "Daniel Neighman"]
|
13
|
+
|
14
|
+
bundle = Bundler::Definition.from_gemfile('Gemfile')
|
15
|
+
bundle.dependencies.each do |dep|
|
16
|
+
next unless dep.groups.include?(:runtime)
|
17
|
+
gem.add_dependency(dep.name, dep.version_requirements.to_s)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
Jeweler::GemcutterTasks.new
|
21
|
+
rescue LoadError
|
22
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'spec/rake/spectask'
|
26
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
27
|
+
spec.libs << 'lib' << 'spec'
|
28
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
29
|
+
end
|
30
|
+
|
31
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
32
|
+
spec.libs << 'lib' << 'spec'
|
33
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
34
|
+
spec.rcov = true
|
35
|
+
end
|
36
|
+
|
37
|
+
task :default => :spec
|
38
|
+
|
39
|
+
begin
|
40
|
+
require 'yard'
|
41
|
+
YARD::Rake::YardocTask.new
|
42
|
+
rescue LoadError
|
43
|
+
task :yardoc do
|
44
|
+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
45
|
+
end
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.3.0.1
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{alex-sinatra_warden}
|
8
|
+
s.version = "0.3.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Justin Smestad", "Daniel Neighman"]
|
12
|
+
s.date = %q{2010-04-24}
|
13
|
+
s.description = %q{basic helpers and authentication methods for using warden with sinatra also providing some hooks into Rack::Flash}
|
14
|
+
s.email = %q{justin.smestad@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"Gemfile",
|
23
|
+
"LICENSE",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"alex-sinatra_warden.gemspec",
|
28
|
+
"lib/sinatra_warden.rb",
|
29
|
+
"lib/sinatra_warden/sinatra.rb",
|
30
|
+
"spec/fixtures/basic_strategy.rb",
|
31
|
+
"spec/fixtures/testing_login.rb",
|
32
|
+
"spec/fixtures/user.rb",
|
33
|
+
"spec/fixtures/views/login.haml",
|
34
|
+
"spec/sinatra_warden_spec.rb",
|
35
|
+
"spec/spec.opts",
|
36
|
+
"spec/spec_helper.rb"
|
37
|
+
]
|
38
|
+
s.homepage = %q{http://github.com/crhym3/sinatra_warden}
|
39
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
40
|
+
s.require_paths = ["lib"]
|
41
|
+
s.rubygems_version = %q{1.3.6}
|
42
|
+
s.summary = %q{authentication system for using warden with sinatra}
|
43
|
+
s.test_files = [
|
44
|
+
"spec/fixtures/basic_strategy.rb",
|
45
|
+
"spec/fixtures/testing_login.rb",
|
46
|
+
"spec/fixtures/user.rb",
|
47
|
+
"spec/sinatra_warden_spec.rb",
|
48
|
+
"spec/spec_helper.rb"
|
49
|
+
]
|
50
|
+
|
51
|
+
if s.respond_to? :specification_version then
|
52
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
53
|
+
s.specification_version = 3
|
54
|
+
|
55
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
56
|
+
s.add_runtime_dependency(%q<sinatra>, [">= 0.9.4"])
|
57
|
+
s.add_runtime_dependency(%q<warden>, [">= 0.10.3"])
|
58
|
+
else
|
59
|
+
s.add_dependency(%q<sinatra>, [">= 0.9.4"])
|
60
|
+
s.add_dependency(%q<warden>, [">= 0.10.3"])
|
61
|
+
end
|
62
|
+
else
|
63
|
+
s.add_dependency(%q<sinatra>, [">= 0.9.4"])
|
64
|
+
s.add_dependency(%q<warden>, [">= 0.10.3"])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
@@ -0,0 +1,131 @@
|
|
1
|
+
module Sinatra
|
2
|
+
module Warden
|
3
|
+
module Helpers
|
4
|
+
|
5
|
+
# The main accessor to the warden middleware
|
6
|
+
def warden
|
7
|
+
request.env['warden']
|
8
|
+
end
|
9
|
+
|
10
|
+
# Return session info
|
11
|
+
#
|
12
|
+
# @param [Symbol] the scope to retrieve session info for
|
13
|
+
def session_info(scope=nil)
|
14
|
+
scope ? warden.session(scope) : scope
|
15
|
+
end
|
16
|
+
|
17
|
+
# Check the current session is authenticated to a given scope
|
18
|
+
def authenticated?(scope=nil)
|
19
|
+
scope ? warden.authenticated?(scope) : warden.authenticated?
|
20
|
+
end
|
21
|
+
alias_method :logged_in?, :authenticated?
|
22
|
+
|
23
|
+
# Authenticate a user against defined strategies
|
24
|
+
def authenticate(*args)
|
25
|
+
warden.authenticate!(*args)
|
26
|
+
end
|
27
|
+
alias_method :login, :authenticate
|
28
|
+
|
29
|
+
# Terminate the current session
|
30
|
+
#
|
31
|
+
# @param [Symbol] the session scope to terminate
|
32
|
+
def logout(scopes=nil)
|
33
|
+
scopes ? warden.logout(scopes) : warden.logout
|
34
|
+
end
|
35
|
+
|
36
|
+
# Access the user from the current session
|
37
|
+
#
|
38
|
+
# @param [Symbol] the scope for the logged in user
|
39
|
+
def user(scope=nil)
|
40
|
+
scope ? warden.user(scope) : warden.user
|
41
|
+
end
|
42
|
+
alias_method :current_user, :user
|
43
|
+
|
44
|
+
# Store the logged in user in the session
|
45
|
+
#
|
46
|
+
# @param [Object] the user you want to store in the session
|
47
|
+
# @option opts [Symbol] :scope The scope to assign the user
|
48
|
+
# @example Set John as the current user
|
49
|
+
# user = User.find_by_name('John')
|
50
|
+
def user=(new_user, opts={})
|
51
|
+
warden.set_user(new_user, opts)
|
52
|
+
end
|
53
|
+
alias_method :current_user=, :user=
|
54
|
+
|
55
|
+
# Require authorization for an action
|
56
|
+
#
|
57
|
+
# @param [String] path to redirect to if user is unauthenticated
|
58
|
+
def authorize!(failure_path=nil)
|
59
|
+
unless authenticated?
|
60
|
+
session[:return_to] = request.path if options.auth_use_referrer
|
61
|
+
redirect(failure_path ? failure_path : options.auth_failure_path)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.registered(app)
|
68
|
+
app.helpers Warden::Helpers
|
69
|
+
|
70
|
+
# Enable Sessions
|
71
|
+
app.set :sessions, true
|
72
|
+
|
73
|
+
app.set :auth_failure_path, '/'
|
74
|
+
app.set :auth_success_path, '/'
|
75
|
+
# Setting this to true will store last request URL
|
76
|
+
# into a user's session so that to redirect back to it
|
77
|
+
# upon successful authentication
|
78
|
+
app.set :auth_use_referrer, false
|
79
|
+
|
80
|
+
app.set :auth_error_message, "Could not log you in."
|
81
|
+
app.set :auth_success_message, "You have logged in successfully."
|
82
|
+
app.set :auth_use_erb, false
|
83
|
+
app.set :auth_login_template, :login
|
84
|
+
|
85
|
+
# OAuth Specific Settings
|
86
|
+
app.set :auth_use_oauth, false
|
87
|
+
|
88
|
+
app.post '/unauthenticated/?' do
|
89
|
+
status 401
|
90
|
+
env['x-rack.flash'][:error] = options.auth_error_message if defined?(Rack::Flash)
|
91
|
+
options.auth_use_erb ? erb(options.auth_login_template) : haml(options.auth_login_template)
|
92
|
+
end
|
93
|
+
|
94
|
+
app.get '/login/?' do
|
95
|
+
if options.auth_use_oauth && !@auth_oauth_request_token.nil?
|
96
|
+
session[:request_token] = @auth_oauth_request_token.token
|
97
|
+
session[:request_token_secret] = @auth_oauth_request_token.secret
|
98
|
+
redirect @auth_oauth_request_token.authorize_url
|
99
|
+
else
|
100
|
+
options.auth_use_erb ? erb(options.auth_login_template) : haml(options.auth_login_template)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
app.get '/oauth_callback/?' do
|
105
|
+
if options.auth_use_oauth
|
106
|
+
authenticate
|
107
|
+
env['x-rack.flash'][:success] = options.auth_success_message if defined?(Rack::Flash)
|
108
|
+
redirect options.auth_success_path
|
109
|
+
else
|
110
|
+
redirect options.auth_failure_path
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
app.post '/login/?' do
|
115
|
+
authenticate
|
116
|
+
env['x-rack.flash'][:success] = options.auth_success_message if defined?(Rack::Flash)
|
117
|
+
redirect options.auth_use_referrer && session[:return_to] ? session.delete(:return_to) :
|
118
|
+
options.auth_success_path
|
119
|
+
end
|
120
|
+
|
121
|
+
app.get '/logout/?' do
|
122
|
+
authorize!
|
123
|
+
logout
|
124
|
+
env['x-rack.flash'][:success] = options.auth_success_message if defined?(Rack::Flash)
|
125
|
+
redirect options.auth_success_path
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end # Warden
|
129
|
+
|
130
|
+
register Warden
|
131
|
+
end # Sinatra
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'warden'
|
2
|
+
require File.join(File.dirname(__FILE__), 'sinatra_warden', 'sinatra')
|
3
|
+
|
4
|
+
Warden::Manager.before_failure do |env, opts|
|
5
|
+
# Sinatra is very sensitive to the request method
|
6
|
+
# since authentication could fail on any type of method, we need
|
7
|
+
# to set it for the failure app so it is routed to the correct block
|
8
|
+
env['REQUEST_METHOD'] = "POST"
|
9
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
|
4
|
+
Warden::Strategies.add(:password) do
|
5
|
+
def valid?
|
6
|
+
# params['email'] && params['password']
|
7
|
+
# p params
|
8
|
+
true
|
9
|
+
end
|
10
|
+
|
11
|
+
def authenticate!
|
12
|
+
u = User.authenticate(params['email'], params['password'])
|
13
|
+
u.nil? ? fail!("Could not log you in.") : success!(u)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
Warden::Strategies.add(:password) do
|
2
|
+
def valid?
|
3
|
+
# params['email'] && params['password']
|
4
|
+
# p params
|
5
|
+
true
|
6
|
+
end
|
7
|
+
|
8
|
+
def authenticate!
|
9
|
+
u = User.authenticate(params['email'], params['password'])
|
10
|
+
u.nil? ? fail!("Could not log you in.") : success!(u)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class TestingLogin < Sinatra::Base
|
15
|
+
register Sinatra::Warden
|
16
|
+
|
17
|
+
set :views, File.join(File.dirname(__FILE__), 'views')
|
18
|
+
set :sessions, true
|
19
|
+
|
20
|
+
set :auth_success_path, '/welcome'
|
21
|
+
|
22
|
+
get '/dashboard' do
|
23
|
+
authorize!('/login')
|
24
|
+
"My Dashboard"
|
25
|
+
end
|
26
|
+
|
27
|
+
get '/warden' do
|
28
|
+
authorize!
|
29
|
+
"#{warden}"
|
30
|
+
end
|
31
|
+
|
32
|
+
get '/check_login' do
|
33
|
+
logged_in? ? "Hello Moto" : "Get out!"
|
34
|
+
end
|
35
|
+
|
36
|
+
get '/account' do
|
37
|
+
authorize!
|
38
|
+
"#{user.email}'s account page"
|
39
|
+
end
|
40
|
+
|
41
|
+
post '/login_as' do
|
42
|
+
authorize!
|
43
|
+
self.user = User.authenticate(params['email'], params['password'])
|
44
|
+
end
|
45
|
+
|
46
|
+
get '/admin' do
|
47
|
+
authorize!
|
48
|
+
"Welcome #{current_user.email}"
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
class TestingLoginWithReferrer < TestingLogin
|
54
|
+
set :auth_use_referrer, true
|
55
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class User
|
2
|
+
include DataMapper::Resource
|
3
|
+
|
4
|
+
property :id, Serial
|
5
|
+
property :email, String
|
6
|
+
property :password, String
|
7
|
+
|
8
|
+
def self.authenticate(email, password)
|
9
|
+
u = self.first(:email => email)
|
10
|
+
u && u.password == password ? u : nil
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
File without changes
|
@@ -0,0 +1,215 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Sinatra::Warden" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@user = User.create(:email => 'justin.smestad@gmail.com', :password => 'thedude')
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should be a valid user" do
|
10
|
+
@user.new?.should be_false
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should create successfully" do
|
14
|
+
@user.password.should == "thedude"
|
15
|
+
User.authenticate('justin.smestad@gmail.com', 'thedude').should == @user
|
16
|
+
end
|
17
|
+
|
18
|
+
context "the authentication system" do
|
19
|
+
it "should allow us to login as that user" do
|
20
|
+
post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
|
21
|
+
last_request.env['warden'].authenticated?.should == true
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should allow us to logout after logging in" do
|
25
|
+
post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
|
26
|
+
last_request.env['warden'].authenticated?.should == true
|
27
|
+
get '/logout'
|
28
|
+
last_request.env['warden'].authenticated?.should == false
|
29
|
+
end
|
30
|
+
|
31
|
+
context "auth_use_referrer is disabled" do
|
32
|
+
it "should not store :return_to" do
|
33
|
+
get '/dashboard'
|
34
|
+
follow_redirect!
|
35
|
+
last_request.session[:return_to].should be_nil
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should redirect to a default success URL" do
|
39
|
+
get '/dashboard'
|
40
|
+
follow_redirect!
|
41
|
+
post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
|
42
|
+
follow_redirect!
|
43
|
+
last_request.path.should == '/welcome'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "when auth_use_referrer is set to true" do
|
48
|
+
def app
|
49
|
+
Rack::Builder.app do
|
50
|
+
use Rack::Session::Cookie
|
51
|
+
use Warden::Manager do |manager|
|
52
|
+
manager.default_strategies :password
|
53
|
+
manager.failure_app = TestingLogin
|
54
|
+
manager.serialize_into_session { |user| user.id }
|
55
|
+
manager.serialize_from_session { |id| User.get(id) }
|
56
|
+
end
|
57
|
+
use Rack::Flash
|
58
|
+
run TestingLoginWithReferrer
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should store referrer in user's session" do
|
63
|
+
get '/dashboard'
|
64
|
+
follow_redirect!
|
65
|
+
last_request.session[:return_to].should == "/dashboard"
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should redirect to stored return_to URL" do
|
69
|
+
get '/dashboard'
|
70
|
+
follow_redirect!
|
71
|
+
post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
|
72
|
+
follow_redirect!
|
73
|
+
last_request.path.should == '/dashboard'
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should remove :return_to from session" do
|
77
|
+
get '/dashboard'
|
78
|
+
follow_redirect!
|
79
|
+
post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
|
80
|
+
follow_redirect!
|
81
|
+
last_request.session[:return_to].should be_nil
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should default to :auth_success_path if there wasn't a return_to" do
|
85
|
+
post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
|
86
|
+
follow_redirect!
|
87
|
+
last_request.path.should == '/welcome'
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context "the helpers" do
|
93
|
+
|
94
|
+
context "the authorize! helper" do
|
95
|
+
it "should redirect to root (default) if not logged in" do
|
96
|
+
get '/admin'
|
97
|
+
follow_redirect!
|
98
|
+
last_request.url.should == 'http://example.org/'
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should redirect to the passed path if available" do
|
102
|
+
get '/dashboard'
|
103
|
+
follow_redirect!
|
104
|
+
last_request.url.should == 'http://example.org/login'
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should allow access if user is logged in" do
|
108
|
+
post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
|
109
|
+
last_request.env['warden'].authenticated?.should be_true
|
110
|
+
get '/dashboard'
|
111
|
+
last_response.body.should == "My Dashboard"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context "the user helper" do
|
116
|
+
|
117
|
+
before(:each) do
|
118
|
+
post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
|
119
|
+
last_request.env['warden'].authenticated?.should be_true
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should be aliased to current_user" do
|
123
|
+
get '/admin'
|
124
|
+
last_response.body.should == "Welcome #{@user.email}"
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should allow assignment of the user (user=)" do
|
128
|
+
john = User.create(:email => 'john.doe@hotmail.com', :password => 'secret')
|
129
|
+
last_request.env['warden'].user.should == @user
|
130
|
+
post '/login_as', 'email' => 'john.doe@hotmail.com', 'password' => 'secret'
|
131
|
+
last_request.env['warden'].user.should == john
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should return the current logged in user" do
|
135
|
+
get '/account'
|
136
|
+
last_response.body.should == "#{@user.email}'s account page"
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
context "the logged_in/authenticated? helper" do
|
142
|
+
|
143
|
+
before(:each) do
|
144
|
+
post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
|
145
|
+
last_request.env['warden'].authenticated?.should be_true
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should be aliased as logged_in?" do
|
149
|
+
get '/check_login'
|
150
|
+
last_response.body.should == "Hello Moto"
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should return false when a user is not authenticated" do
|
154
|
+
get '/logout'
|
155
|
+
last_request.env['warden'].authenticated?.should be_false
|
156
|
+
|
157
|
+
get '/check_login'
|
158
|
+
last_response.body.should == "Get out!"
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
context "the warden helper" do
|
164
|
+
|
165
|
+
before(:each) do
|
166
|
+
post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
|
167
|
+
last_request.env['warden'].authenticated?.should be_true
|
168
|
+
end
|
169
|
+
|
170
|
+
it "returns the environment variables from warden" do
|
171
|
+
get '/warden'
|
172
|
+
last_response.body.should_not be_nil
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
context "Rack::Flash integration" do
|
179
|
+
|
180
|
+
it "should return a success message" do
|
181
|
+
post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
|
182
|
+
last_request.env['x-rack.flash'][:success].should == "You have logged in successfully."
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should return an error message" do
|
186
|
+
post '/login', 'email' => 'bad', 'password' => 'wrong'
|
187
|
+
last_request.env['x-rack.flash'][:error].should == "Could not log you in."
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
191
|
+
|
192
|
+
context "OAuth support" do
|
193
|
+
context "when enabled" do
|
194
|
+
before do
|
195
|
+
pending
|
196
|
+
#TestingLogin.set(:auth_use_oauth, true)
|
197
|
+
#@app = app
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should redirect to authorize_url" do
|
201
|
+
get '/login'
|
202
|
+
follow_redirect!
|
203
|
+
last_request.url.should == "http://twitter.com/oauth/authorize"
|
204
|
+
end
|
205
|
+
|
206
|
+
it "should redirect to a custom authorize_url, if set" do
|
207
|
+
get '/login'
|
208
|
+
follow_redirect!
|
209
|
+
last_request.url.should == "http://facebook.com"
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
-fs --color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
Bundler.require(:default, :runtime, :test)
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
+
|
5
|
+
ENV['RACK_ENV'] ||= 'test'
|
6
|
+
|
7
|
+
require 'sinatra_warden'
|
8
|
+
require 'spec'
|
9
|
+
require 'spec/autorun'
|
10
|
+
|
11
|
+
DataMapper.setup(:default, 'sqlite3::memory:')
|
12
|
+
|
13
|
+
%w(fixtures support).each do |path|
|
14
|
+
Dir[ File.join(File.dirname(__FILE__), path, '/**/*.rb') ].each do |m|
|
15
|
+
require m
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Spec::Runner.configure do |config|
|
20
|
+
config.include(Rack::Test::Methods)
|
21
|
+
|
22
|
+
config.before(:each) do
|
23
|
+
DataMapper.auto_migrate!
|
24
|
+
end
|
25
|
+
|
26
|
+
def app
|
27
|
+
@app ||= Rack::Builder.app do
|
28
|
+
use Rack::Session::Cookie
|
29
|
+
use Warden::Manager do |manager|
|
30
|
+
manager.default_strategies :password
|
31
|
+
manager.failure_app = TestingLogin
|
32
|
+
manager.serialize_into_session { |user| user.id }
|
33
|
+
manager.serialize_from_session { |id| User.get(id) }
|
34
|
+
end
|
35
|
+
use Rack::Flash
|
36
|
+
run TestingLogin
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: alex-sinatra_warden
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 3
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.3.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Justin Smestad
|
14
|
+
- Daniel Neighman
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-04-24 00:00:00 +02:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 9
|
30
|
+
- 4
|
31
|
+
version: 0.9.4
|
32
|
+
prerelease: false
|
33
|
+
type: :runtime
|
34
|
+
name: sinatra
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
- 10
|
44
|
+
- 3
|
45
|
+
version: 0.10.3
|
46
|
+
prerelease: false
|
47
|
+
type: :runtime
|
48
|
+
name: warden
|
49
|
+
version_requirements: *id002
|
50
|
+
description: basic helpers and authentication methods for using warden with sinatra also providing some hooks into Rack::Flash
|
51
|
+
email: justin.smestad@gmail.com
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files:
|
57
|
+
- LICENSE
|
58
|
+
- README.rdoc
|
59
|
+
files:
|
60
|
+
- .document
|
61
|
+
- .gitignore
|
62
|
+
- Gemfile
|
63
|
+
- LICENSE
|
64
|
+
- README.rdoc
|
65
|
+
- Rakefile
|
66
|
+
- VERSION
|
67
|
+
- alex-sinatra_warden.gemspec
|
68
|
+
- lib/sinatra_warden.rb
|
69
|
+
- lib/sinatra_warden/sinatra.rb
|
70
|
+
- spec/fixtures/basic_strategy.rb
|
71
|
+
- spec/fixtures/testing_login.rb
|
72
|
+
- spec/fixtures/user.rb
|
73
|
+
- spec/fixtures/views/login.haml
|
74
|
+
- spec/sinatra_warden_spec.rb
|
75
|
+
- spec/spec.opts
|
76
|
+
- spec/spec_helper.rb
|
77
|
+
has_rdoc: true
|
78
|
+
homepage: http://github.com/crhym3/sinatra_warden
|
79
|
+
licenses: []
|
80
|
+
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options:
|
83
|
+
- --charset=UTF-8
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
segments:
|
98
|
+
- 0
|
99
|
+
version: "0"
|
100
|
+
requirements: []
|
101
|
+
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 1.3.6
|
104
|
+
signing_key:
|
105
|
+
specification_version: 3
|
106
|
+
summary: authentication system for using warden with sinatra
|
107
|
+
test_files:
|
108
|
+
- spec/fixtures/basic_strategy.rb
|
109
|
+
- spec/fixtures/testing_login.rb
|
110
|
+
- spec/fixtures/user.rb
|
111
|
+
- spec/sinatra_warden_spec.rb
|
112
|
+
- spec/spec_helper.rb
|