sinatra-session-auth 0.1.0 → 0.1.1
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 +1 -0
- data/Rakefile +8 -0
- data/VERSION +1 -1
- data/lib/sinatra/session_auth.rb +6 -44
- data/sinatra-session-auth.gemspec +52 -0
- data/test/app/app.rb +79 -0
- data/test/app_test.rb +38 -0
- data/test/test_helper.rb +43 -0
- metadata +10 -4
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
|
+
require "rake/testtask"
|
4
|
+
|
5
|
+
task :default => ["test"]
|
3
6
|
|
4
7
|
begin
|
5
8
|
require 'jeweler'
|
@@ -16,3 +19,8 @@ rescue LoadError
|
|
16
19
|
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
17
20
|
end
|
18
21
|
|
22
|
+
Rake::TestTask.new do |t|
|
23
|
+
t.libs << "test"
|
24
|
+
t.test_files = FileList['test/*_test.rb']
|
25
|
+
t.verbose = true
|
26
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/sinatra/session_auth.rb
CHANGED
@@ -3,17 +3,17 @@ require 'digest/sha1'
|
|
3
3
|
|
4
4
|
module Sinatra
|
5
5
|
module SessionAuth
|
6
|
-
module
|
6
|
+
module ModelHelpers
|
7
7
|
def self.included(klass)
|
8
|
-
klass.send
|
9
|
-
klass.send
|
8
|
+
klass.send :include, InstanceMethods
|
9
|
+
klass.send :extend, ClassMethods
|
10
10
|
end
|
11
11
|
|
12
12
|
module InstanceMethods
|
13
13
|
def password=(pass)
|
14
14
|
@password = pass
|
15
|
-
self.salt =
|
16
|
-
self.hashed_password =
|
15
|
+
self.salt = self.class.random_string(10) unless self.salt
|
16
|
+
self.hashed_password = self.class.encrypt(@password, self.salt)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
@@ -45,10 +45,7 @@ module Sinatra
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def authorize!
|
48
|
-
unless authorized?
|
49
|
-
flash[:notice] = 'You must be logged in to view this page.'
|
50
|
-
redirect '/login'
|
51
|
-
end
|
48
|
+
redirect '/protected/login' unless authorized?
|
52
49
|
end
|
53
50
|
|
54
51
|
def logout!
|
@@ -58,41 +55,6 @@ module Sinatra
|
|
58
55
|
|
59
56
|
def self.registered(app)
|
60
57
|
app.helpers SessionAuth::Helpers
|
61
|
-
app.set :views, "/views"
|
62
|
-
app.get '/login' do
|
63
|
-
erb :login
|
64
|
-
end
|
65
|
-
|
66
|
-
app.post '/login' do
|
67
|
-
if session[:user] = User.authenticate(params[:user])
|
68
|
-
flash[:notice] = "Login succesful"
|
69
|
-
redirect '/'
|
70
|
-
else
|
71
|
-
flash[:notice] = "Login failed - Try again"
|
72
|
-
redirect '/login'
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
app.get '/logout' do
|
77
|
-
logout!
|
78
|
-
flash[:notice] = "Logged out"
|
79
|
-
redirect '/'
|
80
|
-
end
|
81
|
-
|
82
|
-
app.get "/signup" do
|
83
|
-
erb :signup
|
84
|
-
end
|
85
|
-
|
86
|
-
app.post "/signup" do
|
87
|
-
if user = User.create(params[:user])
|
88
|
-
session[:user] = user
|
89
|
-
flash[:notice] = "Your account was succesfully created"
|
90
|
-
redirect '/'
|
91
|
-
else
|
92
|
-
flash[:notice] = "Signup failed - Try again"
|
93
|
-
redirect '/signup'
|
94
|
-
end
|
95
|
-
end
|
96
58
|
end
|
97
59
|
end
|
98
60
|
|
@@ -0,0 +1,52 @@
|
|
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{sinatra-session-auth}
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Patrik Hedman"]
|
12
|
+
s.date = %q{2009-11-25}
|
13
|
+
s.description = %q{sinatra-session-auth is an extension for Sinatra to add orm-agnostic session based user authorization}
|
14
|
+
s.email = %q{patrik@moresale.se}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README.md",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"lib/sinatra/session_auth.rb",
|
26
|
+
"sinatra-session-auth.gemspec",
|
27
|
+
"test/app/app.rb",
|
28
|
+
"test/app_test.rb",
|
29
|
+
"test/test_helper.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/polly/sinatra-session-auth}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.5}
|
35
|
+
s.summary = %q{A orm-agnostic extension to add session based user authorization}
|
36
|
+
s.test_files = [
|
37
|
+
"test/app/app.rb",
|
38
|
+
"test/app_test.rb",
|
39
|
+
"test/test_helper.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
+
else
|
48
|
+
end
|
49
|
+
else
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
data/test/app/app.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
$:.unshift File.join('..', 'lib', 'sinatra')
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'dm-core'
|
5
|
+
require 'sinatra'
|
6
|
+
require 'rack-flash'
|
7
|
+
require 'sinatra/session_auth'
|
8
|
+
|
9
|
+
DataMapper.setup(:default, 'sqlite3::memory:')
|
10
|
+
|
11
|
+
class User
|
12
|
+
include DataMapper::Resource
|
13
|
+
include Sinatra::SessionAuth::ModelHelpers
|
14
|
+
|
15
|
+
property :id, Serial
|
16
|
+
property :login, String
|
17
|
+
property :salt, String
|
18
|
+
property :hashed_password, String
|
19
|
+
end
|
20
|
+
|
21
|
+
User.auto_migrate!
|
22
|
+
|
23
|
+
use Rack::Session::Cookie
|
24
|
+
use Rack::Flash
|
25
|
+
|
26
|
+
get "/" do
|
27
|
+
erb "<%= flash[:notice] %><br />Public"
|
28
|
+
end
|
29
|
+
|
30
|
+
get "/protected" do
|
31
|
+
flash[:notice] = 'You must be logged in to view this page.'
|
32
|
+
authorize!
|
33
|
+
erb "<%= flash[:notice] %><br />Protected"
|
34
|
+
end
|
35
|
+
|
36
|
+
get '/protected/login' do
|
37
|
+
'<form action="/protected/login" method="post">
|
38
|
+
<label for="login">Login</label><input id="login" type="text" size="30" name="user[login]"/>
|
39
|
+
<label for="password">Password</label><input id="password" type="password" size="30" name="user[password]"/>
|
40
|
+
<br/>
|
41
|
+
<input type="submit" value="Submit" name="submit"/>
|
42
|
+
</form>'
|
43
|
+
end
|
44
|
+
|
45
|
+
post '/protected/login' do
|
46
|
+
if session[:user] = User.authenticate(params[:user])
|
47
|
+
flash[:notice] = "Login succesful"
|
48
|
+
redirect '/'
|
49
|
+
else
|
50
|
+
flash[:notice] = "Login failed, try again"
|
51
|
+
redirect '/login'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
get '/protected/signup' do
|
56
|
+
'<form action="/protected/signup" method="post">
|
57
|
+
<label for="login">Login</label><input id="login" type="text" size="30" name="user[login]"/>
|
58
|
+
<label for="password">Password</label><input id="password" type="password" size="30" name="user[password]"/>
|
59
|
+
<br/>
|
60
|
+
<input type="submit" value="Submit" name="submit"/>
|
61
|
+
</form>'
|
62
|
+
end
|
63
|
+
|
64
|
+
post '/protected/signup' do
|
65
|
+
if session[:user] = User.new(params[:user])
|
66
|
+
flash[:notice] = "Your account has been created"
|
67
|
+
redirect '/'
|
68
|
+
else
|
69
|
+
flash[:notice] = "Signup failed, try again"
|
70
|
+
redirect '/login'
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
get '/protected/logout' do
|
75
|
+
logout!
|
76
|
+
flash[:notice] = "Logged out"
|
77
|
+
redirect '/'
|
78
|
+
end
|
79
|
+
|
data/test/app_test.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class AppTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "GET /" do
|
6
|
+
should "not require authentication" do
|
7
|
+
visit "/"
|
8
|
+
assert_contain "Public"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
context "GET /protected" do
|
13
|
+
should "require authentication" do
|
14
|
+
visit "/protected"
|
15
|
+
assert_equal "http://example.org/protected/login", last_request.url
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "A visitor" do
|
20
|
+
should "be able to signup for an account" do
|
21
|
+
visit "/protected/signup"
|
22
|
+
fill_in "Login", :with => "pmh"
|
23
|
+
fill_in "Password", :with => "1234"
|
24
|
+
click_button "Submit"
|
25
|
+
assert_contain "Your account has been created"
|
26
|
+
end
|
27
|
+
|
28
|
+
should "be able to login" do
|
29
|
+
user = User.create(:login => "pmh", :password => "1234")
|
30
|
+
|
31
|
+
visit "/protected/login"
|
32
|
+
fill_in "Login", :with => "pmh"
|
33
|
+
fill_in "Password", :with => "1234"
|
34
|
+
click_button "Submit"
|
35
|
+
assert_contain "Login succesful"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
ENV['RACK_ENV'] = "test"
|
2
|
+
|
3
|
+
require 'app/app'
|
4
|
+
require 'rack/test'
|
5
|
+
require 'webrat'
|
6
|
+
|
7
|
+
Sinatra::Application.set(
|
8
|
+
:environment => :test,
|
9
|
+
:run => false,
|
10
|
+
:raise_errors => true,
|
11
|
+
:logging => false
|
12
|
+
)
|
13
|
+
|
14
|
+
Webrat.configure do |config|
|
15
|
+
config.mode = :rack
|
16
|
+
config.application_port = 4567
|
17
|
+
end
|
18
|
+
|
19
|
+
module TestHelper
|
20
|
+
|
21
|
+
def app
|
22
|
+
# change to your app class if using the 'classy' style
|
23
|
+
# Sinatra::Application.new
|
24
|
+
Sinatra::Application.new
|
25
|
+
end
|
26
|
+
|
27
|
+
def body
|
28
|
+
last_response.body
|
29
|
+
end
|
30
|
+
|
31
|
+
def status
|
32
|
+
last_response.status
|
33
|
+
end
|
34
|
+
|
35
|
+
include Rack::Test::Methods
|
36
|
+
include Webrat::Methods
|
37
|
+
include Webrat::Matchers
|
38
|
+
end
|
39
|
+
|
40
|
+
require 'test/unit'
|
41
|
+
require 'shoulda'
|
42
|
+
|
43
|
+
Test::Unit::TestCase.send(:include, TestHelper)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-session-auth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrik Hedman
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-25 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -29,6 +29,10 @@ files:
|
|
29
29
|
- Rakefile
|
30
30
|
- VERSION
|
31
31
|
- lib/sinatra/session_auth.rb
|
32
|
+
- sinatra-session-auth.gemspec
|
33
|
+
- test/app/app.rb
|
34
|
+
- test/app_test.rb
|
35
|
+
- test/test_helper.rb
|
32
36
|
has_rdoc: true
|
33
37
|
homepage: http://github.com/polly/sinatra-session-auth
|
34
38
|
licenses: []
|
@@ -57,5 +61,7 @@ rubygems_version: 1.3.5
|
|
57
61
|
signing_key:
|
58
62
|
specification_version: 3
|
59
63
|
summary: A orm-agnostic extension to add session based user authorization
|
60
|
-
test_files:
|
61
|
-
|
64
|
+
test_files:
|
65
|
+
- test/app/app.rb
|
66
|
+
- test/app_test.rb
|
67
|
+
- test/test_helper.rb
|