sinatra-simple-auth 0.1.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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2009 Vasily Polovnyov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ ##Sinatra SimpleAuth Extension
2
+
3
+ Extends Sinatra with a extension methods and routes for dealing with a simple, single-password authorization
4
+
5
+ ##Installation
6
+
7
+ sudo gem install sinatra-simple-auth -s http://gemcutter.org/
8
+
9
+ ##Usage
10
+
11
+ require 'rubygems'
12
+ require 'sinatra'
13
+ require 'sinatra/simple_auth'
14
+
15
+ enable :sessions
16
+ set :password, 'my_cool_password' #define single password
17
+ set :home, '/secure/' #where user should be redirected after successful authorization
18
+
19
+ get '/a/?' do
20
+ erb :auth #page with logon form
21
+ end
22
+
23
+ get '/secure/' do
24
+ protected! #protected route, requires auth
25
+ erb :secure
26
+ end
27
+
28
+ get '/' do
29
+ if authorized? #helper method
30
+ "Hello, %username%"
31
+ else
32
+ "Not authorized"
33
+ end
34
+ end
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ require 'rubygems'
2
+ require 'rake/testtask'
3
+ require 'rake/gempackagetask'
4
+
5
+ task :default => :test
6
+
7
+ begin
8
+ require 'jeweler'
9
+ Jeweler::Tasks.new do |gemspec|
10
+ gemspec.name = "sinatra-simple-auth"
11
+ gemspec.version = "0.1.0"
12
+ gemspec.summary = gemspec.description = "super simple auth extension for Sinatra"
13
+ gemspec.email = "vasily@polovnyov.ru"
14
+ gemspec.homepage = "http://github.com/vast/sinatra-simple-auth"
15
+ gemspec.authors = ["Vasily Polovnyov"]
16
+
17
+ gemspec.add_dependency 'sinatra', '>=1.0.0'
18
+ gemspec.add_dependency 'rack', '>=0.9.1'
19
+
20
+ gemspec.add_development_dependency 'rack-test', '>=0.3.0'
21
+
22
+ gemspec.test_files = Dir.glob('test/*')
23
+ gemspec.files = ["LICENSE", "README.md", "Rakefile"] + Dir.glob('lib/**/*') + gemspec.test_files
24
+
25
+ end
26
+ rescue LoadError
27
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
28
+ end
29
+
30
+ Rake::TestTask.new do |t|
31
+ t.libs << "test"
32
+ t.test_files = FileList['test/*_test.rb']
33
+ t.verbose = true
34
+ end
@@ -0,0 +1,70 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module SimpleAuth
5
+ module Helpers
6
+ def authorized?
7
+ session[:arni]
8
+ end
9
+
10
+ def auth!(password)
11
+ if password == options.password
12
+ session[:arni] = true
13
+ redirect_back_or_default(options.home)
14
+ end
15
+ redirect '/a'
16
+ end
17
+
18
+ def logout!
19
+ session.clear
20
+ redirect '/'
21
+ end
22
+
23
+ def protected!
24
+ unless authorized?
25
+ store_location
26
+ redirect '/a'
27
+ end
28
+ end
29
+
30
+ def store_location
31
+ session[:return_to] = request.fullpath if request.get?
32
+ end
33
+
34
+ protected
35
+ def redirect_back_or_default(default)
36
+ if session[:return_to] && session[:return_to] !=~ /^\/a\/?$/
37
+ back = session[:return_to].clone
38
+ session[:return_to] = nil
39
+ redirect back
40
+ end
41
+ redirect default
42
+ end
43
+
44
+ end
45
+
46
+ def self.registered(app)
47
+ app.helpers SimpleAuth::Helpers
48
+
49
+ app.set :password, 'password'
50
+ app.set :home, '/'
51
+
52
+ ['/a/?', '/login/?', '/signin/?'].each do |r|
53
+ app.post r do
54
+ auth!(params[:password])
55
+ end
56
+ end
57
+
58
+ app.delete '/a/?' do
59
+ logout!
60
+ end
61
+
62
+ app.get '/logout/?' do
63
+ logout!
64
+ end
65
+ end
66
+
67
+ end
68
+
69
+ register SimpleAuth
70
+ end
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'sinatra'
3
+ require 'lib/sinatra/simple_auth'
4
+
5
+
6
+ set :password, 'hello'
7
+ set :home, '/secret/'
8
+
9
+ get '/' do
10
+ "hello, i'm root"
11
+ end
12
+
13
+ get '/public' do
14
+ if authorized?
15
+ "hello, %username%"
16
+ else
17
+ "Please login"
18
+ end
19
+ end
20
+
21
+ get '/pvt' do
22
+ protected!
23
+ "private area"
24
+ end
@@ -0,0 +1,56 @@
1
+ require 'sinatra_app'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+ begin; require 'turn'; rescue LoadError; end
5
+
6
+ set :environment, :test
7
+ enable :sessions
8
+
9
+ class SinatraSimpleAuthTest < Test::Unit::TestCase
10
+ include Rack::Test::Methods
11
+
12
+ def app
13
+ Sinatra::Application
14
+ end
15
+
16
+ def test_it_should_login_and_redirect
17
+ post '/a', {:password => app.password}
18
+ assert_redirect app.home
19
+ end
20
+
21
+ def test_it_should_fail_login_and_redirect
22
+ post '/a', {:password => 'some fake data'}
23
+ assert_redirect '/a'
24
+ end
25
+
26
+ def test_it_should_login_and_redirect_back
27
+ get '/pvt'
28
+ assert_redirect '/a'
29
+ login!
30
+ assert_redirect '/pvt'
31
+ end
32
+
33
+ def test_it_should_logout
34
+ login!
35
+ delete '/a'
36
+ assert_redirect '/'
37
+ end
38
+
39
+ def test_authorized_helper_should_work
40
+ get '/public'
41
+ assert last_response.body.include?("Please login")
42
+ login!
43
+ get '/public'
44
+ assert last_response.body.include?("%username%")
45
+ end
46
+
47
+ protected
48
+ def login!
49
+ post '/a', {:password => app.password}
50
+ end
51
+
52
+ def assert_redirect(path)
53
+ assert last_response.redirect?
54
+ assert_equal last_response.headers['Location'], path
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-simple-auth
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Vasily Polovnyov
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-24 00:00:00 +03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: sinatra
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 0
30
+ - 0
31
+ version: 1.0.0
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rack
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 9
44
+ - 1
45
+ version: 0.9.1
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rack-test
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ - 3
58
+ - 0
59
+ version: 0.3.0
60
+ type: :development
61
+ version_requirements: *id003
62
+ description: super simple auth extension for Sinatra
63
+ email: vasily@polovnyov.ru
64
+ executables: []
65
+
66
+ extensions: []
67
+
68
+ extra_rdoc_files:
69
+ - LICENSE
70
+ - README.md
71
+ files:
72
+ - LICENSE
73
+ - README.md
74
+ - Rakefile
75
+ - lib/sinatra/simple_auth.rb
76
+ - test/sinatra_app.rb
77
+ - test/sinatra_simple_auth_test.rb
78
+ has_rdoc: true
79
+ homepage: http://github.com/vast/sinatra-simple-auth
80
+ licenses: []
81
+
82
+ post_install_message:
83
+ rdoc_options:
84
+ - --charset=UTF-8
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ requirements: []
102
+
103
+ rubyforge_project:
104
+ rubygems_version: 1.3.6
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: super simple auth extension for Sinatra
108
+ test_files:
109
+ - test/sinatra_simple_auth_test.rb
110
+ - test/sinatra_app.rb