sinatra-simple-auth 0.1.1 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ .idea/*
2
+ pkg/*
3
+ Gemfile.lock
4
+ .rbenv-version
5
+ .bundle
6
+ *.gem
7
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README.md CHANGED
@@ -1,34 +1,70 @@
1
- ##Sinatra SimpleAuth Extension
1
+ ## Sinatra SimpleAuth Extension
2
2
 
3
- Extends Sinatra with a extension methods and routes for dealing with a simple, single-password authorization
3
+ Sinatra extension with methods and routes for dealing with simple, single-password authorization
4
4
 
5
- ##Installation
5
+ ## Installation
6
6
 
7
- sudo gem install sinatra-simple-auth -s http://gemcutter.org/
7
+ sudo gem install sinatra-simple-auth
8
8
 
9
- ##Usage
9
+ Or via bundler:
10
10
 
11
+ gem 'sinatra-simple-auth'
12
+
13
+ ## Usage (classic style applications)
14
+
15
+ # classic_app.rb
11
16
  require 'rubygems'
12
17
  require 'sinatra'
13
18
  require 'sinatra/simple_auth'
14
19
 
15
20
  enable :sessions
16
- set :password, 'my_cool_password' #define single password
17
- set :home, '/secure/' #where user should be redirected after successful authorization
21
+ set :password, 'my_cool_password' # set the password
22
+ set :home, '/secure/' # where user should be redirected after successful authentication
18
23
 
19
- get '/a/?' do
20
- erb :auth #page with logon form
24
+ get '/login/?' do
25
+ erb :login # page with auth form
21
26
  end
22
27
 
23
28
  get '/secure/' do
24
- protected! #protected route, requires auth
29
+ protected! # protected route, requires auth
25
30
  erb :secure
26
31
  end
27
32
 
28
33
  get '/' do
29
- if authorized? #helper method
34
+ if authorized? # helper method
30
35
  "Hello, %username%"
31
36
  else
32
37
  "Not authorized"
33
38
  end
34
39
  end
40
+
41
+ ## Usage (modular style applications)
42
+
43
+ # modular_app.rb
44
+ require 'sinatra/base'
45
+ require 'sinatra/simple_auth'
46
+
47
+ class SinatraModularApp < Sinatra::Base
48
+ enable :sessions
49
+ register Sinatra::SimpleAuth
50
+
51
+ set :password, 'hello' # set the password
52
+ set :home, '/' # where user should be redirected after successful authentication
53
+
54
+ get '/login/?' do
55
+ erb :login # page with auth form
56
+ end
57
+
58
+ get '/secure/' do
59
+ protected! # protected route, requires auth
60
+ erb :secure
61
+ end
62
+
63
+ get '/' do
64
+ if authorized? # helper method
65
+ "Hello, %username%"
66
+ else
67
+ "Not authorized"
68
+ end
69
+ end
70
+ end
data/Rakefile CHANGED
@@ -1,31 +1,9 @@
1
- require 'rubygems'
2
1
  require 'rake/testtask'
3
- require 'rake/gempackagetask'
2
+ require 'bundler'
3
+ Bundler::GemHelper.install_tasks
4
4
 
5
5
  task :default => :test
6
6
 
7
- begin
8
- require 'jeweler'
9
- Jeweler::Tasks.new do |gemspec|
10
- gemspec.name = "sinatra-simple-auth"
11
- gemspec.version = "0.1.1"
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
-
19
- gemspec.add_development_dependency 'rack-test', '>=0.3.0'
20
-
21
- gemspec.test_files = Dir.glob('test/*')
22
- gemspec.files = ["LICENSE", "README.md", "Rakefile"] + Dir.glob('lib/**/*') + gemspec.test_files
23
-
24
- end
25
- rescue LoadError
26
- puts "Jeweler not available. Install it with: sudo gem install jeweler"
27
- end
28
-
29
7
  Rake::TestTask.new do |t|
30
8
  t.libs << "test"
31
9
  t.test_files = FileList['test/*_test.rb']
@@ -8,22 +8,22 @@ module Sinatra
8
8
  end
9
9
 
10
10
  def auth!(password)
11
- if password == options.password
11
+ if password == settings.password
12
12
  session[:arni] = true
13
- redirect_back_or_default(options.home)
13
+ redirect_back_or_default(settings.home)
14
14
  end
15
- redirect '/a'
15
+ redirect to('/login')
16
16
  end
17
17
 
18
18
  def logout!
19
19
  session.clear
20
- redirect '/'
20
+ redirect to('/')
21
21
  end
22
22
 
23
23
  def protected!
24
24
  unless authorized?
25
25
  store_location
26
- redirect '/a'
26
+ redirect to('/login')
27
27
  end
28
28
  end
29
29
 
@@ -33,12 +33,10 @@ module Sinatra
33
33
 
34
34
  protected
35
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
36
+ if session[:return_to] && session[:return_to] !=~ /^\/login\/?$/
37
+ redirect session.delete(:return_to)
40
38
  end
41
- redirect default
39
+ redirect to(default)
42
40
  end
43
41
 
44
42
  end
@@ -49,13 +47,11 @@ module Sinatra
49
47
  app.set :password, 'password'
50
48
  app.set :home, '/'
51
49
 
52
- ['/a/?', '/login/?', '/signin/?'].each do |r|
53
- app.post r do
54
- auth!(params[:password])
55
- end
50
+ app.post '/login/?' do
51
+ auth!(params[:password])
56
52
  end
57
53
 
58
- app.delete '/a/?' do
54
+ app.delete '/logout/?' do
59
55
  logout!
60
56
  end
61
57
 
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "sinatra-simple-auth"
6
+ s.version = '0.5'
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Vasily Polovnyov"]
9
+ s.email = ["vasily@polovnyov.ru"]
10
+ s.homepage = "http://github.com/vast/sinatra-simple-auth/"
11
+ s.summary = %q{super simple auth extension for Sinatra}
12
+ s.description = %q[super simple auth extension for Sinatra]
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_dependency('sinatra', '>=1.1.0')
20
+ s.add_development_dependency('rack-test')
21
+ s.add_development_dependency('turn')
22
+ end
@@ -0,0 +1,27 @@
1
+ require 'sinatra/base'
2
+ require './lib/sinatra/simple_auth'
3
+
4
+ class SinatraModularApp < Sinatra::Base
5
+ enable :sessions
6
+ register Sinatra::SimpleAuth
7
+
8
+ set :password, 'hello'
9
+ set :home, '/'
10
+
11
+ get '/' do
12
+ "hello, i'm root"
13
+ end
14
+
15
+ get '/public' do
16
+ if authorized?
17
+ "hello, %username%"
18
+ else
19
+ "Please login"
20
+ end
21
+ end
22
+
23
+ get '/pvt' do
24
+ protected!
25
+ "private area"
26
+ end
27
+ end
@@ -1,44 +1,49 @@
1
- require 'sinatra_app'
1
+ require 'sinatra_modular_app'
2
2
  require 'test/unit'
3
3
  require 'rack/test'
4
- begin; require 'turn'; rescue LoadError; end
5
-
6
- set :environment, :test
7
- enable :sessions
4
+ begin; require 'turn/autorun'; rescue LoadError; end
8
5
 
9
6
  class SinatraSimpleAuthTest < Test::Unit::TestCase
10
7
  include Rack::Test::Methods
11
8
 
12
9
  def app
13
- Sinatra::Application
10
+ SinatraModularApp
14
11
  end
15
12
 
16
- def test_it_should_login_and_redirect
17
- post '/a', {:password => app.password}
18
- assert_redirect app.home
13
+ def test_it_should_login_and_redirect_home
14
+ post '/login', :password => app.password
15
+ assert_redirected_to app.home
19
16
  end
20
17
 
21
- def test_it_should_fail_login_and_redirect
22
- post '/a', {:password => 'some fake data'}
23
- assert_redirect '/a'
18
+ def test_it_should_fail_login_and_redirect_back_to_form
19
+ post '/login', :password => 'some fake data'
20
+ assert_redirected_to '/login'
24
21
  end
25
22
 
26
23
  def test_it_should_login_and_redirect_back
27
24
  get '/pvt'
28
- assert_redirect '/a'
25
+ assert_redirected_to '/login'
26
+
27
+ login!
28
+ assert_redirected_to '/pvt'
29
+ end
30
+
31
+ def test_it_should_logout_via_delete
29
32
  login!
30
- assert_redirect '/pvt'
33
+ delete '/logout'
34
+ assert_redirected_to '/'
31
35
  end
32
36
 
33
- def test_it_should_logout
37
+ def test_it_should_logout_via_get
34
38
  login!
35
- delete '/a'
36
- assert_redirect '/'
39
+ get '/logout'
40
+ assert_redirected_to '/'
37
41
  end
38
42
 
39
43
  def test_authorized_helper_should_work
40
44
  get '/public'
41
45
  assert last_response.body.include?("Please login")
46
+
42
47
  login!
43
48
  get '/public'
44
49
  assert last_response.body.include?("%username%")
@@ -46,11 +51,11 @@ class SinatraSimpleAuthTest < Test::Unit::TestCase
46
51
 
47
52
  protected
48
53
  def login!
49
- post '/a', {:password => app.password}
54
+ post '/login', :password => app.password
50
55
  end
51
56
 
52
- def assert_redirect(path)
57
+ def assert_redirected_to(path)
53
58
  assert last_response.redirect?
54
- assert_equal last_response.headers['Location'], path
59
+ assert_equal path, last_response.headers['Location'].sub('http://example.org', '')
55
60
  end
56
61
  end
metadata CHANGED
@@ -1,96 +1,110 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sinatra-simple-auth
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.5'
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Vasily Polovnyov
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2010-03-24 00:00:00 +03:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-10-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  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
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.0
32
22
  type: :runtime
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.1.0
30
+ - !ruby/object:Gem::Dependency
35
31
  name: rack-test
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
36
39
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 0
43
- - 3
44
- - 0
45
- version: 0.3.0
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: turn
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
46
54
  type: :development
47
- version_requirements: *id002
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
48
62
  description: super simple auth extension for Sinatra
49
- email: vasily@polovnyov.ru
63
+ email:
64
+ - vasily@polovnyov.ru
50
65
  executables: []
51
-
52
66
  extensions: []
53
-
54
- extra_rdoc_files:
55
- - LICENSE
56
- - README.md
57
- files:
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
58
71
  - LICENSE
59
72
  - README.md
60
73
  - Rakefile
61
74
  - lib/sinatra/simple_auth.rb
62
- - test/sinatra_app.rb
75
+ - sinatra-simple-auth.gemspec
76
+ - test/sinatra_modular_app.rb
63
77
  - test/sinatra_simple_auth_test.rb
64
- has_rdoc: true
65
- homepage: http://github.com/vast/sinatra-simple-auth
78
+ homepage: http://github.com/vast/sinatra-simple-auth/
66
79
  licenses: []
67
-
68
80
  post_install_message:
69
- rdoc_options:
70
- - --charset=UTF-8
71
- require_paths:
81
+ rdoc_options: []
82
+ require_paths:
72
83
  - lib
73
- required_ruby_version: !ruby/object:Gem::Requirement
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- segments:
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ segments:
78
91
  - 0
79
- version: "0"
80
- required_rubygems_version: !ruby/object:Gem::Requirement
81
- requirements:
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- segments:
92
+ hash: 4494792989292088615
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ segments:
85
100
  - 0
86
- version: "0"
101
+ hash: 4494792989292088615
87
102
  requirements: []
88
-
89
103
  rubyforge_project:
90
- rubygems_version: 1.3.6
104
+ rubygems_version: 1.8.23
91
105
  signing_key:
92
106
  specification_version: 3
93
107
  summary: super simple auth extension for Sinatra
94
- test_files:
108
+ test_files:
109
+ - test/sinatra_modular_app.rb
95
110
  - test/sinatra_simple_auth_test.rb
96
- - test/sinatra_app.rb
data/test/sinatra_app.rb DELETED
@@ -1,24 +0,0 @@
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