sinatra-single-user-auth 0.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 +7 -0
- data/.travis.yml +3 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +91 -0
- data/Rakefile +11 -0
- data/lib/sinatra/simple_auth.rb +67 -0
- data/sinatra-single-user-auth.gemspec +24 -0
- data/test/sinatra_modular_app.rb +28 -0
- data/test/sinatra_simple_auth_test.rb +71 -0
- metadata +139 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
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,91 @@
|
|
1
|
+
## Sinatra Single User Simple Auth Extension
|
2
|
+
|
3
|
+
[](https://travis-ci.org/kingsleyh/sinatra-single-user-auth)
|
4
|
+
|
5
|
+
Sinatra extension with methods and routes for dealing with simple, single user authorization (username and password)
|
6
|
+
|
7
|
+
## Background
|
8
|
+
|
9
|
+
This extension is based on the awesome [sinatra-simple-auth](https://github.com/vast/sinatra-simple-auth) gem by Vasily Polovnyov. I wanted
|
10
|
+
the exact same functionality but I also wanted to be able to specify a username/email as well so I can do some custom atlassian crowd authentication.
|
11
|
+
I thought someone else may find having a username handy as well so here it is. Massive thanks to Vasily as 99.9% of this plugin is his work - I
|
12
|
+
just added the username field.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
```console
|
17
|
+
gem install sinatra-single-user-auth
|
18
|
+
```
|
19
|
+
|
20
|
+
Or via bundler:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'sinatra-single-user-auth'
|
24
|
+
```
|
25
|
+
|
26
|
+
## Usage (classic style applications)
|
27
|
+
|
28
|
+
You just need to make sure you pass the params username and password from the login form
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
# classic_app.rb
|
32
|
+
require 'rubygems'
|
33
|
+
require 'sinatra'
|
34
|
+
require 'sinatra/simple_auth'
|
35
|
+
|
36
|
+
enable :sessions
|
37
|
+
set :username, 'my_username' # set the username
|
38
|
+
set :password, 'my_cool_password' # set the password
|
39
|
+
set :home, '/secure/' # where user should be redirected after successful authentication
|
40
|
+
|
41
|
+
get '/login/?' do
|
42
|
+
erb :login # page with auth form
|
43
|
+
end
|
44
|
+
|
45
|
+
get '/secure/' do
|
46
|
+
protected! # protected route, requires auth
|
47
|
+
erb :secure
|
48
|
+
end
|
49
|
+
|
50
|
+
get '/' do
|
51
|
+
if authorized? # helper method
|
52
|
+
"Hello, #{app.username}"
|
53
|
+
else
|
54
|
+
"Not authorized"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
## Usage (modular style applications)
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
# modular_app.rb
|
63
|
+
require 'sinatra/base'
|
64
|
+
require 'sinatra/simple_auth'
|
65
|
+
|
66
|
+
class SinatraModularApp < Sinatra::Base
|
67
|
+
enable :sessions
|
68
|
+
register Sinatra::SimpleAuth
|
69
|
+
|
70
|
+
set :username, 'my_username' # set the username
|
71
|
+
set :password, 'my_password' # set the password
|
72
|
+
set :home, '/' # where user should be redirected after successful authentication
|
73
|
+
|
74
|
+
get '/login/?' do
|
75
|
+
erb :login # page with auth form
|
76
|
+
end
|
77
|
+
|
78
|
+
get '/secure/' do
|
79
|
+
protected! # protected route, requires auth
|
80
|
+
erb :secure
|
81
|
+
end
|
82
|
+
|
83
|
+
get '/' do
|
84
|
+
if authorized? # helper method
|
85
|
+
"Hello, #{app.username}"
|
86
|
+
else
|
87
|
+
"Not authorized"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,67 @@
|
|
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,username)
|
11
|
+
if password == settings.password and username == settings.username
|
12
|
+
session[:arni] = true
|
13
|
+
redirect_back_or_default(settings.home)
|
14
|
+
end
|
15
|
+
redirect to('/login')
|
16
|
+
end
|
17
|
+
|
18
|
+
def logout!
|
19
|
+
session.clear
|
20
|
+
redirect to('/')
|
21
|
+
end
|
22
|
+
|
23
|
+
def protected!
|
24
|
+
unless authorized?
|
25
|
+
store_location
|
26
|
+
redirect to('/login')
|
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] !=~ /^\/login\/?$/
|
37
|
+
redirect session.delete(:return_to)
|
38
|
+
end
|
39
|
+
redirect to(default)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.registered(app)
|
45
|
+
app.helpers SimpleAuth::Helpers
|
46
|
+
|
47
|
+
app.set :username, 'username'
|
48
|
+
app.set :password, 'password'
|
49
|
+
app.set :home, '/'
|
50
|
+
|
51
|
+
app.post '/login/?' do
|
52
|
+
auth!(params[:password],params[:username])
|
53
|
+
end
|
54
|
+
|
55
|
+
app.delete '/logout/?' do
|
56
|
+
logout!
|
57
|
+
end
|
58
|
+
|
59
|
+
app.get '/logout/?' do
|
60
|
+
logout!
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
register SimpleAuth
|
67
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "sinatra-single-user-auth"
|
6
|
+
s.version = '0.1'
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Kingsley Hendrickse","Vasily Polovnyov"]
|
9
|
+
s.email = ["kingsley@masterthought.net","vasily@polovnyov.ru"]
|
10
|
+
s.homepage = "http://github.com/vast/sinatra-simple-auth/"
|
11
|
+
s.summary = %q{super simple single user auth extension for Sinatra}
|
12
|
+
s.description = %q[super simple single user 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('rake')
|
21
|
+
s.add_development_dependency('rack-test')
|
22
|
+
s.add_development_dependency('turn')
|
23
|
+
s.add_development_dependency('minitest-reporters')
|
24
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/sinatra/simple_auth'
|
3
|
+
|
4
|
+
class SinatraModularApp < Sinatra::Base
|
5
|
+
enable :sessions
|
6
|
+
register Sinatra::SimpleAuth
|
7
|
+
|
8
|
+
set :username, 'username'
|
9
|
+
set :password, 'password'
|
10
|
+
set :home, '/'
|
11
|
+
|
12
|
+
get '/' do
|
13
|
+
"hello, i'm root"
|
14
|
+
end
|
15
|
+
|
16
|
+
get '/public' do
|
17
|
+
if authorized?
|
18
|
+
"hello, #{settings.username}"
|
19
|
+
else
|
20
|
+
"Please login"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
get '/pvt' do
|
25
|
+
protected!
|
26
|
+
"private area"
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/sinatra_modular_app'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'rack/test'
|
4
|
+
begin; require 'turn/autorun'; rescue LoadError; end
|
5
|
+
|
6
|
+
class SinatraSimpleAuthTest < Test::Unit::TestCase
|
7
|
+
include Rack::Test::Methods
|
8
|
+
|
9
|
+
def app
|
10
|
+
SinatraModularApp
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_it_should_login_and_redirect_home
|
14
|
+
post '/login', :password => app.password, :username => app.username
|
15
|
+
assert_redirected_to app.home
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_it_should_fail_login_with_incorrect_password_and_username_and_redirect_back_to_form
|
19
|
+
post '/login', :password => 'incorrect', :username => 'non existing'
|
20
|
+
assert_redirected_to '/login'
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_it_should_fail_login_with_incorrect_password_and_redirect_back_to_form
|
24
|
+
post '/login', :password => 'incorrect', :username => app.username
|
25
|
+
assert_redirected_to '/login'
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_it_should_fail_login_with_incorrect_username_and_redirect_back_to_form
|
29
|
+
post '/login', :password => app.password, :username => 'non existing'
|
30
|
+
assert_redirected_to '/login'
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_it_should_login_and_redirect_back
|
34
|
+
get '/pvt'
|
35
|
+
assert_redirected_to '/login'
|
36
|
+
|
37
|
+
login!
|
38
|
+
assert_redirected_to '/pvt'
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_it_should_logout_via_delete
|
42
|
+
login!
|
43
|
+
delete '/logout'
|
44
|
+
assert_redirected_to '/'
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_it_should_logout_via_get
|
48
|
+
login!
|
49
|
+
get '/logout'
|
50
|
+
assert_redirected_to '/'
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_authorized_helper_should_work
|
54
|
+
get '/public'
|
55
|
+
assert last_response.body.include?("Please login")
|
56
|
+
|
57
|
+
login!
|
58
|
+
get '/public'
|
59
|
+
assert last_response.body.include?(app.username)
|
60
|
+
end
|
61
|
+
|
62
|
+
protected
|
63
|
+
def login!
|
64
|
+
post '/login', :password => app.password, :username => app.username
|
65
|
+
end
|
66
|
+
|
67
|
+
def assert_redirected_to(path)
|
68
|
+
assert last_response.redirect?
|
69
|
+
assert_equal path, last_response.headers['Location'].sub('http://example.org', '')
|
70
|
+
end
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-single-user-auth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kingsley Hendrickse
|
9
|
+
- Vasily Polovnyov
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-07-06 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: sinatra
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.1.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: 1.1.0
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: rake
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
type: :development
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rack-test
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: turn
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
name: minitest-reporters
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
type: :development
|
88
|
+
prerelease: false
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
description: super simple single user auth extension for Sinatra
|
96
|
+
email:
|
97
|
+
- kingsley@masterthought.net
|
98
|
+
- vasily@polovnyov.ru
|
99
|
+
executables: []
|
100
|
+
extensions: []
|
101
|
+
extra_rdoc_files: []
|
102
|
+
files:
|
103
|
+
- .gitignore
|
104
|
+
- .travis.yml
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- lib/sinatra/simple_auth.rb
|
110
|
+
- sinatra-single-user-auth.gemspec
|
111
|
+
- test/sinatra_modular_app.rb
|
112
|
+
- test/sinatra_simple_auth_test.rb
|
113
|
+
homepage: http://github.com/vast/sinatra-simple-auth/
|
114
|
+
licenses: []
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - ! '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
127
|
+
requirements:
|
128
|
+
- - ! '>='
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 1.8.25
|
134
|
+
signing_key:
|
135
|
+
specification_version: 3
|
136
|
+
summary: super simple single user auth extension for Sinatra
|
137
|
+
test_files:
|
138
|
+
- test/sinatra_modular_app.rb
|
139
|
+
- test/sinatra_simple_auth_test.rb
|