sinatra_warden 0.2.0.2 → 0.3.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/Gemfile +4 -4
- data/VERSION +1 -1
- data/lib/sinatra_warden/sinatra.rb +46 -21
- data/sinatra_warden.gemspec +5 -5
- data/spec/fixtures/basic_strategy.rb +6 -1
- data/spec/fixtures/testing_login.rb +14 -0
- data/spec/fixtures/user.rb +0 -1
- data/spec/sinatra_warden_spec.rb +8 -5
- data/spec/spec_helper.rb +3 -1
- metadata +14 -15
data/Gemfile
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
source
|
1
|
+
source 'http://rubygems.org'
|
2
2
|
|
3
3
|
group :runtime do
|
4
4
|
gem 'sinatra', '>= 0.9.4'
|
5
|
-
gem 'warden', '>= 0.
|
5
|
+
gem 'warden', '>= 0.10.3'
|
6
6
|
end
|
7
7
|
|
8
8
|
group :test do
|
@@ -10,7 +10,7 @@ group :test do
|
|
10
10
|
gem 'jeweler', '~> 1.3.0'
|
11
11
|
gem 'bundler', '~> 0.9.7'
|
12
12
|
gem 'rspec', '~> 1.2.9', :require => 'spec'
|
13
|
-
gem 'yard'
|
13
|
+
gem 'yard', '>= 0.5.4'
|
14
14
|
gem 'rack-test', '~> 0.5.0', :require => 'rack/test'
|
15
15
|
gem 'rcov'
|
16
16
|
|
@@ -18,5 +18,5 @@ group :test do
|
|
18
18
|
gem 'dm-core', '~> 0.10.1'
|
19
19
|
gem 'bcrypt-ruby', :require => 'bcrypt'
|
20
20
|
gem 'haml'
|
21
|
-
gem 'rack-flash', :require => 'rack
|
21
|
+
gem 'rack-flash', :require => 'rack-flash'
|
22
22
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
@@ -2,33 +2,58 @@ module Sinatra
|
|
2
2
|
module Warden
|
3
3
|
module Helpers
|
4
4
|
|
5
|
-
# The main accessor
|
5
|
+
# The main accessor to the warden middleware
|
6
6
|
def warden
|
7
7
|
request.env['warden']
|
8
8
|
end
|
9
9
|
|
10
|
-
#
|
11
|
-
|
12
|
-
|
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?
|
13
20
|
end
|
14
21
|
alias_method :logged_in?, :authenticated?
|
15
22
|
|
16
|
-
#
|
17
|
-
def
|
18
|
-
warden.
|
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
|
19
41
|
end
|
20
42
|
alias_method :current_user, :user
|
21
43
|
|
22
|
-
#
|
23
|
-
# Usage: self.user = @user
|
44
|
+
# Store the logged in user in the session
|
24
45
|
#
|
25
|
-
# @param [
|
26
|
-
|
27
|
-
|
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)
|
28
52
|
end
|
29
53
|
alias_method :current_user=, :user=
|
30
54
|
|
31
55
|
# Require authorization for an action
|
56
|
+
#
|
32
57
|
# @param [String] path to redirect to if user is unauthenticated
|
33
58
|
def authorize!(failure_path=nil)
|
34
59
|
redirect(failure_path ? failure_path : options.auth_failure_path) unless authenticated?
|
@@ -49,13 +74,13 @@ module Sinatra
|
|
49
74
|
app.set :auth_success_message, "You have logged in successfully."
|
50
75
|
app.set :auth_use_erb, false
|
51
76
|
app.set :auth_login_template, :login
|
52
|
-
|
77
|
+
|
53
78
|
# OAuth Specific Settings
|
54
79
|
app.set :auth_use_oauth, false
|
55
80
|
|
56
81
|
app.post '/unauthenticated/?' do
|
57
82
|
status 401
|
58
|
-
flash[:error] =
|
83
|
+
env['x-rack.flash'][:error] = options.auth_error_message if defined?(Rack::Flash)
|
59
84
|
options.auth_use_erb ? erb(options.auth_login_template) : haml(options.auth_login_template)
|
60
85
|
end
|
61
86
|
|
@@ -64,15 +89,15 @@ module Sinatra
|
|
64
89
|
session[:request_token] = @auth_oauth_request_token.token
|
65
90
|
session[:request_token_secret] = @auth_oauth_request_token.secret
|
66
91
|
redirect @auth_oauth_request_token.authorize_url
|
67
|
-
else
|
92
|
+
else
|
68
93
|
options.auth_use_erb ? erb(options.auth_login_template) : haml(options.auth_login_template)
|
69
94
|
end
|
70
95
|
end
|
71
96
|
|
72
97
|
app.get '/oauth_callback/?' do
|
73
98
|
if options.auth_use_oauth
|
74
|
-
|
75
|
-
flash[:success] = options.auth_success_message if defined?(Rack::Flash)
|
99
|
+
authenticate
|
100
|
+
env['x-rack.flash'][:success] = options.auth_success_message if defined?(Rack::Flash)
|
76
101
|
redirect options.auth_success_path
|
77
102
|
else
|
78
103
|
redirect options.auth_failure_path
|
@@ -80,15 +105,15 @@ module Sinatra
|
|
80
105
|
end
|
81
106
|
|
82
107
|
app.post '/login/?' do
|
83
|
-
|
84
|
-
flash[:success] = options.auth_success_message if defined?(Rack::Flash)
|
108
|
+
authenticate
|
109
|
+
env['x-rack.flash'][:success] = options.auth_success_message if defined?(Rack::Flash)
|
85
110
|
redirect options.auth_success_path
|
86
111
|
end
|
87
112
|
|
88
113
|
app.get '/logout/?' do
|
89
114
|
authorize!
|
90
|
-
|
91
|
-
flash[:success] = options.auth_success_message if defined?(Rack::Flash)
|
115
|
+
logout
|
116
|
+
env['x-rack.flash'][:success] = options.auth_success_message if defined?(Rack::Flash)
|
92
117
|
redirect options.auth_success_path
|
93
118
|
end
|
94
119
|
end
|
data/sinatra_warden.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sinatra_warden}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Justin Smestad", "Daniel Neighman"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-04-23}
|
13
13
|
s.description = %q{basic helpers and authentication methods for using warden with sinatra also providing some hooks into Rack::Flash}
|
14
14
|
s.email = %q{justin.smestad@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -54,14 +54,14 @@ Gem::Specification.new do |s|
|
|
54
54
|
|
55
55
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
56
56
|
s.add_runtime_dependency(%q<sinatra>, [">= 0.9.4"])
|
57
|
-
s.add_runtime_dependency(%q<warden>, [">= 0.
|
57
|
+
s.add_runtime_dependency(%q<warden>, [">= 0.10.3"])
|
58
58
|
else
|
59
59
|
s.add_dependency(%q<sinatra>, [">= 0.9.4"])
|
60
|
-
s.add_dependency(%q<warden>, [">= 0.
|
60
|
+
s.add_dependency(%q<warden>, [">= 0.10.3"])
|
61
61
|
end
|
62
62
|
else
|
63
63
|
s.add_dependency(%q<sinatra>, [">= 0.9.4"])
|
64
|
-
s.add_dependency(%q<warden>, [">= 0.
|
64
|
+
s.add_dependency(%q<warden>, [">= 0.10.3"])
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
@@ -1,3 +1,16 @@
|
|
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
|
+
|
1
14
|
class TestingLogin < Sinatra::Base
|
2
15
|
register Sinatra::Warden
|
3
16
|
|
@@ -5,6 +18,7 @@ class TestingLogin < Sinatra::Base
|
|
5
18
|
set :sessions, true
|
6
19
|
|
7
20
|
set :auth_success_path, '/welcome'
|
21
|
+
|
8
22
|
get '/dashboard' do
|
9
23
|
authorize!('/login')
|
10
24
|
"My Dashboard"
|
data/spec/fixtures/user.rb
CHANGED
data/spec/sinatra_warden_spec.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe "Sinatra::Warden" do
|
4
|
+
|
4
5
|
before(:each) do
|
5
6
|
@user = User.create(:email => 'justin.smestad@gmail.com', :password => 'thedude')
|
6
7
|
end
|
@@ -52,6 +53,7 @@ describe "Sinatra::Warden" do
|
|
52
53
|
end
|
53
54
|
|
54
55
|
context "the user helper" do
|
56
|
+
|
55
57
|
before(:each) do
|
56
58
|
post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
|
57
59
|
last_request.env['warden'].authenticated?.should be_true
|
@@ -77,6 +79,7 @@ describe "Sinatra::Warden" do
|
|
77
79
|
end
|
78
80
|
|
79
81
|
context "the logged_in/authenticated? helper" do
|
82
|
+
|
80
83
|
before(:each) do
|
81
84
|
post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
|
82
85
|
last_request.env['warden'].authenticated?.should be_true
|
@@ -98,6 +101,7 @@ describe "Sinatra::Warden" do
|
|
98
101
|
end
|
99
102
|
|
100
103
|
context "the warden helper" do
|
104
|
+
|
101
105
|
before(:each) do
|
102
106
|
post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
|
103
107
|
last_request.env['warden'].authenticated?.should be_true
|
@@ -115,32 +119,31 @@ describe "Sinatra::Warden" do
|
|
115
119
|
|
116
120
|
it "should return a success message" do
|
117
121
|
post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
|
118
|
-
last_request.env['rack.
|
122
|
+
last_request.env['x-rack.flash'][:success].should == "You have logged in successfully."
|
119
123
|
end
|
120
124
|
|
121
125
|
it "should return an error message" do
|
122
126
|
post '/login', 'email' => 'bad', 'password' => 'wrong'
|
123
|
-
last_request.env['rack.
|
127
|
+
last_request.env['x-rack.flash'][:error].should == "Could not log you in."
|
124
128
|
end
|
125
129
|
|
126
130
|
end
|
127
131
|
|
128
132
|
context "OAuth support" do
|
129
133
|
context "when enabled" do
|
130
|
-
before
|
134
|
+
before do
|
135
|
+
pending
|
131
136
|
#TestingLogin.set(:auth_use_oauth, true)
|
132
137
|
#@app = app
|
133
138
|
end
|
134
139
|
|
135
140
|
it "should redirect to authorize_url" do
|
136
|
-
pending
|
137
141
|
get '/login'
|
138
142
|
follow_redirect!
|
139
143
|
last_request.url.should == "http://twitter.com/oauth/authorize"
|
140
144
|
end
|
141
145
|
|
142
146
|
it "should redirect to a custom authorize_url, if set" do
|
143
|
-
pending
|
144
147
|
get '/login'
|
145
148
|
follow_redirect!
|
146
149
|
last_request.url.should == "http://facebook.com"
|
data/spec/spec_helper.rb
CHANGED
@@ -11,7 +11,7 @@ require 'spec/autorun'
|
|
11
11
|
DataMapper.setup(:default, 'sqlite3::memory:')
|
12
12
|
|
13
13
|
%w(fixtures support).each do |path|
|
14
|
-
Dir[ File.join(
|
14
|
+
Dir[ File.join(File.dirname(__FILE__), path, '/**/*.rb') ].each do |m|
|
15
15
|
require m
|
16
16
|
end
|
17
17
|
end
|
@@ -29,6 +29,8 @@ Spec::Runner.configure do |config|
|
|
29
29
|
use Warden::Manager do |manager|
|
30
30
|
manager.default_strategies :password
|
31
31
|
manager.failure_app = TestingLogin
|
32
|
+
manager.serialize_into_session { |user| user.id }
|
33
|
+
manager.serialize_from_session { |id| User.get(id) }
|
32
34
|
end
|
33
35
|
use Rack::Flash
|
34
36
|
run TestingLogin
|
metadata
CHANGED
@@ -4,10 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 3
|
8
8
|
- 0
|
9
|
-
|
10
|
-
version: 0.2.0.2
|
9
|
+
version: 0.3.0
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Justin Smestad
|
@@ -16,13 +15,13 @@ autorequire:
|
|
16
15
|
bindir: bin
|
17
16
|
cert_chain: []
|
18
17
|
|
19
|
-
date: 2010-
|
18
|
+
date: 2010-04-23 00:00:00 -06:00
|
20
19
|
default_executable:
|
21
20
|
dependencies:
|
22
21
|
- !ruby/object:Gem::Dependency
|
22
|
+
type: :runtime
|
23
23
|
name: sinatra
|
24
|
-
|
25
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
25
|
requirements:
|
27
26
|
- - ">="
|
28
27
|
- !ruby/object:Gem::Version
|
@@ -31,22 +30,22 @@ dependencies:
|
|
31
30
|
- 9
|
32
31
|
- 4
|
33
32
|
version: 0.9.4
|
34
|
-
|
35
|
-
|
33
|
+
requirement: *id001
|
34
|
+
prerelease: false
|
36
35
|
- !ruby/object:Gem::Dependency
|
36
|
+
type: :runtime
|
37
37
|
name: warden
|
38
|
-
|
39
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
39
|
requirements:
|
41
40
|
- - ">="
|
42
41
|
- !ruby/object:Gem::Version
|
43
42
|
segments:
|
44
43
|
- 0
|
45
|
-
-
|
46
|
-
-
|
47
|
-
version: 0.
|
48
|
-
|
49
|
-
|
44
|
+
- 10
|
45
|
+
- 3
|
46
|
+
version: 0.10.3
|
47
|
+
requirement: *id002
|
48
|
+
prerelease: false
|
50
49
|
description: basic helpers and authentication methods for using warden with sinatra also providing some hooks into Rack::Flash
|
51
50
|
email: justin.smestad@gmail.com
|
52
51
|
executables: []
|