Skip to content

Commit 7801d4f

Browse files
committed
serialize sessions
1 parent 5c8401a commit 7801d4f

8 files changed

+42
-10
lines changed

Gemfile.lock

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ PATH
22
remote: .
33
specs:
44
roda-auth (0.0.1)
5+
bcrypt (~> 3)
56
roda (~> 1.2)
67
warden (~> 1.2)
78

89
GEM
910
remote: https://rubygems.org/
1011
specs:
12+
bcrypt (3.1.9)
1113
minitest (5.5.0)
1214
rack (1.6.0)
1315
rack-test (0.6.2)
1416
rack (>= 1.0)
1517
rake (10.4.2)
16-
roda (1.2.0)
18+
roda (1.3.0)
1719
rack
1820
warden (1.2.3)
1921
rack (>= 1.0)

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Roda plugin for Authentication
22
=============
33

4+
### Status
5+
6+
This is a first stab at integrating Roda and Warden. It is by no means ready for real use.
7+
48
### Quick start
59

610
Install gem with
@@ -59,6 +63,12 @@ class MyUser
5963
end
6064
end
6165

66+
#required when using :form strategy (for sessions)
67+
68+
def self.find_by_id(id)
69+
find(id)
70+
end
71+
6272
#optional - used for generating/updating auth tokens or tracking logins
6373

6474
def authentic!

Rakefile

+8
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@ Rake::TestTask.new do |t|
44
t.libs << "test"
55
t.pattern = "test/*_test.rb"
66
end
7+
8+
desc 'individual test'
9+
task :one, [:file] do |_, f|
10+
Rake::TestTask.new do |t|
11+
t.libs << "test"
12+
t.test_files = [f[:file]]
13+
end
14+
end

lib/roda/plugins/auth.rb

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ def self.configure(app, *args)
2828
when :form
2929
strategies = [:password]
3030
app.use Rack::Session::Cookie, options.delete(:cookie)
31+
Warden::Manager.serialize_into_session do |user|
32+
user.id
33+
end
34+
Warden::Manager.serialize_from_session do |id|
35+
user_class.find_by_id(id)
36+
end
3137
when :token
3238
strategies = [:token, :password]
3339
end

test/auth_basic_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def setup
3030
def test_public
3131
assert_equal 200, status('/public')
3232
end
33-
33+
3434
def test_private_refused
3535
assert_equal 401, status('/private')
3636
assert_equal "Basic realm=\"/private\"", header('WWW-AUTHENTICATE', '/private')

test/auth_form_test.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ def setup
1313

1414
app :bare do |app|
1515

16-
app.plugin :auth, :form, redirect: '/login'
16+
app.plugin :auth, :form, redirect: '/login', cookie: {secret:'foo'}
1717

1818
app.route do |r|
19-
r.post('login') { sign_in }
19+
r.post('login') { sign_in ? 'ok' : nil }
2020
r.get('login') { 'LOGIN FORM' }
2121
r.post('logout') { sign_out }
2222
r.on 'public' do
@@ -33,21 +33,21 @@ def setup
3333
def test_public
3434
assert_equal 200, status('/public')
3535
end
36-
36+
3737
def test_private_refuse_redirect
3838
r = req('/private')
3939
assert_equal 302, r[0]
4040
assert_equal "/login", r[1]['LOCATION']
4141
end
4242

4343
def test_private_accepted
44-
post('/signout')
44+
post('/logout')
4545
cookie = login
4646
assert_equal 200, status('/private', {'HTTP_COOKIE' => cookie})
4747
end
4848

4949
def test_private_error
50-
req('/signout')
50+
req('/logout')
5151
cookie = login(invalid_credentials)
5252
assert_equal 302, status('/private', {'HTTP_COOKIE' => cookie})
5353
end
@@ -58,7 +58,7 @@ def test_private_error
5858

5959
def login(cred = valid_credentials)
6060
r = req('/login', {'REQUEST_METHOD' => 'POST', 'rack.input' => save_args(cred)})
61-
r[0] == 201 && r[1]["Set-Cookie"]
61+
r[0] == 200 && r[1]["Set-Cookie"]
6262
end
6363

6464

test/auth_token_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_protected_error
4040
end
4141

4242
def test_login_body
43-
assert_equal 201, status('/session', {'REQUEST_METHOD' => 'POST', 'rack.input' => save_args(valid_credentials)})
43+
assert_equal 200, status('/session', {'REQUEST_METHOD' => 'POST', 'rack.input' => save_args(valid_credentials)})
4444
end
4545

4646
def test_login_body_invalid

test/test_helpers.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def authentic!
148148
end
149149

150150
## test dummy
151-
151+
152152
def initialize(args)
153153
@username = args[:username]
154154
@password = args[:password]
@@ -159,6 +159,12 @@ def initialize(args)
159159
def self.db
160160
@@db ||= {users: {}, tokens: {}}
161161
end
162+
163+
def self.find_by_id(id)
164+
db[:users].values.find do |u|
165+
u.id == id
166+
end
167+
end
162168

163169
def to_json(state = nil)
164170
{id: @id, username: @username, token: @token}.to_json(state)

0 commit comments

Comments
 (0)