auth 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -0
- data/lib/auth/middleware.rb +49 -0
- data/lib/auth/version.rb +1 -1
- data/test/middleware_test.rb +52 -0
- metadata +6 -38
data/CHANGELOG
CHANGED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rack'
|
3
|
+
require 'rack/auth/abstract/handler'
|
4
|
+
require 'rack/auth/abstract/request'
|
5
|
+
require 'auth'
|
6
|
+
|
7
|
+
module Auth
|
8
|
+
class Middleware < Rack::Auth::AbstractHandler
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
auth = Request.new(env)
|
12
|
+
|
13
|
+
return unauthorized unless auth.provided?
|
14
|
+
return bad_request unless auth.bearer?
|
15
|
+
|
16
|
+
if valid?(auth)
|
17
|
+
env['REMOTE_USER'] = auth.account_id
|
18
|
+
return @app.call(env)
|
19
|
+
end
|
20
|
+
|
21
|
+
unauthorized
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def challenge
|
27
|
+
'Bearer realm="%s"' % realm
|
28
|
+
end
|
29
|
+
|
30
|
+
def valid?(auth)
|
31
|
+
auth.account_id ? true : false
|
32
|
+
end
|
33
|
+
|
34
|
+
class Request < Rack::Auth::AbstractRequest
|
35
|
+
def bearer?
|
36
|
+
:bearer == scheme
|
37
|
+
end
|
38
|
+
|
39
|
+
def access_token
|
40
|
+
@access_token ||= params.unpack("m*").first
|
41
|
+
end
|
42
|
+
|
43
|
+
def account_id
|
44
|
+
@account_id ||= Auth.validate_token(access_token)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
data/lib/auth/version.rb
CHANGED
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.expand_path('test/test_helper')
|
2
|
+
require 'auth/middleware'
|
3
|
+
|
4
|
+
class MiddlewareTest < Test::Unit::TestCase
|
5
|
+
include Rack::Test::Methods
|
6
|
+
|
7
|
+
def app
|
8
|
+
inner_app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, [env['REMOTE_USER']]] }
|
9
|
+
Auth::Middleware.new(inner_app, 'Test realm')
|
10
|
+
end
|
11
|
+
|
12
|
+
def setup
|
13
|
+
Auth.redis.flushall
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_unauthenticated_request
|
17
|
+
env = Rack::MockRequest.env_for('/test')
|
18
|
+
res = app.call(env)
|
19
|
+
assert_equal 401, res[0]
|
20
|
+
assert_equal 'Bearer realm="Test realm"', res[1]['WWW-Authenticate']
|
21
|
+
assert_empty res[2]
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_authenticated_request
|
25
|
+
token = Auth.issue_token('test-user')
|
26
|
+
env = Rack::MockRequest.env_for('/test',
|
27
|
+
'HTTP_AUTHORIZATION' => "Bearer #{Base64.encode64(token)}")
|
28
|
+
res = app.call(env)
|
29
|
+
assert_equal 200, res[0]
|
30
|
+
assert_equal nil, res[1]['WWW-Authenticate']
|
31
|
+
assert_equal ['test-user'], res[2]
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_authenticated_non_bearer_request
|
35
|
+
env = Rack::MockRequest.env_for('/test',
|
36
|
+
'HTTP_AUTHORIZATION' => "Basic #{Base64.encode64('test')}")
|
37
|
+
res = app.call(env)
|
38
|
+
assert_equal 400, res[0]
|
39
|
+
assert_equal nil, res[1]['WWW-Authenticate']
|
40
|
+
assert_empty res[2]
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_authenticated_invalid_request
|
44
|
+
env = Rack::MockRequest.env_for('/test',
|
45
|
+
'HTTP_AUTHORIZATION' => "Bearer #{Base64.encode64('wrong')}")
|
46
|
+
res = app.call(env)
|
47
|
+
assert_equal 401, res[0]
|
48
|
+
assert_equal 'Bearer realm="Test realm"', res[1]['WWW-Authenticate']
|
49
|
+
assert_empty res[2]
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: auth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 5
|
9
|
-
version: 0.0.5
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.6
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Niklas Holmgren
|
@@ -14,8 +10,7 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date: 2011-
|
18
|
-
default_executable:
|
13
|
+
date: 2011-12-19 00:00:00 Z
|
19
14
|
dependencies:
|
20
15
|
- !ruby/object:Gem::Dependency
|
21
16
|
name: json
|
@@ -25,10 +20,6 @@ dependencies:
|
|
25
20
|
requirements:
|
26
21
|
- - ">"
|
27
22
|
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 1
|
30
|
-
- 0
|
31
|
-
- 0
|
32
23
|
version: 1.0.0
|
33
24
|
type: :runtime
|
34
25
|
version_requirements: *id001
|
@@ -40,10 +31,6 @@ dependencies:
|
|
40
31
|
requirements:
|
41
32
|
- - ">"
|
42
33
|
- !ruby/object:Gem::Version
|
43
|
-
segments:
|
44
|
-
- 1
|
45
|
-
- 0
|
46
|
-
- 0
|
47
34
|
version: 1.0.0
|
48
35
|
type: :runtime
|
49
36
|
version_requirements: *id002
|
@@ -55,10 +42,6 @@ dependencies:
|
|
55
42
|
requirements:
|
56
43
|
- - ">"
|
57
44
|
- !ruby/object:Gem::Version
|
58
|
-
segments:
|
59
|
-
- 1
|
60
|
-
- 0
|
61
|
-
- 0
|
62
45
|
version: 1.0.0
|
63
46
|
type: :runtime
|
64
47
|
version_requirements: *id003
|
@@ -70,10 +53,6 @@ dependencies:
|
|
70
53
|
requirements:
|
71
54
|
- - ">"
|
72
55
|
- !ruby/object:Gem::Version
|
73
|
-
segments:
|
74
|
-
- 2
|
75
|
-
- 0
|
76
|
-
- 0
|
77
56
|
version: 2.0.0
|
78
57
|
type: :runtime
|
79
58
|
version_requirements: *id004
|
@@ -85,10 +64,6 @@ dependencies:
|
|
85
64
|
requirements:
|
86
65
|
- - ">"
|
87
66
|
- !ruby/object:Gem::Version
|
88
|
-
segments:
|
89
|
-
- 0
|
90
|
-
- 8
|
91
|
-
- 0
|
92
67
|
version: 0.8.0
|
93
68
|
type: :runtime
|
94
69
|
version_requirements: *id005
|
@@ -100,10 +75,6 @@ dependencies:
|
|
100
75
|
requirements:
|
101
76
|
- - ">"
|
102
77
|
- !ruby/object:Gem::Version
|
103
|
-
segments:
|
104
|
-
- 0
|
105
|
-
- 5
|
106
|
-
- 6
|
107
78
|
version: 0.5.6
|
108
79
|
type: :development
|
109
80
|
version_requirements: *id006
|
@@ -124,16 +95,17 @@ files:
|
|
124
95
|
- lib/auth/client.rb
|
125
96
|
- lib/auth/exceptions.rb
|
126
97
|
- lib/auth/helpers.rb
|
98
|
+
- lib/auth/middleware.rb
|
127
99
|
- lib/auth/sentry.rb
|
128
100
|
- lib/auth/server/views/authorize.erb
|
129
101
|
- lib/auth/server.rb
|
130
102
|
- lib/auth/version.rb
|
131
103
|
- lib/auth.rb
|
132
104
|
- test/auth_test.rb
|
105
|
+
- test/middleware_test.rb
|
133
106
|
- test/redis-test.conf
|
134
107
|
- test/server_test.rb
|
135
108
|
- test/test_helper.rb
|
136
|
-
has_rdoc: true
|
137
109
|
homepage: http://github.com/sutajio/auth/
|
138
110
|
licenses: []
|
139
111
|
|
@@ -147,21 +119,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
147
119
|
requirements:
|
148
120
|
- - ">="
|
149
121
|
- !ruby/object:Gem::Version
|
150
|
-
segments:
|
151
|
-
- 0
|
152
122
|
version: "0"
|
153
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
154
124
|
none: false
|
155
125
|
requirements:
|
156
126
|
- - ">="
|
157
127
|
- !ruby/object:Gem::Version
|
158
|
-
segments:
|
159
|
-
- 0
|
160
128
|
version: "0"
|
161
129
|
requirements: []
|
162
130
|
|
163
131
|
rubyforge_project:
|
164
|
-
rubygems_version: 1.
|
132
|
+
rubygems_version: 1.8.11
|
165
133
|
signing_key:
|
166
134
|
specification_version: 3
|
167
135
|
summary: Auth is a Redis-backed high performance OAuth2 authorization server.
|