sinatra-basicauth 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +21 -0
- data/lib/sinatra-basicauth.rb +1 -0
- data/lib/sinatra/basic_auth.rb +82 -0
- data/sinatra-basicauth.gemspec +19 -0
- metadata +60 -0
data/README.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# Sinatra Basic Auth
|
2
|
+
Simple basic auth helper for Sinatra
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
|
6
|
+
require 'sinatra'
|
7
|
+
require 'sinatra/basic_auth'
|
8
|
+
basic_auth do
|
9
|
+
realm 'Give me password!'
|
10
|
+
username 'Frank'
|
11
|
+
password 'changeme'
|
12
|
+
end
|
13
|
+
|
14
|
+
get '/' do
|
15
|
+
'This is public'
|
16
|
+
end
|
17
|
+
|
18
|
+
get '/private' do
|
19
|
+
require_basic_auth
|
20
|
+
'This is private'
|
21
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'sinatra/basic_auth'
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
module Sinatra
|
4
|
+
|
5
|
+
class AuthenticationSettings
|
6
|
+
|
7
|
+
def initialize app, &blk
|
8
|
+
@app = app
|
9
|
+
instance_eval &blk
|
10
|
+
end
|
11
|
+
|
12
|
+
%w[ username password realm ].each do |param|
|
13
|
+
class_eval %[
|
14
|
+
def #{param} val, &blk
|
15
|
+
@app.set :basic_auth_#{param}, val
|
16
|
+
end
|
17
|
+
]
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
module Helpers
|
23
|
+
class Authentication
|
24
|
+
attr_accessor :request, :app
|
25
|
+
def initialize( app, request )
|
26
|
+
self.app = app
|
27
|
+
self.request = request
|
28
|
+
end
|
29
|
+
|
30
|
+
def auth
|
31
|
+
@basic_auth ||= Rack::Auth::Basic::Request.new( request.env )
|
32
|
+
end
|
33
|
+
|
34
|
+
def unauthorized!
|
35
|
+
app.headers 'WWW-Authenticate' => %(Basic realm="#{settings.basic_auth_realm}")
|
36
|
+
throw :halt, [ 401, 'Authorization Required' ]
|
37
|
+
end
|
38
|
+
|
39
|
+
def bad_request!
|
40
|
+
throw :halt, [ 400, 'Bad Request' ]
|
41
|
+
end
|
42
|
+
|
43
|
+
def authorized?
|
44
|
+
request.env['REMOTE_USER']
|
45
|
+
end
|
46
|
+
|
47
|
+
def authorize( username, password )
|
48
|
+
settings.basic_auth_username == username &&
|
49
|
+
settings.basic_auth_password == password
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def require_basic_auth
|
54
|
+
a = Authentication.new( self, request )
|
55
|
+
return if a.authorized?
|
56
|
+
a.unauthorized! unless a.auth.provided?
|
57
|
+
a.bad_request! unless a.auth.basic?
|
58
|
+
a.unauthorized! unless a.authorize( *a.auth.credentials )
|
59
|
+
request.env['REMOTE_USER'] = a.auth.username
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
module BasicAuth
|
65
|
+
|
66
|
+
def basic_auth &block
|
67
|
+
AuthenticationSettings.new(self, &block)
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.registered( app )
|
71
|
+
app.set :basic_auth_realm, 'You need to authenticate yourself!'
|
72
|
+
app.set :basic_auth_username, 'Frank'
|
73
|
+
app.set :basic_auth_password, 'changeme'
|
74
|
+
|
75
|
+
app.helpers Helpers
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
register BasicAuth
|
81
|
+
end
|
82
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
spec = Gem::Specification.new do |s|
|
2
|
+
s.name = 'sinatra-basicauth'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2012-01-28'
|
5
|
+
s.summary = 'Simple basic auth helper for Sinatra'
|
6
|
+
s.description = s.summary
|
7
|
+
|
8
|
+
s.homepage = "http://github.com/trekdemo/sinatra-basicauth"
|
9
|
+
|
10
|
+
s.authors = ["Gergo Sulymosi"]
|
11
|
+
s.email = "gergo.sulymosi@gmail.com"
|
12
|
+
|
13
|
+
s.add_dependency('rack')
|
14
|
+
s.has_rdoc = false
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n").sort
|
17
|
+
#s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-basicauth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gergo Sulymosi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-28 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack
|
16
|
+
requirement: &70260062828600 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70260062828600
|
25
|
+
description: Simple basic auth helper for Sinatra
|
26
|
+
email: gergo.sulymosi@gmail.com
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- README.md
|
32
|
+
- lib/sinatra-basicauth.rb
|
33
|
+
- lib/sinatra/basic_auth.rb
|
34
|
+
- sinatra-basicauth.gemspec
|
35
|
+
homepage: http://github.com/trekdemo/sinatra-basicauth
|
36
|
+
licenses: []
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.8.6
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: Simple basic auth helper for Sinatra
|
59
|
+
test_files: []
|
60
|
+
has_rdoc: false
|