simple_api_auth 0.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 +1 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +26 -0
- data/README.md +27 -0
- data/Rakefile +7 -0
- data/lib/simple_api_auth.rb +2 -0
- data/lib/simple_api_auth/railtie.rb +9 -0
- data/lib/simple_api_auth/simple_api_auth.rb +35 -0
- data/simple_api_auth.gemspec +20 -0
- data/spec/lib/simple_api_auth_spec.rb +45 -0
- data/spec/spec_helper.rb +12 -0
- metadata +107 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
simple_api_auth (0.0.1)
|
5
|
+
rack
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.1.2)
|
11
|
+
rack (1.2.2)
|
12
|
+
rspec (2.5.0)
|
13
|
+
rspec-core (~> 2.5.0)
|
14
|
+
rspec-expectations (~> 2.5.0)
|
15
|
+
rspec-mocks (~> 2.5.0)
|
16
|
+
rspec-core (2.5.1)
|
17
|
+
rspec-expectations (2.5.0)
|
18
|
+
diff-lcs (~> 1.1.2)
|
19
|
+
rspec-mocks (2.5.0)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
rspec (~> 2.5)
|
26
|
+
simple_api_auth!
|
data/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# Simple API Key Authentication
|
2
|
+
|
3
|
+
A Rack middleware and Railtie(for Rails3) for API key authentication.
|
4
|
+
It will authenticate all requests to /api/* or /apis/*
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
### Gemfile
|
9
|
+
|
10
|
+
gem "simple_api_auth"
|
11
|
+
|
12
|
+
### config/api_key.yml
|
13
|
+
|
14
|
+
api_key: you_api_key
|
15
|
+
|
16
|
+
### Generate a random API key
|
17
|
+
|
18
|
+
rake generate
|
19
|
+
|
20
|
+
### Run spec
|
21
|
+
|
22
|
+
bundle exec rspec spec
|
23
|
+
|
24
|
+
## License
|
25
|
+
|
26
|
+
Copyright © 2011 Wen-Tien Chang
|
27
|
+
Licensed under the MIT: http://www.opensource.org/licenses/mit-license.php
|
data/Rakefile
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
module SimpleApiAuth
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
initializer "simple_api_auth_railtie.configure_rails_initialization" do |config|
|
4
|
+
SimpleApiAuth::Middleware.api_key = YAML.load_file("#{Rails.root}/config/api_key.yml")["api_key"]
|
5
|
+
config.middleware.use SimpleApiAuth::Middleware
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module SimpleApiAuth
|
2
|
+
|
3
|
+
class Middleware
|
4
|
+
|
5
|
+
@@api_path_matcher = /^\/api\//
|
6
|
+
@@unauthorized_response = [401, {"Content-Type" => "text/plain"}, ["Unauthorized."]]
|
7
|
+
|
8
|
+
def self.api_key=(key)
|
9
|
+
@@api_key = key
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(app)
|
13
|
+
@app = app
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(env)
|
17
|
+
path = env['PATH_INFO']
|
18
|
+
if path =~ @@api_path_matcher
|
19
|
+
api_key = env["Authorization"] || Rack::Request.new(env).params['api_key']
|
20
|
+
unless api_key == @@api_key
|
21
|
+
return @@unauthorized_response
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
@app.call(env)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.generate(length = 40)
|
31
|
+
require "digest/sha1"
|
32
|
+
Digest::SHA1.hexdigest(Time.now.to_s + rand(99999999).to_s)[0..length-1]
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "simple_api_auth"
|
5
|
+
s.version = "0.0.1"
|
6
|
+
s.platform = Gem::Platform::RUBY
|
7
|
+
s.authors = ["Wen-Tien Chang"]
|
8
|
+
s.email = ["ihower@gmail.com"]
|
9
|
+
s.homepage = "http://ihower.tw"
|
10
|
+
s.summary = %q{Simple API Key Authentication}
|
11
|
+
s.description = %q{A Rack middleware and Railtie(for Rails3) for API key authentication.}
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
|
15
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
|
18
|
+
s.add_dependency "rack"
|
19
|
+
s.add_development_dependency "rspec", "~> 2.5"
|
20
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SimpleApiAuth do
|
4
|
+
|
5
|
+
before do
|
6
|
+
SimpleApiAuth::Middleware.api_key = "abc"
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:app) {
|
10
|
+
Rack::Builder.new do
|
11
|
+
use SimpleApiAuth::Middleware
|
12
|
+
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ["Hello, World!"]] }
|
13
|
+
end
|
14
|
+
}
|
15
|
+
|
16
|
+
describe SimpleApiAuth::Middleware do
|
17
|
+
it "should pass if request is not for /api" do
|
18
|
+
response = Rack::MockRequest.new(app).get('/')
|
19
|
+
response.status.should == 200
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should pass if request is authorized by header" do
|
23
|
+
|
24
|
+
response = Rack::MockRequest.new(app).get('/api/test', "Authorization" => "abc" )
|
25
|
+
response.status.should == 200
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should pass if request is authorized by params[:api_key]" do
|
29
|
+
response = Rack::MockRequest.new(app).get('/api/test?api_key=abc')
|
30
|
+
response.status.should == 200
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should return 401 if request is unauthorized" do
|
34
|
+
response = Rack::MockRequest.new(app).get('/api/test')
|
35
|
+
response.status.should == 401
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#generate" do
|
40
|
+
it "should generate a random string" do
|
41
|
+
SimpleApiAuth.generate.should be_a_kind_of String
|
42
|
+
SimpleApiAuth.generate.size.should == 40
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path("../../lib/simple_api_auth", __FILE__)
|
2
|
+
require 'rack/mock'
|
3
|
+
|
4
|
+
# Requires supporting ruby files with custom matchers and macros, etc,
|
5
|
+
# in spec/support/ and its subdirectories.
|
6
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
|
10
|
+
config.mock_with :rspec
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_api_auth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Wen-Tien Chang
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-05-03 00:00:00 +08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rack
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rspec
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 9
|
44
|
+
segments:
|
45
|
+
- 2
|
46
|
+
- 5
|
47
|
+
version: "2.5"
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
description: A Rack middleware and Railtie(for Rails3) for API key authentication.
|
51
|
+
email:
|
52
|
+
- ihower@gmail.com
|
53
|
+
executables: []
|
54
|
+
|
55
|
+
extensions: []
|
56
|
+
|
57
|
+
extra_rdoc_files: []
|
58
|
+
|
59
|
+
files:
|
60
|
+
- .gitignore
|
61
|
+
- Gemfile
|
62
|
+
- Gemfile.lock
|
63
|
+
- README.md
|
64
|
+
- Rakefile
|
65
|
+
- lib/simple_api_auth.rb
|
66
|
+
- lib/simple_api_auth/railtie.rb
|
67
|
+
- lib/simple_api_auth/simple_api_auth.rb
|
68
|
+
- simple_api_auth.gemspec
|
69
|
+
- spec/lib/simple_api_auth_spec.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
has_rdoc: true
|
72
|
+
homepage: http://ihower.tw
|
73
|
+
licenses: []
|
74
|
+
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
hash: 3
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 3
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
requirements: []
|
99
|
+
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.6.2
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: Simple API Key Authentication
|
105
|
+
test_files:
|
106
|
+
- spec/lib/simple_api_auth_spec.rb
|
107
|
+
- spec/spec_helper.rb
|