lockbox_middleware 1.2.2 → 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +43 -5
- data/lib/lockbox_cache.rb +2 -2
- data/lib/lockbox_middleware.rb +1 -1
- data/spec/lib/lockbox_cache_spec.rb +50 -0
- data/spec/lib/lockbox_middleware_spec.rb +23 -7
- metadata +6 -4
data/README.rdoc
CHANGED
@@ -4,14 +4,31 @@ LockBox is a centralized API authentication service written by the DNC Innovatio
|
|
4
4
|
users share a single identity across multiple services.
|
5
5
|
It is licensed under the New BSD License (see the LICENSE file for details).
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
unfortunate Rails dependency in the middleware gem. Hopefully we'll get rid of that soon.
|
7
|
+
The server is a Ruby on Rails application, while the client is pure Rack middleware (which means it
|
8
|
+
integrates nicely with any modern Ruby web framework, including Rails 2.3+).
|
10
9
|
|
11
10
|
Lockbox handles things like rate limiting, API key signup and management, and supports HMAC
|
12
|
-
authentication as well as plain-text key exchange.
|
11
|
+
authentication as well as plain-text key exchange.
|
13
12
|
|
14
|
-
==
|
13
|
+
== Client Installation
|
14
|
+
|
15
|
+
gem install lockbox_middleware
|
16
|
+
|
17
|
+
=== Rails 2
|
18
|
+
|
19
|
+
Add the following lines to the "config/environment.rb" file:
|
20
|
+
|
21
|
+
config.gem 'lockbox_middleware'
|
22
|
+
config.middleware.use 'LockBox'
|
23
|
+
|
24
|
+
You may want to restrict the "config.middleware.use" line to the config/environments/production.rb file so that
|
25
|
+
your test and development environments aren't trying to access the LockBox server.
|
26
|
+
|
27
|
+
=== Rails 3
|
28
|
+
|
29
|
+
Untested.
|
30
|
+
|
31
|
+
== Client Configuration
|
15
32
|
|
16
33
|
LockBox needs a configuration file named "lockbox.yml" in order to work. In a Rack app (incl. Rails),
|
17
34
|
this file should be placed in app_root/config/lockbox.yml.
|
@@ -33,6 +50,27 @@ Here's an example lockbox.yml:
|
|
33
50
|
protect_paths:
|
34
51
|
- ^/api/
|
35
52
|
|
53
|
+
The 'all' section of the yaml file is a LockBox-specific shortcut so you can DRY up your protected path definitions.
|
54
|
+
Settings in more specific environments will override those in the 'all' section.
|
55
|
+
|
56
|
+
== Server Installation
|
57
|
+
|
58
|
+
Running your own LockBox server is pretty easy. Just clone this repository (I'd stick with the master branch)
|
59
|
+
and set it up under your favorite Rails hosting environment. It has only been tested with PostgreSQL, but it's not
|
60
|
+
doing anything exotic with the DB, so it will probably work with other databases too.
|
61
|
+
|
62
|
+
The gem dependencies are generally kept up to date in the Gemfile, so you can use bundler:
|
63
|
+
|
64
|
+
gem install bundler
|
65
|
+
bundle install
|
66
|
+
|
67
|
+
Or you can use the Rails 2 gem installer:
|
68
|
+
|
69
|
+
rake gems:install
|
70
|
+
|
71
|
+
Then you should configure the database.yml for the test environment and run 'rake spec' in the app root to have RSpec
|
72
|
+
(gem install rspec-rails) run all of the tests. If these all pass, then you're probably good to go.
|
73
|
+
|
36
74
|
== Download
|
37
75
|
|
38
76
|
Github: http://github.com/dnclabs/lockbox/tree/master
|
data/lib/lockbox_cache.rb
CHANGED
data/lib/lockbox_middleware.rb
CHANGED
@@ -69,7 +69,7 @@ class LockBox
|
|
69
69
|
return [app_response[0], response_headers, app_response[2]]
|
70
70
|
else
|
71
71
|
message = "Access Denied"
|
72
|
-
return [401, {'Content-Type' => 'text/plain', 'Content-Length' => "#{message.length}"}, message]
|
72
|
+
return [401, {'Content-Type' => 'text/plain', 'Content-Length' => "#{message.length}"}, [message]]
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'lockbox_cache'
|
3
|
+
|
4
|
+
describe LockBoxCache::Cache do
|
5
|
+
subject { LockBoxCache::Cache.new }
|
6
|
+
|
7
|
+
describe "#write" do
|
8
|
+
it "should save what you write to it" do
|
9
|
+
subject.write(:foo, 'bar')
|
10
|
+
subject.read(:foo).should == 'bar'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#read" do
|
15
|
+
it "should return nil when reading a non-existent key" do
|
16
|
+
subject.read(:foo).should be_nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#delete" do
|
21
|
+
it "should delete the key and value" do
|
22
|
+
subject.write(:foo, 'bar')
|
23
|
+
subject.delete(:foo)
|
24
|
+
subject.read(:foo).should be_nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "in a Rails app" do
|
29
|
+
it "should use the Rails cache" do
|
30
|
+
subject.write(:foo, 'bar')
|
31
|
+
Rails.cache.read(:foo).should == 'bar'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "in a Rack app" do
|
36
|
+
it "should still work" do
|
37
|
+
cache = LockBoxCache::Cache.new(false)
|
38
|
+
cache.write(:foo, 'bar')
|
39
|
+
cache.read(:foo).should == 'bar'
|
40
|
+
Rails.cache.read(:foo).should be_nil
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should still delete shit" do
|
44
|
+
cache = LockBoxCache::Cache.new(false)
|
45
|
+
cache.write(:foo, 'bar')
|
46
|
+
cache.delete(:foo)
|
47
|
+
cache.read(:foo).should be_nil
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -7,7 +7,7 @@ describe 'LockBox' do
|
|
7
7
|
|
8
8
|
def app
|
9
9
|
# Initialize our LockBox middleware with an "app" that just always returns 200, if it gets .called
|
10
|
-
LockBox.new(Proc.new {|env| [200,{},"successfully hit rails app"]})
|
10
|
+
LockBox.new(Proc.new {|env| [200,{'Content-Type' => 'text/plain'},["successfully hit rails app"]]})
|
11
11
|
end
|
12
12
|
|
13
13
|
def safely_edit_config_file(settings, env=nil)
|
@@ -105,14 +105,30 @@ describe 'LockBox' do
|
|
105
105
|
LockBox.stubs(:get).with("/authentication/blah", any_parameters).returns(bad_response)
|
106
106
|
end
|
107
107
|
|
108
|
-
|
109
|
-
|
110
|
-
|
108
|
+
context "with invalid api key" do
|
109
|
+
it "should return 401 for a protected path request" do
|
110
|
+
get "/api/some_controller/some_action?key=blah"
|
111
|
+
last_response.status.should == 401
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should return an array as the response body" do
|
115
|
+
# Rack compliance thing
|
116
|
+
env = Rack::MockRequest.env_for "/api/some_controller/some_action?key=blah"
|
117
|
+
app.call(env)[2].should be_an_instance_of(Array)
|
118
|
+
end
|
111
119
|
end
|
112
120
|
|
113
|
-
|
114
|
-
|
115
|
-
|
121
|
+
context "with valid api key" do
|
122
|
+
it "should return 200 for a request that starts with /api and has api key" do
|
123
|
+
get "/api/some_controller/some_action?key=123456"
|
124
|
+
last_response.status.should == 200
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should have a Content-Type header" do
|
128
|
+
# Rack compliance bug
|
129
|
+
env = Rack::MockRequest.env_for "/api/some_controller/some_action?key=123456"
|
130
|
+
app.call(env)[1].should include('Content-Type')
|
131
|
+
end
|
116
132
|
end
|
117
133
|
|
118
134
|
it "should cache lockbox responses for max-age when Cache-Control allows it" do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lockbox_middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 1.2.
|
9
|
+
- 3
|
10
|
+
version: 1.2.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Chris Gill
|
@@ -18,7 +18,7 @@ autorequire:
|
|
18
18
|
bindir: bin
|
19
19
|
cert_chain: []
|
20
20
|
|
21
|
-
date: 2010-
|
21
|
+
date: 2010-07-08 00:00:00 -04:00
|
22
22
|
default_executable:
|
23
23
|
dependencies:
|
24
24
|
- !ruby/object:Gem::Dependency
|
@@ -63,6 +63,7 @@ files:
|
|
63
63
|
- lib/lockbox_middleware.rb
|
64
64
|
- LICENSE
|
65
65
|
- README.rdoc
|
66
|
+
- spec/lib/lockbox_cache_spec.rb
|
66
67
|
- spec/lib/lockbox_middleware_spec.rb
|
67
68
|
- spec/spec.opts
|
68
69
|
- spec/spec_helper.rb
|
@@ -103,6 +104,7 @@ signing_key:
|
|
103
104
|
specification_version: 3
|
104
105
|
summary: Rack middleware for the LockBox centralized API authorization service.
|
105
106
|
test_files:
|
107
|
+
- spec/lib/lockbox_cache_spec.rb
|
106
108
|
- spec/lib/lockbox_middleware_spec.rb
|
107
109
|
- spec/spec.opts
|
108
110
|
- spec/spec_helper.rb
|